|
15 | 15 | params = params_for_request '{"key":"value"}', "application/json; charset=utf-8"
|
16 | 16 | params['key'].must_equal "value"
|
17 | 17 | end
|
18 |
| - |
| 18 | + |
19 | 19 | specify "should parse 'application/json' requests with empty body" do
|
20 | 20 | params = params_for_request "", "application/json"
|
21 | 21 | params.must_equal({})
|
|
33 | 33 | result.must_be_empty
|
34 | 34 | end
|
35 | 35 |
|
| 36 | + describe "contradiction between body and type" do |
| 37 | + def assert_failed_to_parse_as_json(response) |
| 38 | + response.wont_equal nil |
| 39 | + status, headers, body = response |
| 40 | + status.must_equal 400 |
| 41 | + body.must_equal ["failed to parse body as JSON"] |
| 42 | + end |
| 43 | + |
| 44 | + specify "should return bad request with invalid JSON" do |
| 45 | + test_body = '"bar":"foo"}' |
| 46 | + env = Rack::MockRequest.env_for "/", {:method => "POST", :input => test_body, "CONTENT_TYPE" => 'application/json'} |
| 47 | + app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, Rack::Request.new(env).POST] } |
| 48 | + response = Rack::PostBodyContentTypeParser.new(app).call(env) |
| 49 | + |
| 50 | + assert_failed_to_parse_as_json(response) |
| 51 | + end |
| 52 | + end |
36 | 53 | end
|
37 | 54 |
|
38 | 55 | def params_for_request(body, content_type)
|
39 | 56 | env = Rack::MockRequest.env_for "/", {:method => "POST", :input => body, "CONTENT_TYPE" => content_type}
|
40 | 57 | app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, Rack::Request.new(env).POST] }
|
41 | 58 | Rack::PostBodyContentTypeParser.new(app).call(env).last
|
42 | 59 | end
|
43 |
| - |
| 60 | + |
44 | 61 | rescue LoadError => e
|
45 | 62 | # Missing dependency JSON, skipping tests.
|
46 | 63 | STDERR.puts "WARN: Skipping Rack::PostBodyContentTypeParser tests (json not installed)"
|
|
0 commit comments