Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5abf22

Browse files
committed
support all methods and two headers
1 parent 849776d commit c5abf22

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

app.rb

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,39 @@
22
$stdout.sync = true
33

44
class App < Sinatra::Base
5+
use Rack::CommonLogger
6+
57
get '/get' do
6-
puts "/get"
78
'OK'
89
end
910

1011
get '/get-header' do
11-
puts "/get-header"
1212
test_header
1313
'OK'
1414
end
1515

16-
post '/post' do
17-
puts "/post"
18-
test_post_body
16+
get '/get-headers' do
17+
test_two_headers
1918
'OK'
2019
end
2120

22-
post '/post-headers' do
23-
puts '/post-headers'
24-
test_post_body
25-
test_header
26-
'OK'
21+
[:put, :post, :delete].each do |method|
22+
self.send(method, '/data') do
23+
test_post_body
24+
'OK'
25+
end
26+
27+
self.send(method, '/data-header') do
28+
test_post_body
29+
test_header
30+
'OK'
31+
end
32+
33+
self.send(method, '/data-headers') do
34+
test_post_body
35+
test_two_headers
36+
'OK'
37+
end
2738
end
2839

2940
private
@@ -33,8 +44,15 @@ def test_header
3344
halt 420, 'FAIL' unless header == 'true'
3445
end
3546

47+
def test_two_headers
48+
header1 = env['HTTP_X_TEST_HEADER1']
49+
header2 = env['HTTP_X_TEST_HEADER2']
50+
puts "header1=#{header1} header2=#{header2}"
51+
halt 420, 'FAIL' unless header1 == 'one' && header2 == 'two'
52+
end
53+
3654
def test_post_body
37-
postbody = request.body.string
55+
postbody = request.body.read
3856
puts "body=#{postbody}"
3957
halt 420, 'FAIL' unless postbody == "POSTDATA"
4058
end

0 commit comments

Comments
 (0)