Skip to content

Commit 59f1e15

Browse files
committed
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. 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.
1 parent 95484d5 commit 59f1e15

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

spec/grape/headers_on_error_spec.rb

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'spec_helper'
2+
3+
describe Grape::API do
4+
let(:error_header) do
5+
Class.new(Grape::API) do
6+
before do
7+
header 'X-Grape-Before-Header', '1'
8+
end
9+
after do
10+
header 'X-Grape-After-Header', '1'
11+
end
12+
get '/success' do
13+
header 'X-Grape-Returns-Error', '1'
14+
end
15+
get '/error' do
16+
header 'X-Grape-Returns-Error', '1'
17+
error!(success: false)
18+
end
19+
end
20+
end
21+
22+
subject do
23+
ErrorHeader = error_header unless defined?(ErrorHeader)
24+
Class.new(Grape::API) do
25+
format :json
26+
mount ErrorHeader => '/'
27+
end
28+
end
29+
30+
def app
31+
subject
32+
end
33+
34+
it 'should returns all headers on success' do
35+
get '/success'
36+
expect(last_response.headers['X-Grape-Returns-Error']).to eq('1')
37+
expect(last_response.headers['X-Grape-Before-Header']).to eq('1')
38+
expect(last_response.headers['X-Grape-After-Header']).to eq('1')
39+
end
40+
41+
it 'should returns all headers on error' do
42+
get '/error'
43+
expect(last_response.headers['X-Grape-Returns-Error']).to eq('1')
44+
expect(last_response.headers['X-Grape-Before-Header']).to eq('1')
45+
expect(last_response.headers['X-Grape-After-Header']).to eq('1')
46+
end
47+
end

0 commit comments

Comments
 (0)