Skip to content

Commit

Permalink
adds #original_uri method to all request drivers (#137)
Browse files Browse the repository at this point in the history
use #original_uri instead of #request_uri if any value returned from the former
added RubyMine .idea folder to .gitignore
  • Loading branch information
iMacTia authored and kjg committed Jan 27, 2017
1 parent b429eda commit 914a666
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 1 deletion.
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

0 comments on commit 914a666

Please sign in to comment.