Skip to content

Instantly share code, notes, and snippets.

@haarts
Created January 19, 2012 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haarts/1640182 to your computer and use it in GitHub Desktop.
Save haarts/1640182 to your computer and use it in GitHub Desktop.
Failing Rack::Cors Rspec
# in config/application.rb
config.middleware.use Rack::Cors do
allow do
origins '*'
resource '/search*', :headers => :any, :methods => :get
end
end
# in spec/requests/cors_spec.rb
require 'spec_helper'
describe "Cors" do
it "returns the correct headers" do
get '/search/index', {:auth_token => @user.authentication_token, :q => "ladyagaga"}
response.headers.should_not have_key("Access-Control-Allow-Origin")
# request.env["Origin"] = "*" #doesn't work
get '/search/index', {:q => "ladyagaga"}, {"Origin" => "*"} #doesn't work either
response.headers.should have_key("Access-Control-Allow-Origin")
end
end
@patbenatar
Copy link

@haarts Did you ever have any luck getting this to work? I'm running into the same problem today :-/

@dotemacs
Copy link

By the looks of things, this seems like an example in a Rails app. But since there isn't anything 'in the wild' on how to do it, and I've just got it going for a Sinatra app, this might help:

post '/search',
json,
{ 'HTTP_ORIGIN' => 'http://super-test.com',
  'Access-Control-Request-Method' => 'POST',
  'HTTP_ACCEPT' => "application/json" }

with the example verification:

last_response.headers['Access-Control-Allow-Origin'].should eq 'http://super-test.com'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment