Skip to content

Commit

Permalink
Make JSONP a noop when the status is 1xx, 204 or 304.
Browse files Browse the repository at this point in the history
  • Loading branch information
judofyr authored and mpalmer committed Jun 24, 2015
1 parent 77b8c5d commit 35c9772
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rack/contrib/jsonp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def call(env)

status, headers, response = @app.call(env)

if STATUS_WITH_NO_ENTITY_BODY.include?(status)
return status, headers, response
end

headers = HeaderHash.new(headers)

if is_json?(headers) && has_callback?(request)
Expand Down
9 changes: 9 additions & 0 deletions test/spec_rack_jsonp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,13 @@ def assert_bad_request(response)
body.must_equal [test_body]
end

specify "should not change anything if the request doesn't have a body" do
app1 = lambda { |env| [100, {}, []] }
app2 = lambda { |env| [204, {}, []] }
app3 = lambda { |env| [304, {}, []] }
request = Rack::MockRequest.env_for("/", :params => "callback=foo", 'HTTP_ACCEPT' => 'application/json')
Rack::JSONP.new(app1).call(request).must_equal app1.call({})
Rack::JSONP.new(app2).call(request).must_equal app2.call({})
Rack::JSONP.new(app3).call(request).must_equal app3.call({})
end
end

0 comments on commit 35c9772

Please sign in to comment.