Permalink
Browse files

Added support for HEAD signing

  • Loading branch information...
1 parent 5e6f375 commit 1017dec9bcf6cad3e09056657c4f9c8e72e382ac Arthur Taylor committed Aug 15, 2011
Showing with 75 additions and 5 deletions.
  1. +3 −2 lib/aws/s3/connection.rb
  2. +24 −2 test/authentication_test.rb
  3. +5 −0 test/connection_test.rb
  4. +43 −1 test/test_helper.rb
View
@@ -58,10 +58,11 @@ def request(verb, path, headers = {}, body = nil, attempts = 0, &block)
def url_for(path, options = {})
authenticate = options.delete(:authenticated)
+ verb = options.delete(:method) || :get
# Default to true unless explicitly false
authenticate = true if authenticate.nil?
path = self.class.prepare_path(path)
- request = request_method(:get).new(path, {})
+ request = request_method(verb).new(path, {})
query_string = query_string_authentication(request, options)
returning "#{protocol(options)}#{http.address}#{port_string}#{path}" do |url|
url << "?#{query_string}" if authenticate
@@ -275,4 +276,4 @@ def validate(options)
end
end
end
-end
+end
@@ -31,7 +31,7 @@ def test_query_string_with_explicit_expiry
assert_equal expires, query_string.send(:canonical_string).instance_variable_get(:@options)[:expires]
assert_equal AmazonDocExampleData::Example3.query_string, query_string
end
-
+
def test_expires_in_is_coerced_to_being_an_integer_in_case_it_is_a_special_integer_proxy
# References bug: http://rubyforge.org/tracker/index.php?func=detail&aid=17458&group_id=2409&atid=9356
integer_proxy = Class.new do
@@ -57,6 +57,28 @@ def secret ; AmazonDocExampleData::Example3.secret_access_key end
def expires; AmazonDocExampleData::Example3.expires end
end
+class HeadMethodTest < Test::Unit::TestCase
+
+ def test_authorization_header
+ header = Authentication::Header.new(request, key_id, secret)
+ assert_equal ExampleData::Example1.canonical_string_header, header.send(:canonical_string)
+ assert_equal ExampleData::Example1.authorization_header, header
+ end
+
+ def test_query_string
+ query_string = Authentication::QueryString.new(request, key_id, secret, :expires_in => 60)
+ assert_equal ExampleData::Example1.canonical_string_query, query_string.send(:canonical_string)
+ assert_equal ExampleData::Example1.query_string, query_string
+ end
+
+ private
+ def request; ExampleData::Example1.request end
+ def key_id ; ExampleData::Example1.access_key_id end
+ def secret ; ExampleData::Example1.secret_access_key end
+ def expires; ExampleData::Example1.expires end
+
+end
+
class CanonicalStringTest < Test::Unit::TestCase
def setup
@request = Net::HTTP::Post.new('/test')
@@ -111,4 +133,4 @@ def test_canonical_string
request = AmazonDocExampleData::Example1.request
assert_equal AmazonDocExampleData::Example1.canonical_string, Authentication::CanonicalString.new(request)
end
-end
+end
View
@@ -102,6 +102,11 @@ def test_url_for_with_and_without_authenticated_urls
assert authenticated[connection.url_for('/foo', :authenticated => true)]
assert !authenticated[connection.url_for('/foo', :authenticated => false)]
end
+
+ def test_signing_method_override
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :server => 'example.org')
+ assert connection.url_for('/foo') != connection.url_for('/foo', :method => :head)
+ end
def test_connecting_through_a_proxy
connection = nil
View
@@ -11,6 +11,48 @@
require_library_or_gem 'flexmock/test_unit'
+module ExampleData
+ module Example1
+ module_function
+
+ def request
+ request = Net::HTTP::Head.new('/quotes/nelson')
+ request['Content-Type'] = 'text/html'
+ request['Date'] = 'Thu, 17 Nov 2005 18:49:58 GMT'
+ request['X-Amz-Magic'] = 'abracadabra'
+ request
+ end
+
+ def canonical_string_query
+ "HEAD\n\ntext/html\n1132253458\nx-amz-magic:abracadabra\n/quotes/nelson"
+ end
+
+ def canonical_string_header
+ "HEAD\n\ntext/html\nThu, 17 Nov 2005 18:49:58 GMT\nx-amz-magic:abracadabra\n/quotes/nelson"
+ end
+
+ def access_key_id
+ '44CF9590006BF252F707'
+ end
+
+ def secret_access_key
+ 'OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV'
+ end
+
+ def signature
+ 'jZNOcbfWmD/A/f3hSvVzXZjM2HU='
+ end
+
+ def query_string
+ 'AWSAccessKeyId=44CF9590006BF252F707&Expires=1132253458&Signature=mfm9VGIfnfKc20fYJAaYSuYXPpk%3D'
+ end
+
+ def authorization_header
+ 'AWS 44CF9590006BF252F707:hDwh8uo1zH9XTfSQKiyw4j9QyXs='
+ end
+ end
+end
+
# Data copied from http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAuthentication.html
module AmazonDocExampleData
module Example1
@@ -107,4 +149,4 @@ def mock_connection_for(klass, options = {})
flexmock(klass).should_receive(:connection).and_return(connection)
end
-end
+end

0 comments on commit 1017dec

Please sign in to comment.