From 7df4982632b31bac132e1c0fc3273a5f204d7a37 Mon Sep 17 00:00:00 2001 From: Matt Patterson Date: Fri, 2 Aug 2013 15:14:21 +0200 Subject: [PATCH] Rework mime type matching and provides specs --- .../get/conditions/provides_condition_spec.rb | 139 +++++++++++------- 1 file changed, 89 insertions(+), 50 deletions(-) diff --git a/spec/http_methods/get/conditions/provides_condition_spec.rb b/spec/http_methods/get/conditions/provides_condition_spec.rb index 8898c8e..1687f37 100644 --- a/spec/http_methods/get/conditions/provides_condition_spec.rb +++ b/spec/http_methods/get/conditions/provides_condition_spec.rb @@ -4,7 +4,7 @@ describe 'GET provide conditions' do - it 'matches mime_types with dots, hyphens and plus signs' do + context 'matching mime types with dots, hyphens and plus signs' do mime_types = %w( application/atom+xml application/ecmascript @@ -114,70 +114,109 @@ application/x-pkcs7-signature ) #iterate through the above array to majke a seperate test for each - mime_types.each { |mime_type| expect(mime_type).to match(Sinatra::Request::HEADER_VALUE_WITH_PARAMS) } + mime_types.each do |mime_type| + it "matches #{mime_type} correctly" do + expect(mime_type).to match(Sinatra::Request::HEADER_VALUE_WITH_PARAMS) + end + end end - it 'filters by accept header' do - app = Sinatra.new do - get '/', :provides => :xml do - env['HTTP_ACCEPT'] + context 'filtering by accept header' do + let(:app) { + Sinatra.new do + get '/', :provides => :xml do + env['HTTP_ACCEPT'] + end + get '/foo', :provides => :html do + env['HTTP_ACCEPT'] + end + get '/stream', :provides => 'text/event-stream' do + env['HTTP_ACCEPT'] + end end - get '/foo', :provides => :html do - env['HTTP_ACCEPT'] + } + + context 'a route which provides :xml' do + it 'correctly filters an explicit Accept: application/xml header' do + response = get '/', {}, {'HTTP_ACCEPT' => 'application/xml'} + + expect(response['Content-Type']).to be == 'application/xml;charset=utf-8' + expect(response.body).to be == 'application/xml' end - get '/stream', :provides => 'text/event-stream' do - env['HTTP_ACCEPT'] + + it 'matches the route with a blank Accept header' do + response = get '/', {}, {'HTTP_ACCEPT' => ''} + expect(response['Content-Type']).to be == 'application/xml;charset=utf-8' + expect(response.body).to be == '' end - end - #132-135 should be an it block - response = app.call 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/', 'HTTP_ACCEPT' => 'application/xml', 'rack.input' => '' - - expect(response[1]['Content-Type']).to be == 'application/xml;charset=utf-8' - expect(response[2]).to be == ['application/xml'] - response = app.call 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/', 'HTTP_ACCEPT' => '','rack.input' => '' - expect(response[0]).to be == 200 - expect(response[1]['Content-Type']).to be == 'application/xml;charset=utf-8' - expect(response[2]).to be == [''] + it 'matches the route when handed the catch-all */* header' do + response = get '/', {}, {'HTTP_ACCEPT' => '*/*'} + expect(response['Content-Type']).to be == 'application/xml;charset=utf-8' + expect(response.body).to be == '*/*' + end - response = app.call 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/', 'HTTP_ACCEPT' => '*/*', 'rack.input' => '' - expect(response[0]).to be == 200 - expect(response[1]['Content-Type']).to be == 'application/xml;charset=utf-8' - expect(response[2]).to be == ['*/*'] + it "doesn't match when an unsupported mime type is explictly asked for" do + response = get '/', {}, {'HTTP_ACCEPT' => 'text/html;q=0.9'} + expect(response.status).to be == 404 + end + end - response = app.call 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/', 'HTTP_ACCEPT' => 'text/html;q=0.9', 'rack.input' => '' - expect(response[0]).to be == 404 + context "A route which provides :html" do + it "correctly routes an explicit text/html Accept header" do + response = get '/foo', {}, {'HTTP_ACCEPT' => 'text/html'} + expect(response.status).to be == 200 + expect(response.body).to be == 'text/html' + end - response = app.call 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/foo', 'HTTP_ACCEPT' => 'text/html;q=0.9', 'rack.input' => '' - expect(response[0]).to be == 200 - expect(response[2]).to be == ['text/html;q=0.9'] + it "correctly routes an explicit text/html, with quality, Accept header" do + response = get '/foo', {}, {'HTTP_ACCEPT' => 'text/html;q=0.9'} + expect(response.status).to be == 200 + expect(response.body).to be == 'text/html;q=0.9' + end - response = app.call 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/foo', 'HTTP_ACCEPT' => '','rack.input' => '' - expect(response[0]).to be == 200 - expect(response[2]).to be == [''] + it "matches the route with a blank Accept header" do + response = get '/foo', {}, {'HTTP_ACCEPT' => ''} + expect(response.status).to be == 200 + expect(response.body).to be == '' + end - response = app.call 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/foo', 'HTTP_ACCEPT' => '*/*', 'rack.input' => '' - expect(response[0]).to be == 200 - expect(response[2]).to be == ['*/*'] + it 'matches the route when handed the catch-all */* header' do + response = get '/foo', {}, {'HTTP_ACCEPT' => '*/*'} + expect(response.status).to be == 200 + expect(response.body).to be == '*/*' + end - response = app.call 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/foo', 'HTTP_ACCEPT' => 'application/xml', 'rack.input' => '' - expect(response[0]).to be == 404 + it "doesn't match when an unsupported mime type is explictly asked for" do + response = get '/foo', {}, {'HTTP_ACCEPT' => 'application/xml'} + expect(response.status).to be == 404 + end + end - response = app.call 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/stream', 'HTTP_ACCEPT' => 'text/event-stream', 'rack.input' => '' - expect(response[0]).to be == 200 - expect(response[2]).to be == ['text/event-stream'] + context "A route which provides text/event-stream" do + it "correctly routes an explicit text/event-stream Accept header" do + response = get '/stream', {}, {'HTTP_ACCEPT' => 'text/event-stream'} + expect(response.status).to be == 200 + expect(response.body).to be == 'text/event-stream' + end - response = app.call 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/stream', 'HTTP_ACCEPT' => '', 'rack.input' => '' - expect(response[0]).to be == 200 - expect(response[2]).to be == [''] - - response = app.call 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/stream', 'HTTP_ACCEPT' => '*/*', 'rack.input' => '' - expect(response[0]).to be == 200 - expect(response[2]).to be == ['*/*'] + it "matches the route with a blank Accept header" do + response = get '/stream', {}, {'HTTP_ACCEPT' => ''} + expect(response.status).to be == 200 + expect(response.body).to be == '' + end - response = app.call 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/stream', 'HTTP_ACCEPT' => 'application/xml', 'rack.input' => '' - expect(response[0]).to be == 404 + it 'matches the route when handed the catch-all */* header' do + response = get '/stream', {}, {'HTTP_ACCEPT' => '*/*'} + expect(response.status).to be == 200 + expect(response.body).to be == '*/*' + end + it "doesn't match when an unsupported mime type is explictly asked for" do + response = get '/stream', {}, {'HTTP_ACCEPT' => 'application/xml'} + expect(response.status).to be == 404 + end + end end @@ -402,4 +441,4 @@ expect(response[2]).to be == ['text/xml;charset=utf-8'] end -end \ No newline at end of file +end