From ea13fab9a1151cde21c147fac0f0f206ba75efd2 Mon Sep 17 00:00:00 2001 From: Matt Haynes Date: Fri, 3 Sep 2010 00:10:38 +0800 Subject: [PATCH] Modify JSONP middleware to not fail if Content-Type header is missing --- lib/rack/contrib/jsonp.rb | 2 +- test/spec_rack_jsonp.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/rack/contrib/jsonp.rb b/lib/rack/contrib/jsonp.rb index 4c187080..0c35c164 100644 --- a/lib/rack/contrib/jsonp.rb +++ b/lib/rack/contrib/jsonp.rb @@ -41,7 +41,7 @@ def call(env) private def is_json?(headers) - headers['Content-Type'].include?('application/json') + headers.key?('Content-Type') && headers['Content-Type'].include?('application/json') end def has_callback?(request) diff --git a/test/spec_rack_jsonp.rb b/test/spec_rack_jsonp.rb index eb6570ff..dc9d1c55 100644 --- a/test/spec_rack_jsonp.rb +++ b/test/spec_rack_jsonp.rb @@ -69,5 +69,13 @@ body = Rack::JSONP.new(app).call(request).last body.should.equal [test_body] end + + specify "should not change anything if there is no Content-Type header" do + test_body = '404 Not Found' + app = lambda { |env| [404, {}, [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 \ No newline at end of file