Skip to content
This repository has been archived by the owner on Aug 21, 2019. It is now read-only.

Enable CORS for GET and OPTIONS requests #3

Merged
merged 1 commit into from
Apr 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gem 'net-http-persistent'
gem 'airbrake'
gem 'rack-contrib'
gem 'jbuilder'
gem 'rack-cors'

gem 'us_states', :git => 'git://github.com/GSA/us_states.git'

Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ GEM
rack (>= 0.4)
rack-contrib (1.1.0)
rack (>= 0.9.1)
rack-cors (0.3.1)
rack-ssl (1.3.3)
rack
rack-test (0.6.2)
Expand Down Expand Up @@ -189,6 +190,7 @@ DEPENDENCIES
nokogiri
oj
rack-contrib
rack-cors
rails (= 3.2.13)
rails-api
rspec
Expand Down
6 changes: 6 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ class Application < Rails::Application

config.middleware.use Rack::JSONP

config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :options]
end
end
end
end
34 changes: 34 additions & 0 deletions spec/cors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

class DummyController < ApplicationController
def show
render text: 'text'
end
end

describe DummyController, type: :request do
describe '#show' do
before do
Rails.application.routes.draw do
match '/show' => 'dummy#show'
end

get 'show', nil, { 'HTTP_ORIGIN' => 'http://www.example.com' }
end

after do
Rails.application.reload_routes!
end

it 'should respond with an "Access-Control-Allow-Origin" header' do
expect(headers.keys).to include('Access-Control-Allow-Origin')
end

%w[GET OPTIONS].each do |verb|
it "should respond with a 'Access-Control-Allow-Methods' header allowing #{verb}" do
allowed_methods = headers['Access-Control-Allow-Methods'].split(', ')
expect(allowed_methods).to include(verb)
end
end
end
end