Skip to content

Commit

Permalink
added spec for expected headers when error raised
Browse files Browse the repository at this point in the history
When we set headers using `before`, `after` or just before `error!` raised, we expect them to transferred with error response together.

* First spec will be `success` due to there is no error.
* Second test will fail at the moment because when an error raised, all headers will be ignored. They shouldn't be.

added spec for expected headers when error raised

When we set headers using `before`, `after` or just before `error!` raised, we expect them to transferred with error response together.

* First spec will be `success` due to there is no error.
* Second test will fail at the moment because when an error raised, all headers will be ignored. They shouldn't be.
  • Loading branch information
gencer committed Dec 21, 2017
1 parent 95484d5 commit 59f1e15
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions spec/grape/headers_on_error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'spec_helper'

describe Grape::API do
let(:error_header) do
Class.new(Grape::API) do
before do
header 'X-Grape-Before-Header', '1'
end
after do
header 'X-Grape-After-Header', '1'
end
get '/success' do
header 'X-Grape-Returns-Error', '1'
end
get '/error' do
header 'X-Grape-Returns-Error', '1'
error!(success: false)
end
end
end

subject do
ErrorHeader = error_header unless defined?(ErrorHeader)
Class.new(Grape::API) do
format :json
mount ErrorHeader => '/'
end
end

def app
subject
end

it 'should returns all headers on success' do
get '/success'
expect(last_response.headers['X-Grape-Returns-Error']).to eq('1')
expect(last_response.headers['X-Grape-Before-Header']).to eq('1')
expect(last_response.headers['X-Grape-After-Header']).to eq('1')
end

it 'should returns all headers on error' do
get '/error'
expect(last_response.headers['X-Grape-Returns-Error']).to eq('1')
expect(last_response.headers['X-Grape-Before-Header']).to eq('1')
expect(last_response.headers['X-Grape-After-Header']).to eq('1')
end
end

0 comments on commit 59f1e15

Please sign in to comment.