spec/integration/jsonp_spec.rb in goliath-1.0.4 vs spec/integration/jsonp_spec.rb in goliath-1.0.5

- old
+ new

@@ -4,28 +4,79 @@ use Goliath::Rack::Params use Goliath::Rack::JSONP use Goliath::Rack::Render, 'json' def response(env) - [200, {'CONTENT_TYPE' => 'application/json'}, "OK"] + headers = { + 'CONTENT_TYPE' => 'application/json', + # Note: specifically not 'CONTENT_LENGTH'. 'Content-Length' gets set by + # AsyncRack::ContentLength if not already present. So if we set + # 'CONTENT_LENGTH', no problem - AsyncRack recalculates the body's + # content length anyway and stores it in the 'Content-Length' header. But + # if we set 'Content-Length', AsyncRack will avoid overwriting it. We + # thus want the JSONP middleware to react to the case where we've + # *already* set the 'Content-Length' header before hitting it. + 'Content-Length' => '2', + } + [200, headers, 'OK'] end end describe 'JSONP' do let(:err) { Proc.new { fail "API request failed" } } - it 'sets the content type' do - with_api(JSON_API) do - get_request({:query => {:callback => 'test'}}, err) do |c| - c.response_header['CONTENT_TYPE'].should =~ %r{^application/javascript} + context 'without a callback param' do + let(:query) { {} } + + it 'does not alter the content type' do + with_api(JSON_API) do + get_request({ query: query }, err) do |c| + c.response_header['CONTENT_TYPE'].should =~ %r{^application/json} + end end end + + it 'does not alter the content length' do + with_api(JSON_API) do + get_request({ query: query }, err) do |c| + c.response_header['CONTENT_LENGTH'].to_i.should == 2 + end + end + end + + it 'does not wrap the response with anything' do + with_api(JSON_API) do + get_request({ query: query }, err) do |c| + c.response.should == 'OK' + end + end + end end - it 'wraps response with callback' do - with_api(JSON_API) do - get_request({:query => {:callback => 'test'}}, err) do |c| - c.response.should =~ /^test\(.*\)$/ + context 'with a callback param' do + let(:query) { { callback: 'test' } } + + it 'adjusts the content type' do + with_api(JSON_API) do + get_request({ query: query }, err) do |c| + c.response_header['CONTENT_TYPE'].should =~ %r{^application/javascript} + end end end + + it 'adjusts the content length' do + with_api(JSON_API) do + get_request({ query: query }, err) do |c| + c.response_header['CONTENT_LENGTH'].to_i.should == 8 + end + end + end + + it 'wraps response with callback' do + with_api(JSON_API) do + get_request({ query: query }, err) do |c| + c.response.should =~ /^test\(.*\)$/ + end + end + end end -end \ No newline at end of file +end