Skip to content

Instantly share code, notes, and snippets.

@melcher
Created February 6, 2014 23:53
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 melcher/8854953 to your computer and use it in GitHub Desktop.
Save melcher/8854953 to your computer and use it in GitHub Desktop.
Adds support for OPTIONS verb testing in rspec-rails Request (integration) tests
module RequestHelper
#...
# Add support for testing `options` requests in rspec.
# See: https://github.com/rspec/rspec-rails/issues/925
def options(*args)
reset! unless integration_session
integration_session.__send__(:process, :options, *args).tap do
copy_session_variables!
end
end
end
@monfresh
Copy link

Hi Graham. Thanks for this gist. I'm trying to test CORS as well with RSpec. I'm using the rack-cors gem which requires the Origin HTTP header to be passed in the request. My specs are failing and I'm pretty sure it's because I'm not passing in the Origin header correctly. I get the correct HTTP status in the response, so I know it's actually hitting the correct :options route, but it's not returning any Access-Control-Allow headers, which means that the Origin wasn't passed. I've tested it via curl, so I know everything is actually working, so the tests should be passing.

Here's how I include the Origin header in a regular POST request in an integration spec:

post '/api/locations', { name: 'foo' }, 'HTTP_ORIGIN' => 'http://ohanapi.org'

When I try the same thing with your options method, the Origin header doesn't seem to be recognized:

options '/api/locations', {}, 'HTTP_ORIGIN' => 'http://ohanapi.org'

I also tried removing the middle hash, but that didn't work either:

options '/api/locations', 'HTTP_ORIGIN' => 'http://ohanapi.org'

What am I doing wrong?

Thanks!

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