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

Add support for proxy servers (e.g. Nginx) URI rewrites #137

Merged
merged 3 commits into from
Jan 27, 2017
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/doc
/.yardoc
gemfiles/*.lock
/.idea
2 changes: 1 addition & 1 deletion lib/api_auth/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def canonical_string(override_method = nil)
[request_method.upcase,
@request.content_type,
@request.content_md5,
parse_uri(@request.request_uri),
parse_uri(@request.original_uri || @request.request_uri),
@request.timestamp].join(',')
end

Expand Down
4 changes: 4 additions & 0 deletions lib/api_auth/request_drivers/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def content_md5
value.nil? ? '' : value
end

def original_uri
find_header(%w(X-ORIGINAL-URI X_ORIGINAL_URI HTTP_X_ORIGINAL_URI))
end

def request_uri
@request.request_uri
end
Expand Down
4 changes: 4 additions & 0 deletions lib/api_auth/request_drivers/curb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def content_md5
value.nil? ? '' : value
end

def original_uri
find_header(%w(X-ORIGINAL-URI X_ORIGINAL_URI HTTP_X_ORIGINAL_URI))
end

def request_uri
@request.url
end
Expand Down
4 changes: 4 additions & 0 deletions lib/api_auth/request_drivers/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def content_md5
value.nil? ? '' : value
end

def original_uri
find_header(%w(X-ORIGINAL-URI X_ORIGINAL_URI HTTP_X_ORIGINAL_URI))
end

def request_uri
query_string = @request.params.to_query
query_string = nil if query_string.empty?
Expand Down
4 changes: 4 additions & 0 deletions lib/api_auth/request_drivers/httpi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def content_md5
value.nil? ? '' : value
end

def original_uri
find_header(%w(X-ORIGINAL-URI X_ORIGINAL_URI HTTP_X_ORIGINAL_URI))
end

def request_uri
@request.url.request_uri
end
Expand Down
4 changes: 4 additions & 0 deletions lib/api_auth/request_drivers/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def content_md5
value.nil? ? '' : value
end

def original_uri
find_header(%w(X-ORIGINAL-URI X_ORIGINAL_URI HTTP_X_ORIGINAL_URI))
end

def request_uri
@request.path
end
Expand Down
4 changes: 4 additions & 0 deletions lib/api_auth/request_drivers/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def content_md5
value.nil? ? '' : value
end

def original_uri
find_header(%w(X-ORIGINAL-URI X_ORIGINAL_URI HTTP_X_ORIGINAL_URI))
end

def request_uri
@request.fullpath
end
Expand Down
4 changes: 4 additions & 0 deletions lib/api_auth/request_drivers/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def content_md5
value.nil? ? '' : value
end

def original_uri
find_header(%w(X-ORIGINAL-URI X_ORIGINAL_URI HTTP_X_ORIGINAL_URI))
end

def request_uri
@request.url
end
Expand Down
25 changes: 25 additions & 0 deletions spec/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,31 @@
end
end
end

context "when there's a proxy server (e.g. Nginx) with rewrite rules" do
let(:request) do
Faraday::Request.create('GET') do |req|
req.options = Faraday::RequestOptions.new(Faraday::FlatParamsEncoder)
req.params = Faraday::Utils::ParamsHash.new
req.url('/resource.xml?foo=bar&bar=foo')
req.headers = { 'X-Original-URI' => '/api/resource.xml?foo=bar&bar=foo' }
end
end
subject(:headers) { described_class.new(request) }
let(:driver) { headers.instance_variable_get('@request') }

before do
allow(driver).to receive(:content_type).and_return 'text/html'
allow(driver).to receive(:content_md5).and_return '12345'
allow(driver).to receive(:timestamp).and_return 'Mon, 23 Jan 1984 03:29:56 GMT'
end

context 'the driver uses the original_uri' do
it 'constructs the canonical_string with the original_uri' do
expect(headers.canonical_string).to eq 'GET,text/html,12345,/api/resource.xml?foo=bar&bar=foo,Mon, 23 Jan 1984 03:29:56 GMT'
end
end
end
end
end

Expand Down