-
Notifications
You must be signed in to change notification settings - Fork 73
/
test_server
executable file
·56 lines (46 loc) · 1.08 KB
/
test_server
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
#!/usr/bin/env ruby
require 'yaml'
require 'webrick'
include WEBrick
class TestServlet < HTTPServlet::AbstractServlet
def respond_with(method, req, res)
res.body = req.to_yaml
res['Content-Type'] = "text/plain"
end
def do_GET(req,res)
respond_with(:GET, req, res)
end
def do_POST(req,res)
respond_with(:POST, req, res)
end
def do_PUT(req,res)
respond_with(:PUT, req, res)
end
def do_DELETE(req,res)
respond_with(:DELETE, req, res)
end
end
class TimeoutServlet < HTTPServlet::AbstractServlet
def do_GET(req,res)
sleep(1.1)
end
end
class RedirectServlet < HTTPServlet::AbstractServlet
def do_GET(req,res)
res['Location'] = "http://localhost:9001/test"
res.status = 301
end
end
server = WEBrick::HTTPServer.new :Port => 9001
server.mount("/test", TestServlet)
server.mount("/timeout", TimeoutServlet)
server.mount("/redirect", RedirectServlet)
exit_code = lambda do
begin
server.shutdown unless server.nil?
rescue Object => e
puts "Error #{__FILE__}:#{__LINE__}\n#{e.message}"
end
end
trap("INT"){exit_code.call}
server.start