Skip to content

Commit

Permalink
Support non-preflight OPTIONS requests
Browse files Browse the repository at this point in the history
An alternative fix to #28
  • Loading branch information
cyu committed Jul 14, 2013
1 parent 44f0d91 commit 910f6df
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/rack/cors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def call(env)
" Access-Control-Request-Headers: #{env['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"
].join("\n")
end
if env['REQUEST_METHOD'] == 'OPTIONS'
if env['REQUEST_METHOD'] == 'OPTIONS' and env['HTTP_ACCESS_CONTROL_REQUEST_METHOD']
if headers = process_preflight(env)
debug(env) do
"Preflight Headers:\n" +
Expand Down
17 changes: 8 additions & 9 deletions test/unit/cors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def app

should('support simple cors request') { cors_request }

should 'support OPTIONS cors request' do
cors_request '/options', :method => :options
end

should 'support regex origins configuration' do
cors_request :origin => 'http://192.168.0.1:1234'
end
Expand Down Expand Up @@ -103,11 +107,6 @@ def app
assert_cors_failure
end

should 'fail if Access-Control-Request-Method does not exist' do
preflight_request('http://localhost:3000', '/', :method => nil)
assert_cors_failure
end

should 'fail if Access-Control-Request-Method is not allowed' do
preflight_request('http://localhost:3000', '/get-only', :method => :post)
assert_cors_failure
Expand Down Expand Up @@ -167,11 +166,11 @@ def app
def cors_request(*args)
path = args.first.is_a?(String) ? args.first : '/'

opts = args.last.is_a?(Hash) ? args.last : {:origin => 'http://localhost:3000'}
origin = opts[:origin]
opts = { :method => :get, :origin => 'http://localhost:3000' }
opts.merge! args.last if args.last.is_a?(Hash)

header 'Origin', origin
get path
header 'Origin', opts[:origin]
current_session.__send__ opts[:method], path
assert_cors_success
end

Expand Down
1 change: 1 addition & 0 deletions test/unit/test.ru
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use Rack::Cors do

resource '/get-only', :methods => :get
resource '/', :headers => :any
resource '/options', :methods => :options
resource '/single_header', :headers => 'x-domain-token'
resource '/two_headers', :headers => %w{x-domain-token x-requested-with}
resource '/expose_single_header', :expose => 'expose-test'
Expand Down

0 comments on commit 910f6df

Please sign in to comment.