Skip to content

Commit

Permalink
JSONP middleware shouldn't change the response when the request isn't…
Browse files Browse the repository at this point in the history
… a json request and/or when the response isn't a json response.
  • Loading branch information
lawrencepit committed Jun 3, 2010
1 parent 5c87420 commit 356dcdd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
10 changes: 6 additions & 4 deletions lib/rack/contrib/jsonp.rb
Expand Up @@ -17,10 +17,12 @@ def initialize(app)
#
def call(env)
status, headers, response = @app.call(env)
request = Rack::Request.new(env)
if request.params.include?('callback')
response = pad(request.params.delete('callback'), response)
headers['Content-Length'] = response.length.to_s
if env['HTTP_ACCEPT'] =~ /application\/json/ && headers['Content-Type'] =~ /application\/json/
request = Rack::Request.new(env)
if request.params.include?('callback')
response = pad(request.params.delete('callback'), response)
headers['Content-Length'] = response.length.to_s
end
end
[status, headers, response]
end
Expand Down
28 changes: 22 additions & 6 deletions test/spec_rack_jsonp.rb
Expand Up @@ -8,27 +8,43 @@
specify "should wrap the response body in the Javascript callback" do
test_body = '{"bar":"foo"}'
callback = 'foo'
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [test_body]] }
request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}")
app = lambda { |env| [200, {'Content-Type' => 'application/json'}, [test_body]] }
request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}", 'HTTP_ACCEPT' => 'application/json')
body = Rack::JSONP.new(app).call(request).last
body.should.equal "#{callback}(#{test_body})"
end

specify "should modify the content length to the correct value" do
test_body = '{"bar":"foo"}'
callback = 'foo'
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [test_body]] }
request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}")
app = lambda { |env| [200, {'Content-Type' => 'application/json'}, [test_body]] }
request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}", 'HTTP_ACCEPT' => 'application/json')
headers = Rack::JSONP.new(app).call(request)[1]
headers['Content-Length'].should.equal((test_body.length + callback.length + 2).to_s) # 2 parentheses
end
end

specify "should not change anything if no callback param is provided" do
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['{"bar":"foo"}']] }
request = Rack::MockRequest.env_for("/", :params => "foo=bar")
app = lambda { |env| [200, {'Content-Type' => 'application/json'}, ['{"bar":"foo"}']] }
request = Rack::MockRequest.env_for("/", :params => "foo=bar", 'HTTP_ACCEPT' => 'application/json')
body = Rack::JSONP.new(app).call(request).last
body.join.should.equal '{"bar":"foo"}'
end

specify "should not change anything if it's not a json request" do
test_body = '{"bar":"foo"}'
app = lambda { |env| [200, {'Content-Type' => 'application/json'}, [test_body]] }
request = Rack::MockRequest.env_for("/", :params => "callback=foo", 'HTTP_ACCEPT' => 'application/html')
body = Rack::JSONP.new(app).call(request).last
body.should.equal [test_body]
end

specify "should not change anything if it's not a json response" do
test_body = '<html><body>404 Not Found</body></html>'
app = lambda { |env| [404, {'Content-Type' => 'text/html'}, [test_body]] }
request = Rack::MockRequest.env_for("/", :params => "callback=foo", 'HTTP_ACCEPT' => 'application/json')
body = Rack::JSONP.new(app).call(request).last
body.should.equal [test_body]
end

end

0 comments on commit 356dcdd

Please sign in to comment.