Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework mime type matching and provides specs #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 89 additions & 50 deletions spec/http_methods/get/conditions/provides_condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -402,4 +441,4 @@
expect(response[2]).to be == ['text/xml;charset=utf-8']
end

end
end