Permalink
Browse files

Merge pull request #2 from github/url_for-with-parameters

Url for with parameters
  • Loading branch information...
2 parents 3cab307 + 2dcee48 commit 1edde7cd11e8e6099070799bb5288c84d1d3a049 @technoweenie technoweenie committed Jan 4, 2013
Showing with 49 additions and 6 deletions.
  1. +8 −0 lib/aws/s3.rb
  2. +1 −1 lib/aws/s3/extensions.rb
  3. +5 −1 lib/aws/s3/object.rb
  4. +6 −0 test/connection_test.rb
  5. +2 −2 test/extensions_test.rb
  6. +27 −2 test/object_test.rb
View
@@ -36,6 +36,14 @@ def self.escape_uri(path)
URI.escape(path.to_s, UNSAFE_URI)
end
+ def self.escape_uri_component(path)
+ escaped = escape_uri(path)
+ escaped.gsub!(/=/, '%3D')
+ escaped.gsub!(/&/, '%26')
+ escaped.gsub!(/;/, '%3B')
+ escaped
+ end
+
Base.class_eval do
include AWS::S3::Connection::Management
end
View
@@ -7,7 +7,7 @@ def to_query_string(include_question_mark = true)
unless empty?
query_string << '?' if include_question_mark
query_string << inject([]) do |params, (key, value)|
- params << "#{key}=#{AWS::S3.escape_uri(value)}"
+ params << "#{key}=#{AWS::S3.escape_uri_component(value)}"
end.join('&')
end
query_string
View
@@ -297,7 +297,11 @@ def path!(bucket, name, options = {}) #:nodoc:
options.replace(bucket)
bucket = nil
end
- '/' << File.join(bucket_name(bucket), name)
+ path = '/' << File.join(bucket_name(bucket), name)
+ if (query = options[:query]).respond_to?(:to_query_string)
+ path << query.to_query_string
+ end
+ path
end
private
View
@@ -102,6 +102,12 @@ 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_url_for_with_canonical_query_params
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :server => 'example.org')
+ dispositioned = lambda {|url| url['?response-content-disposition=a']}
+ assert dispositioned[connection.url_for("/foo?response-content-disposition=a")]
+ end
def test_connecting_through_a_proxy
connection = nil
View
@@ -29,8 +29,8 @@ def test_elements_joined_by_ampersand
end
def test_escape_values
- hash = {:one => '5+ 1'}
- assert_equal '?one=5%2B%201', hash.to_query_string
+ hash = {:one => '5+ 1=6&'}
+ assert_equal '?one=5%2B%201%3D6%26', hash.to_query_string
end
def test_normalized_options
View
@@ -5,7 +5,7 @@ def setup
bucket = Bucket.new(Parsing::XmlParser.new(Fixtures::Buckets.bucket_with_one_key))
@object = bucket.objects.first
end
-
+
def test_header_settings_reader_and_writer
headers = {'content-type' => 'text/plain'}
mock_connection_for(S3Object, :returns => {:headers => headers})
@@ -57,6 +57,31 @@ def test_content_type_inference
def test_object_has_owner
assert_kind_of Owner, @object.owner
end
+
+ def test_url_is_authenticated
+ conn = Connection.new :access_key_id => '123', :secret_access_key => 'abc'
+
+ begin
+ AWS::S3::Base.connections['AWS::S3::Base'] = conn
+ authenticated = lambda {|url| url['?AWSAccessKeyId']}
+ assert authenticated[@object.url]
+ ensure
+ AWS::S3::Base.connections.clear
+ end
+ end
+
+ def test_url_with_custom_query
+ conn = Connection.new :access_key_id => '123', :secret_access_key => 'abc'
+
+ begin
+ AWS::S3::Base.connections['AWS::S3::Base'] = conn
+ assert_match 'response-content-disposition=attachment%3B%20filename%3Dfoo.txt',
+ @object.url(:query => {
+ 'response-content-disposition' => 'attachment; filename=foo.txt'})
+ ensure
+ AWS::S3::Base.connections.clear
+ end
+ end
def test_owner_attributes_are_accessible
owner = @object.owner
@@ -217,4 +242,4 @@ def test_value_is_set_to_response_body
def test_response_is_accessible_from_value_object
assert_equal @response, @value.response
end
-end
+end

0 comments on commit 1edde7c

Please sign in to comment.