From 59f1e15eebd21a320b573d2162dd04224b17c457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gencer=20W=2E=20Gen=C3=A7?= Date: Thu, 21 Dec 2017 10:59:28 +0300 Subject: [PATCH] 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. --- spec/grape/headers_on_error_spec.rb | 47 +++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 spec/grape/headers_on_error_spec.rb diff --git a/spec/grape/headers_on_error_spec.rb b/spec/grape/headers_on_error_spec.rb new file mode 100644 index 000000000..e26c2f4ca --- /dev/null +++ b/spec/grape/headers_on_error_spec.rb @@ -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