Skip to content

Instantly share code, notes, and snippets.

@igneus
Last active September 18, 2016 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igneus/eda2b04a7e23fbd4d0bfc44df3d7d0b7 to your computer and use it in GitHub Desktop.
Save igneus/eda2b04a7e23fbd4d0bfc44df3d7d0b7 to your computer and use it in GitHub Desktop.
returns unhappy HTTP status codes you ask for.
# Application returning unhappy HTTP status codes you ask for, e.g.
# GET /404
# returns 404 Not Found etc.
# Created for manual testing of HTTP clients.
require 'sinatra'
require 'json'
KNOWN_ERRORS = {
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
}
get '/:error.?:format?' do
error = params['error'].to_i
error = 500 unless KNOWN_ERRORS.has_key? 500
case params['format']
when 'json'
[error, JSON.dump({error: KNOWN_ERRORS[error]})]
else
status error
erb :error, locals: {code: error, message: KNOWN_ERRORS[error]}
end
end
get '/' do
erb :index, locals: {errors: KNOWN_ERRORS}
end
__END__
@@error
<!doctype html>
<html>
<head><title><%= code %> <%= message %></title></head>
<body>
<h1><%= code %> <%= message %></h1>
</body>
</html>
@@index
<!doctype html>
<html>
<head><title>Available errors</title></head>
<body>
<h1>Available errors</h1>
<ul>
<% errors.each_pair do |code,message| %>
<li><a href="/<%= code %>"><%= code %> <%= message %></a></li>
<% end %>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment