Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only intercept preflight requests for fonts #34

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 5 additions & 1 deletion lib/font_assets/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def call(env)
@ssl_request = Rack::Request.new(env).scheme == "https"
# intercept the "preflight" request
if env["REQUEST_METHOD"] == "OPTIONS"
return [200, access_control_headers, []]
if ext = extension(env["PATH_INFO"]) and font_asset?(ext)
return [200, access_control_headers, []]
else
return @app.call(env)
end
else
code, headers, body = @app.call(env)
set_headers! headers, body, env["PATH_INFO"]
Expand Down
25 changes: 24 additions & 1 deletion spec/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
end
end

context 'for OPTIONS requests' do
context 'for OPTIONS requests to a font' do
let(:app) { load_app 'http://test.options' }
let(:response) { request app, '/test.ttf', :method => 'OPTIONS' }

Expand All @@ -159,6 +159,29 @@
end
end

context 'for OPTIONS requests to a non-font' do
let(:app) { load_app 'http://test.options' }
let(:response) { request app, '/test.json', :method => 'OPTIONS' }

context 'the response headers' do
subject { response[1] }

its(["Access-Control-Allow-Headers"]) { should_not == "x-requested-with" }
its(["Access-Control-Max-Age"]) { should_not == "3628800" }
its(['Access-Control-Allow-Methods']) { should_not == 'GET' }
its(['Access-Control-Allow-Origin']) { should_not == 'http://test.options' }

it 'should contain a Content-Type' do
subject['Content-Type'].should_not be_nil
end
end

context 'the response body' do
subject { response[2] }
it { should_not be_empty }
end
end


private

Expand Down