-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow cors headers to be set by authenticators
- Loading branch information
Showing
10 changed files
with
219 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module Apia | ||
class CORS | ||
|
||
attr_accessor :methods | ||
attr_accessor :headers | ||
attr_accessor :origin | ||
|
||
def initialize | ||
@origin = '*' | ||
@methods = '*' | ||
@headers = [] | ||
end | ||
|
||
def to_headers | ||
return {} if @origin.nil? | ||
|
||
headers = {} | ||
headers['Access-Control-Allow-Origin'] = @origin | ||
|
||
if @methods.is_a?(String) | ||
headers['Access-Control-Allow-Methods'] = @methods | ||
elsif @methods.is_a?(Array) && @methods.any? | ||
headers['Access-Control-Allow-Methods'] = @methods.map(&:upcase).join(', ') | ||
end | ||
|
||
if @headers.is_a?(String) | ||
headers['Access-Control-Allow-Headers'] = @headers | ||
elsif @headers.is_a?(Array) && @headers.any? | ||
headers['Access-Control-Allow-Headers'] = @headers.join(', ') | ||
end | ||
|
||
headers | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'apia/cors' | ||
|
||
describe Apia::CORS do | ||
describe '#to_headers' do | ||
subject(:cors) { described_class.new } | ||
|
||
context 'with the details' do | ||
it 'returns a wildcard origin and methods' do | ||
expect(cors.to_headers).to eq({ 'Access-Control-Allow-Origin' => '*', | ||
'Access-Control-Allow-Methods' => '*' }) | ||
end | ||
end | ||
|
||
context 'when origin is set to nil' do | ||
it 'returns an empty array' do | ||
cors.origin = nil | ||
expect(cors.to_headers).to eq({}) | ||
end | ||
end | ||
|
||
context 'when origin is set to a hostname' do | ||
before do | ||
cors.origin = 'example.com' | ||
end | ||
|
||
it 'includes the Access-Control-Allow-Origin header' do | ||
expect(cors.to_headers).to eq({ | ||
'Access-Control-Allow-Origin' => 'example.com', | ||
'Access-Control-Allow-Methods' => '*' | ||
}) | ||
end | ||
|
||
context 'when methods have been provided' do | ||
it 'includes the Access-Control-Allow-Methods header' do | ||
cors.methods = %w[GET POST] | ||
expect(cors.to_headers).to eq({ | ||
'Access-Control-Allow-Origin' => 'example.com', | ||
'Access-Control-Allow-Methods' => 'GET, POST' | ||
}) | ||
end | ||
|
||
it 'upcases any methods provided' do | ||
cors.methods = %w[get post] | ||
expect(cors.to_headers).to eq({ | ||
'Access-Control-Allow-Origin' => 'example.com', | ||
'Access-Control-Allow-Methods' => 'GET, POST' | ||
}) | ||
end | ||
end | ||
|
||
context 'when headers have been provided' do | ||
it 'includes the Access-Control-Allow-Headers header' do | ||
cors.headers = %w[X-Custom Content-Type] | ||
expect(cors.to_headers).to eq({ | ||
'Access-Control-Allow-Origin' => 'example.com', | ||
'Access-Control-Allow-Methods' => '*', | ||
'Access-Control-Allow-Headers' => 'X-Custom, Content-Type' | ||
}) | ||
end | ||
end | ||
|
||
context 'when methods and headers have been provided' do | ||
it 'includes the Access-Control-Allow-Methods and Access-Control-Allow-Headers headers' do | ||
cors.methods = %w[GET POST] | ||
cors.headers = %w[X-Custom Content-Type] | ||
expect(cors.to_headers).to eq({ | ||
'Access-Control-Allow-Origin' => 'example.com', | ||
'Access-Control-Allow-Methods' => 'GET, POST', | ||
'Access-Control-Allow-Headers' => 'X-Custom, Content-Type' | ||
}) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters