diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index a042070731..756b94d1b9 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -567,6 +567,72 @@ def subject.enable_root_route! expect(last_response.headers['Content-Type']).to eql 'text/plain' end + describe 'adds an OPTIONS route that' do + before do + subject.before { header 'X-Custom-Header', 'foo' } + subject.get 'example' do + 'example' + end + subject.route :any, '*path' do + error! :not_found, 404 + end + options '/example' + end + + it 'returns a 204' do + expect(last_response.status).to eql 204 + end + + it 'has an empty body' do + expect(last_response.body).to be_blank + end + + it 'has an Allow header' do + expect(last_response.headers['Allow']).to eql 'OPTIONS, GET, HEAD' + end + + it 'has a X-Custom-Header' do + expect(last_response.headers['X-Custom-Header']).to eql 'foo' + end + + it 'has no Content-Type' do + expect(last_response.content_type).to be_nil + end + + it 'has no Content-Length' do + expect(last_response.content_length).to be_nil + end + end + + describe 'adds a 405 Not Allowed route that' do + before do + subject.before { header 'X-Custom-Header', 'foo' } + subject.post :example do + 'example' + end + subject.route :any, '*path' do + error! :not_found, 404 + end + get '/example' + end + + it 'returns a 405' do + expect(last_response.status).to eql 405 + end + + it 'contains error message in body' do + expect(last_response.body).to eq '405 Not Allowed' + end + + it 'has an Allow header' do + expect(last_response.headers['Allow']).to eql 'OPTIONS, POST' + end + + it 'has a X-Custom-Header' do + expect(last_response.headers['X-Custom-Header']).to eql 'foo' + end + end + context 'allows HEAD on a GET request that' do before do subject.get 'example' do