From c1393b8f8fec62a7a4f182dd2a570a7caf12b274 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Fri, 1 May 2015 15:41:35 +0000 Subject: [PATCH] Refactor API CORS specs --- .../api/cross_origin_request_spec.rb | 35 +++++++------------ spec/support/acceptance_helpers.rb | 4 +++ 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/spec/integration/api/cross_origin_request_spec.rb b/spec/integration/api/cross_origin_request_spec.rb index 2b0f7ed..dfd856d 100644 --- a/spec/integration/api/cross_origin_request_spec.rb +++ b/spec/integration/api/cross_origin_request_spec.rb @@ -12,36 +12,27 @@ describe 'API cross origin request' do end it 'responds to preflight request' do - @integration_session.send( - :process, - :options, - api_playlists_path(format: :json), - nil, - 'Origin' => origin + options api_playlists_path(format: :json), nil, 'Origin' => origin + expect(response.headers).to include( + 'Access-Control-Allow-Origin' => origin, + 'Access-Control-Allow-Credentials' => 'true', + 'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE', + 'Access-Control-Allow-Headers' => + 'Content-Type, Content-Length, X-Requested-With' ) - - expect(response.headers['Access-Control-Allow-Origin']).to eq origin - expect(response.headers['Access-Control-Allow-Credentials']).to eq 'true' - expect(response.headers['Access-Control-Allow-Methods']) - .to eq 'GET, POST, PUT, DELETE' - expect(response.headers['Access-Control-Allow-Headers']) - .to eq 'Content-Type, Content-Length, X-Requested-With' end it 'responds to basic request' do - # FIXME: replace with a more stable/generic action get api_playlists_path(format: :json), nil, 'Origin' => origin - - expect(response.headers['Access-Control-Allow-Origin']).to eq origin - expect(response.headers['Access-Control-Allow-Credentials']).to eq 'true' - expect(response.headers['Access-Control-Expose-Headers']) - .to eq 'Content-Length' + expect(response.headers).to include( + 'Access-Control-Allow-Origin' => origin, + 'Access-Control-Allow-Credentials' => 'true', + 'Access-Control-Expose-Headers' => 'Content-Length' + ) end it 'responds to request without origin' do - # FIXME: replace with a more stable/generic action - get api_playlists_path(format: :json) - + get api_playlists_path format: :json expect(response.headers['Access-Control-Allow-Origin']).to eq '' end end diff --git a/spec/support/acceptance_helpers.rb b/spec/support/acceptance_helpers.rb index 53fe41b..8ba6bc4 100644 --- a/spec/support/acceptance_helpers.rb +++ b/spec/support/acceptance_helpers.rb @@ -1,4 +1,8 @@ module AcceptanceHelpers + def options path, options = nil, headers = nil + @integration_session.send :process, :options, path, options, headers + end + def sign_in create :user do |o| visit new_session_path