1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
require 'webrick'
class ExampleService < WEBrick::HTTPServlet::AbstractServlet
PORT = 65432 # rubocop:disable NumericLiterals
def do_GET(request, response) # rubocop:disable MethodName
case request.path
when '/'
handle_root(request, response)
when '/params'
handle_params(request, response)
when '/multiple-params'
handle_multiple_params(request, response)
when '/proxy'
response.status = 200
response.body = 'Proxy!'
when '/not-found'
response.body = 'not found'
response.status = 404
when '/redirect-301'
response.status = 301
response['Location'] = "http://127.0.0.1:#{PORT}/"
when '/redirect-302'
response.status = 302
response['Location'] = "http://127.0.0.1:#{PORT}/"
else
response.status = 404
end
end
def handle_root(request, response)
response.status = 200
case request['Accept']
when 'application/json'
response['Content-Type'] = 'application/json'
response.body = '{"json": true}'
else
response['Content-Type'] = 'text/html'
response.body = '<!doctype html>'
end
end
def handle_params(request, response)
if request.query_string == 'foo=bar'
response.status = 200
response.body = 'Params!'
end
end
def handle_multiple_params(request, response)
params = CGI.parse(request.query_string)
if params == {'foo' => ['bar'], 'baz' => ['quux']}
response.status = 200
response.body = 'More Params!'
end
end
def do_POST(request, response) # rubocop:disable MethodName
case request.path
when '/form'
if request.query['example'] == 'testing-form'
response.status = 200
response.body = 'passed :)'
else
response.status = 400
response.body = 'invalid! >:E'
end
when '/body'
if request.body == 'testing-body'
response.status = 200
response.body = 'passed :)'
else
response.status = 400
response.body = 'invalid! >:E'
end
else
response.status = 404
end
end
def do_HEAD(request, response) # rubocop:disable MethodName
case request.path
when '/'
response.status = 200
response['Content-Type'] = 'text/html'
else
response.status = 404
end
end
end
ExampleServer = WEBrick::HTTPServer.new(:Port => ExampleService::PORT, :AccessLog => [])
ExampleServer.mount '/', ExampleService
t = Thread.new { ExampleServer.start }
trap('INT') do
ExampleServer.shutdown
exit
end
Thread.pass while t.status && t.status != 'sleep'
|