aws-s3-instructure 0.6.2.1319222580

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/COPYING +19 -0
  2. data/INSTALL +55 -0
  3. data/README +545 -0
  4. data/Rakefile +300 -0
  5. data/bin/s3sh +6 -0
  6. data/bin/setup.rb +10 -0
  7. data/lib/aws/s3.rb +60 -0
  8. data/lib/aws/s3/acl.rb +636 -0
  9. data/lib/aws/s3/authentication.rb +266 -0
  10. data/lib/aws/s3/base.rb +240 -0
  11. data/lib/aws/s3/bittorrent.rb +58 -0
  12. data/lib/aws/s3/bucket.rb +319 -0
  13. data/lib/aws/s3/connection.rb +295 -0
  14. data/lib/aws/s3/error.rb +69 -0
  15. data/lib/aws/s3/exceptions.rb +133 -0
  16. data/lib/aws/s3/extensions.rb +356 -0
  17. data/lib/aws/s3/logging.rb +314 -0
  18. data/lib/aws/s3/object.rb +612 -0
  19. data/lib/aws/s3/owner.rb +44 -0
  20. data/lib/aws/s3/parsing.rb +99 -0
  21. data/lib/aws/s3/response.rb +180 -0
  22. data/lib/aws/s3/service.rb +51 -0
  23. data/lib/aws/s3/version.rb +12 -0
  24. data/support/faster-xml-simple/lib/faster_xml_simple.rb +187 -0
  25. data/support/faster-xml-simple/test/regression_test.rb +47 -0
  26. data/support/faster-xml-simple/test/test_helper.rb +17 -0
  27. data/support/faster-xml-simple/test/xml_simple_comparison_test.rb +46 -0
  28. data/support/rdoc/code_info.rb +211 -0
  29. data/test/acl_test.rb +254 -0
  30. data/test/authentication_test.rb +156 -0
  31. data/test/base_test.rb +136 -0
  32. data/test/bucket_test.rb +74 -0
  33. data/test/connection_test.rb +256 -0
  34. data/test/error_test.rb +70 -0
  35. data/test/extensions_test.rb +340 -0
  36. data/test/fixtures.rb +89 -0
  37. data/test/fixtures/buckets.yml +133 -0
  38. data/test/fixtures/errors.yml +34 -0
  39. data/test/fixtures/headers.yml +3 -0
  40. data/test/fixtures/logging.yml +15 -0
  41. data/test/fixtures/loglines.yml +5 -0
  42. data/test/fixtures/logs.yml +7 -0
  43. data/test/fixtures/policies.yml +16 -0
  44. data/test/logging_test.rb +89 -0
  45. data/test/mocks/fake_response.rb +26 -0
  46. data/test/object_test.rb +205 -0
  47. data/test/parsing_test.rb +66 -0
  48. data/test/remote/acl_test.rb +117 -0
  49. data/test/remote/bittorrent_test.rb +45 -0
  50. data/test/remote/bucket_test.rb +146 -0
  51. data/test/remote/logging_test.rb +82 -0
  52. data/test/remote/object_test.rb +371 -0
  53. data/test/remote/test_file.data +0 -0
  54. data/test/remote/test_helper.rb +33 -0
  55. data/test/response_test.rb +68 -0
  56. data/test/service_test.rb +23 -0
  57. data/test/test_helper.rb +110 -0
  58. metadata +198 -0
@@ -0,0 +1,156 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class HeaderAuthenticationTest < Test::Unit::TestCase
4
+ def test_encoded_canonical
5
+ signature = Authentication::Signature.new(request, key_id, secret)
6
+ assert_equal AmazonDocExampleData::Example1.canonical_string, signature.send(:canonical_string)
7
+ assert_equal AmazonDocExampleData::Example1.signature, signature.send(:encoded_canonical)
8
+ end
9
+
10
+ def test_authorization_header
11
+ header = Authentication::Header.new(request, key_id, secret)
12
+ assert_equal AmazonDocExampleData::Example1.canonical_string, header.send(:canonical_string)
13
+ assert_equal AmazonDocExampleData::Example1.authorization_header, header
14
+ end
15
+
16
+ private
17
+ def request; AmazonDocExampleData::Example1.request end
18
+ def key_id ; AmazonDocExampleData::Example1.access_key_id end
19
+ def secret ; AmazonDocExampleData::Example1.secret_access_key end
20
+ end
21
+
22
+ class QueryStringAuthenticationTest < Test::Unit::TestCase
23
+ def test_query_string
24
+ query_string = Authentication::QueryString.new(request, key_id, secret, :expires_in => 60)
25
+ assert_equal AmazonDocExampleData::Example3.canonical_string, query_string.send(:canonical_string)
26
+ assert_equal AmazonDocExampleData::Example3.query_string, query_string
27
+ end
28
+
29
+ def test_query_string_with_explicit_expiry
30
+ query_string = Authentication::QueryString.new(request, key_id, secret, :expires => expires)
31
+ assert_equal expires, query_string.send(:canonical_string).instance_variable_get(:@options)[:expires]
32
+ assert_equal AmazonDocExampleData::Example3.query_string, query_string
33
+ end
34
+
35
+ def test_expires_in_is_coerced_to_being_an_integer_in_case_it_is_a_special_integer_proxy
36
+ # References bug: http://rubyforge.org/tracker/index.php?func=detail&aid=17458&group_id=2409&atid=9356
37
+ integer_proxy = Class.new do
38
+ attr_reader :integer
39
+ def initialize(integer)
40
+ @integer = integer
41
+ end
42
+
43
+ def to_int
44
+ integer
45
+ end
46
+ end
47
+
48
+ actual_integer = 25
49
+ query_string = Authentication::QueryString.new(request, key_id, secret, :expires_in => integer_proxy.new(actual_integer))
50
+ assert_equal actual_integer, query_string.send(:expires_in)
51
+ end
52
+
53
+ private
54
+ def request; AmazonDocExampleData::Example3.request end
55
+ def key_id ; AmazonDocExampleData::Example3.access_key_id end
56
+ def secret ; AmazonDocExampleData::Example3.secret_access_key end
57
+ def expires; AmazonDocExampleData::Example3.expires end
58
+ end
59
+
60
+ class CanonicalStringTest < Test::Unit::TestCase
61
+ def setup
62
+ @request = Net::HTTP::Post.new('/test')
63
+ @canonical_string = Authentication::CanonicalString.new(@request)
64
+ end
65
+
66
+ def test_path_does_not_include_query_string
67
+ request = Net::HTTP::Get.new('/test/query/string?foo=bar&baz=quux')
68
+ assert_equal '/test/query/string', Authentication::CanonicalString.new(request).send(:path)
69
+
70
+ # Make sure things still work when there is *no* query string
71
+ request = Net::HTTP::Get.new('/')
72
+ assert_equal '/', Authentication::CanonicalString.new(request).send(:path)
73
+ request = Net::HTTP::Get.new('/foo/bar')
74
+ assert_equal '/foo/bar', Authentication::CanonicalString.new(request).send(:path)
75
+ end
76
+
77
+ # quotes in comments in these next few tests refer to
78
+ # http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html#ConstructingTheCanonicalizedResourceElement
79
+ def test_path_includes_significant_query_strings
80
+ # "If the request addresses a sub-resource, like ?versioning, ?location,
81
+ # ?acl, or ?torrent, or ?versionid append the sub-resource, its value if it
82
+ # has one, and the question mark. Note that in case of multiple
83
+ # sub-resources, sub-resources must be lexicographically sorted by
84
+ # sub-resource name and separated by '&'. e.g. ?acl&versionId=value."
85
+ significant_query_strings = [
86
+ ['/test/query/string?acl', '/test/query/string?acl'],
87
+ ['/test/query/string?acl&foo=bar', '/test/query/string?acl'],
88
+ ['/test/query/string?foo=bar&acl', '/test/query/string?acl'],
89
+ ['/test/query/string?acl=foo', '/test/query/string?acl=foo'],
90
+ ['/test/query/string?torrent=foo', '/test/query/string?torrent=foo'],
91
+ ['/test/query/string?logging=foo', '/test/query/string?logging=foo'],
92
+ ['/test/query/string?bar=baz&acl=foo', '/test/query/string?acl=foo'],
93
+ ['/test/query/string?acl=foo&torrent=bar', '/test/query/string?acl=foo&torrent=bar'],
94
+ ['/test/query/string?torrent=bar&acl=foo', '/test/query/string?acl=foo&torrent=bar']
95
+ ]
96
+
97
+ significant_query_strings.each do |uncleaned_path, expected_cleaned_path|
98
+ assert_equal expected_cleaned_path, Authentication::CanonicalString.new(Net::HTTP::Get.new(uncleaned_path)).send(:path)
99
+ end
100
+ end
101
+
102
+ def test_path_includes_significant_query_strings_from_options
103
+ assert_equal '/test/query/string?acl=foo', Authentication::CanonicalString.new(Net::HTTP::Get.new('/test/query/string'), :acl => 'foo').send(:path)
104
+ assert_equal '/test/query/string?acl&torrent=foo', Authentication::CanonicalString.new(Net::HTTP::Get.new('/test/query/string?acl'), :torrent => 'foo').send(:path)
105
+ end
106
+
107
+ def test_path_includes_significant_query_strings_with_unencoded_values
108
+ # "When signing you do not encode these values. However, when making the
109
+ # request, you must encode these parameter values."
110
+ assert_equal '/test/query/string?acl=foo bar', Authentication::CanonicalString.new(Net::HTTP::Get.new('/test/query/string?acl=foo+bar')).send(:path)
111
+ end
112
+
113
+ def test_path_recognizes_all_significant_query_strings
114
+ # "The list of sub-resources that must be included when constructing the
115
+ # CanonicalizedResource Element are: acl, location, logging, notification,
116
+ # partNumber, policy, requestPayment, torrent, uploadId, uploads,
117
+ # versionId, versioning, versions and website... If the request specifies
118
+ # query string parameters overriding the response header values... append
119
+ # the query string parameters, and its values... The query string
120
+ # parameters in a GET request include response-content-type,
121
+ # response-content-language, response-expires, response-cache-control,
122
+ # response-content-disposition, and response-content-encoding."
123
+ base_path = '/test/query/string'
124
+ [ 'acl', 'location', 'logging', 'notification', 'partNumber', 'policy',
125
+ 'requestPayment', 'torrent', 'uploadId', 'uploads', 'versionId',
126
+ 'versioning', 'versions', 'website', 'response-content-type',
127
+ 'response-content-language', 'response-expires',
128
+ 'response-cache-control', 'response-content-disposition',
129
+ 'response-content-encoding' ].each do |significant_parameter|
130
+ path = "#{base_path}?#{significant_parameter}"
131
+ assert_equal path, Authentication::CanonicalString.new(Net::HTTP::Get.new(path)).send(:path)
132
+ end
133
+ end
134
+
135
+ def test_default_headers_set
136
+ Authentication::CanonicalString.default_headers.each do |header|
137
+ assert @canonical_string.headers.include?(header)
138
+ end
139
+ end
140
+
141
+ def test_interesting_headers_are_copied_over
142
+ an_interesting_header = 'content-md5'
143
+ string_without_interesting_header = Authentication::CanonicalString.new(@request)
144
+ assert string_without_interesting_header.headers[an_interesting_header].empty?
145
+
146
+ # Add an interesting header
147
+ @request[an_interesting_header] = 'foo'
148
+ string_with_interesting_header = Authentication::CanonicalString.new(@request)
149
+ assert_equal 'foo', string_with_interesting_header.headers[an_interesting_header]
150
+ end
151
+
152
+ def test_canonical_string
153
+ request = AmazonDocExampleData::Example1.request
154
+ assert_equal AmazonDocExampleData::Example1.canonical_string, Authentication::CanonicalString.new(request)
155
+ end
156
+ end
data/test/base_test.rb ADDED
@@ -0,0 +1,136 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class BaseTest < Test::Unit::TestCase
4
+ def test_connection_established
5
+ assert_raises(NoConnectionEstablished) do
6
+ Base.connection
7
+ end
8
+
9
+ Base.establish_connection!(:access_key_id => '123', :secret_access_key => 'abc')
10
+ assert_kind_of Connection, Base.connection
11
+
12
+ instance = Base.new
13
+ assert_equal instance.send(:connection), Base.connection
14
+ assert_equal instance.send(:http), Base.connection.http
15
+ end
16
+
17
+ def test_respond_with
18
+ assert_equal Base::Response, Base.send(:response_class)
19
+ Base.send(:respond_with, Bucket::Response) do
20
+ assert_equal Bucket::Response, Base.send(:response_class)
21
+ end
22
+ assert_equal Base::Response, Base.send(:response_class)
23
+ end
24
+
25
+ def test_request_tries_again_when_encountering_an_internal_error
26
+ mock_connection_for(Bucket, :returns => [
27
+ # First request is an internal error
28
+ {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
29
+ # Second request is a success
30
+ {:body => Fixtures::Buckets.empty_bucket, :code => 200}
31
+ ])
32
+ bucket = nil # Block scope hack
33
+ assert_nothing_raised do
34
+ bucket = Bucket.find('marcel')
35
+ end
36
+ # Don't call objects 'cause we don't want to make another request
37
+ assert bucket.object_cache.empty?
38
+ end
39
+
40
+ def test_request_tries_up_to_three_times
41
+ mock_connection_for(Bucket, :returns => [
42
+ # First request is an internal error
43
+ {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
44
+ # Second request is also an internal error
45
+ {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
46
+ # Ditto third
47
+ {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
48
+ # Fourth works
49
+ {:body => Fixtures::Buckets.empty_bucket, :code => 200}
50
+ ])
51
+ bucket = nil # Block scope hack
52
+ assert_nothing_raised do
53
+ bucket = Bucket.find('marcel')
54
+ end
55
+ # Don't call objects 'cause we don't want to make another request
56
+ assert bucket.object_cache.empty?
57
+ end
58
+
59
+ def test_request_tries_again_three_times_and_gives_up
60
+ mock_connection_for(Bucket, :returns => [
61
+ # First request is an internal error
62
+ {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
63
+ # Second request is also an internal error
64
+ {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
65
+ # Ditto third
66
+ {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
67
+ # Ditto fourth
68
+ {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
69
+ ])
70
+ assert_raises(InternalError) do
71
+ Bucket.find('marcel')
72
+ end
73
+ end
74
+ end
75
+
76
+ class MultiConnectionsTest < Test::Unit::TestCase
77
+ class ClassToTestSettingCurrentBucket < Base
78
+ set_current_bucket_to 'foo'
79
+ end
80
+
81
+ def setup
82
+ Base.send(:connections).clear
83
+ end
84
+
85
+ def test_default_connection_options_are_used_for_subsequent_connections
86
+ assert !Base.connected?
87
+
88
+ assert_raises(MissingAccessKey) do
89
+ Base.establish_connection!
90
+ end
91
+
92
+ assert !Base.connected?
93
+
94
+ assert_raises(NoConnectionEstablished) do
95
+ Base.connection
96
+ end
97
+
98
+ assert_nothing_raised do
99
+ Base.establish_connection!(:access_key_id => '123', :secret_access_key => 'abc')
100
+ end
101
+
102
+ assert Base.connected?
103
+
104
+ assert_nothing_raised do
105
+ Base.connection
106
+ end
107
+
108
+ # All subclasses are currently using the default connection
109
+ assert_equal Base.connection, Bucket.connection
110
+
111
+ # No need to pass in the required options. The default connection will supply them
112
+ assert_nothing_raised do
113
+ Bucket.establish_connection!(:server => 'foo.s3.amazonaws.com')
114
+ end
115
+
116
+ assert Base.connection != Bucket.connection
117
+ assert_equal '123', Bucket.connection.access_key_id
118
+ assert_equal 'foo', Bucket.connection.subdomain
119
+ end
120
+
121
+ def test_current_bucket
122
+ Base.establish_connection!(:access_key_id => '123', :secret_access_key => 'abc')
123
+ assert_raises(CurrentBucketNotSpecified) do
124
+ Base.current_bucket
125
+ end
126
+
127
+ S3Object.establish_connection!(:server => 'foo-bucket.s3.amazonaws.com')
128
+ assert_nothing_raised do
129
+ assert_equal 'foo-bucket', S3Object.current_bucket
130
+ end
131
+ end
132
+
133
+ def test_setting_the_current_bucket
134
+ assert_equal 'foo', ClassToTestSettingCurrentBucket.current_bucket
135
+ end
136
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class BucketTest < Test::Unit::TestCase
4
+ def test_bucket_name_validation
5
+ valid_names = %w(123 joe step-one step_two step3 step_4 step-5 step.six)
6
+ invalid_names = ['12', 'jo', 'kevin spacey', 'larry@wall', '', 'a' * 256]
7
+ validate_name = Proc.new {|name| Bucket.send(:validate_name!, name)}
8
+ valid_names.each do |valid_name|
9
+ assert_nothing_raised { validate_name[valid_name] }
10
+ end
11
+
12
+ invalid_names.each do |invalid_name|
13
+ assert_raises(InvalidBucketName) { validate_name[invalid_name] }
14
+ end
15
+ end
16
+
17
+ def test_empty_bucket
18
+ mock_connection_for(Bucket, :returns => {:body => Fixtures::Buckets.empty_bucket, :code => 200})
19
+ bucket = Bucket.find('marcel_molina')
20
+ assert bucket.empty?
21
+ end
22
+
23
+ def test_bucket_with_one_file
24
+ mock_connection_for(Bucket, :returns => {:body => Fixtures::Buckets.bucket_with_one_key, :code => 200})
25
+ bucket = Bucket.find('marcel_molina')
26
+ assert !bucket.empty?
27
+ assert_equal 1, bucket.size
28
+ assert_equal %w(tongue_overload.jpg), bucket.objects.map {|object| object.key}
29
+ assert bucket['tongue_overload.jpg']
30
+ end
31
+
32
+ def test_bucket_with_more_than_one_file
33
+ mock_connection_for(Bucket, :returns => {:body => Fixtures::Buckets.bucket_with_more_than_one_key, :code => 200})
34
+ bucket = Bucket.find('marcel_molina')
35
+ assert !bucket.empty?
36
+ assert_equal 2, bucket.size
37
+ assert_equal %w(beluga_baby.jpg tongue_overload.jpg), bucket.objects.map {|object| object.key}.sort
38
+ assert bucket['tongue_overload.jpg']
39
+ end
40
+
41
+ def test_bucket_path
42
+ assert_equal '/bucket_name?max-keys=2', Bucket.send(:path, 'bucket_name', :max_keys => 2)
43
+ assert_equal '/bucket_name', Bucket.send(:path, 'bucket_name', {})
44
+ end
45
+
46
+ def test_should_not_be_truncated
47
+ mock_connection_for(Bucket, :returns => {:body => Fixtures::Buckets.bucket_with_more_than_one_key, :code => 200})
48
+ bucket = Bucket.find('marcel_molina')
49
+ assert !bucket.is_truncated
50
+ end
51
+
52
+ def test_should_be_truncated
53
+ mock_connection_for(Bucket, :returns => {:body => Fixtures::Buckets.truncated_bucket_with_more_than_one_key, :code => 200})
54
+ bucket = Bucket.find('marcel_molina')
55
+ assert bucket.is_truncated
56
+ end
57
+
58
+ def test_bucket_name_should_have_leading_slash_prepended_only_once_when_forcing_a_delete
59
+ # References bug: http://rubyforge.org/tracker/index.php?func=detail&aid=19158&group_id=2409&atid=9356
60
+ bucket_name = 'foo'
61
+ expected_bucket_path = "/#{bucket_name}"
62
+
63
+ mock_bucket = flexmock('Mock bucket') do |mock|
64
+ mock.should_receive(:delete_all).once
65
+ end
66
+ mock_response = flexmock('Mock delete response') do |mock|
67
+ mock.should_receive(:success?).once
68
+ end
69
+
70
+ flexmock(Bucket).should_receive(:find).with(bucket_name).once.and_return(mock_bucket)
71
+ flexmock(Base).should_receive(:delete).with(expected_bucket_path).once.and_return(mock_response)
72
+ Bucket.delete(bucket_name, :force => true)
73
+ end
74
+ end
@@ -0,0 +1,256 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ConnectionTest < Test::Unit::TestCase
4
+ attr_reader :keys
5
+ def setup
6
+ @keys = {:access_key_id => '123', :secret_access_key => 'abc'}.freeze
7
+ end
8
+
9
+ def test_creating_a_connection
10
+ connection = Connection.new(keys)
11
+ assert_kind_of Net::HTTP, connection.http
12
+ end
13
+
14
+ def test_use_ssl_option_is_set_in_connection
15
+ connection = Connection.new(keys.merge(:use_ssl => true))
16
+ assert connection.http.use_ssl?
17
+ end
18
+
19
+ def test_setting_port_to_443_implies_use_ssl
20
+ connection = Connection.new(keys.merge(:port => 443))
21
+ assert connection.http.use_ssl?
22
+ end
23
+
24
+ def test_protocol
25
+ connection = Connection.new(keys)
26
+ assert_equal 'http://', connection.protocol
27
+ connection = Connection.new(keys.merge(:use_ssl => true))
28
+ assert_equal 'https://', connection.protocol
29
+ end
30
+
31
+ def test_url_for_honors_use_ssl_option_if_it_is_false_even_if_connection_has_use_ssl_option_set
32
+ # References bug: http://rubyforge.org/tracker/index.php?func=detail&aid=17628&group_id=2409&atid=9356
33
+ connection = Connection.new(keys.merge(:use_ssl => true))
34
+ assert_match %r(^http://), connection.url_for('/pathdoesnotmatter', :authenticated => false, :use_ssl => false)
35
+ end
36
+
37
+ def test_connection_is_not_persistent_by_default
38
+ connection = Connection.new(keys)
39
+ assert !connection.persistent?
40
+
41
+ connection = Connection.new(keys.merge(:persistent => true))
42
+ assert connection.persistent?
43
+ end
44
+
45
+ def test_server_and_port_are_passed_onto_connection
46
+ connection = Connection.new(keys)
47
+ options = connection.instance_variable_get('@options')
48
+ assert_equal connection.http.address, options[:server]
49
+ assert_equal connection.http.port, options[:port]
50
+ end
51
+
52
+ def test_not_including_required_access_keys_raises
53
+ assert_raises(MissingAccessKey) do
54
+ Connection.new
55
+ end
56
+
57
+ assert_raises(MissingAccessKey) do
58
+ Connection.new(:access_key_id => '123')
59
+ end
60
+
61
+ assert_nothing_raised do
62
+ Connection.new(keys)
63
+ end
64
+ end
65
+
66
+ def test_access_keys_extracted
67
+ connection = Connection.new(keys)
68
+ assert_equal '123', connection.access_key_id
69
+ assert_equal 'abc', connection.secret_access_key
70
+ end
71
+
72
+ def test_request_method_class_lookup
73
+ connection = Connection.new(keys)
74
+ expectations = {
75
+ :get => Net::HTTP::Get, :post => Net::HTTP::Post,
76
+ :put => Net::HTTP::Put, :delete => Net::HTTP::Delete,
77
+ :head => Net::HTTP::Head
78
+ }
79
+
80
+ expectations.each do |verb, klass|
81
+ assert_equal klass, connection.send(:request_method, verb)
82
+ end
83
+ end
84
+
85
+ def test_url_for_uses_default_protocol_server_and_port
86
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :port => 80)
87
+ assert_match %r(^http://s3\.amazonaws\.com/foo\?), connection.url_for('/foo')
88
+
89
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :use_ssl => true, :port => 443)
90
+ assert_match %r(^https://s3\.amazonaws\.com/foo\?), connection.url_for('/foo')
91
+ end
92
+
93
+ def test_url_for_remembers_custom_protocol_server_and_port
94
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :server => 'example.org', :port => 555, :use_ssl => true)
95
+ assert_match %r(^https://example\.org:555/foo\?), connection.url_for('/foo')
96
+ end
97
+
98
+ def assert_url_has_query_parameter(url, key, value=:unspecified)
99
+ if value == :unspecified
100
+ assert_match /\?([^&]+&)*#{Regexp.escape(key)}(=|&|$)/, url
101
+ elsif value.nil?
102
+ assert_match /\?([^&]+&)*#{Regexp.escape(key)}=?(&|$)/, url
103
+ else
104
+ assert_match /\?([^&]+&)*#{Regexp.escape(key)}=#{Regexp.escape(CGI.escape(value.to_s))}/, url
105
+ end
106
+ end
107
+
108
+ def assert_url_lacks_query_parameter(url, key)
109
+ assert_no_match /\?([^&]+&)*#{Regexp.escape(key)}(=|&|$)/, url
110
+ end
111
+
112
+ def assert_url_authenticated(url)
113
+ assert_url_has_query_parameter(url, 'AWSAccessKeyId')
114
+ end
115
+
116
+ def assert_url_unauthenticated(url)
117
+ assert_url_lacks_query_parameter(url, 'AWSAccessKeyId')
118
+ end
119
+
120
+ def test_url_for_with_and_without_authenticated_urls
121
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :server => 'example.org')
122
+ authenticated = lambda {|url| url['?AWSAccessKeyId']}
123
+ assert_url_authenticated connection.url_for('/foo')
124
+ assert_url_authenticated connection.url_for('/foo', :authenticated => true)
125
+ assert_url_unauthenticated connection.url_for('/foo', :authenticated => false)
126
+ end
127
+
128
+ def test_url_for_with_request_parameter
129
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :server => 'example.org')
130
+ url = connection.url_for('/foo', 'response-content-disposition' => 'attachment; foo.txt')
131
+ assert_url_has_query_parameter url, 'response-content-disposition', 'attachment; foo.txt'
132
+ end
133
+
134
+ def test_url_for_with_unknown_request_parameter
135
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :server => 'example.org')
136
+ url = connection.url_for('/foo', 'random-parameter' => 'attachment; foo.txt')
137
+ assert_url_lacks_query_parameter url, 'random-parameter'
138
+ end
139
+
140
+ def test_url_for_with_authenticated_and_request_parameter
141
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :server => 'example.org')
142
+ url = connection.url_for('/foo', :authenticated => true, 'response-content-disposition' => 'attachment; foo.txt')
143
+ assert_url_has_query_parameter url, 'response-content-disposition', 'attachment; foo.txt'
144
+ assert_url_authenticated url
145
+ end
146
+
147
+ def test_connecting_through_a_proxy
148
+ connection = nil
149
+ assert_nothing_raised do
150
+ connection = Connection.new(keys.merge(:proxy => sample_proxy_settings))
151
+ end
152
+ assert connection.http.proxy?
153
+ end
154
+
155
+ def test_request_only_escapes_the_path_the_first_time_it_runs_and_not_subsequent_times
156
+ connection = Connection.new(@keys)
157
+ unescaped_path = 'path with spaces'
158
+ escaped_path = 'path%20with%20spaces'
159
+
160
+ flexmock(Connection).should_receive(:prepare_path).with(unescaped_path).once.and_return(escaped_path).ordered
161
+ flexmock(connection.http).should_receive(:request).and_raise(Errno::EPIPE).ordered
162
+ flexmock(connection.http).should_receive(:request).ordered
163
+ connection.request :put, unescaped_path
164
+ end
165
+
166
+ def test_if_request_has_no_body_then_the_content_length_is_set_to_zero
167
+ # References bug: http://rubyforge.org/tracker/index.php?func=detail&aid=13052&group_id=2409&atid=9356
168
+ connection = Connection.new(@keys)
169
+ flexmock(Net::HTTP::Put).new_instances.should_receive(:content_length=).once.with(0).ordered
170
+ flexmock(connection.http).should_receive(:request).once.ordered
171
+ connection.request :put, 'path does not matter'
172
+ end
173
+ end
174
+
175
+ class ConnectionOptionsTest < Test::Unit::TestCase
176
+
177
+ def setup
178
+ @options = generate_options(:server => 'example.org', :port => 555)
179
+ @default_options = generate_options
180
+ end
181
+
182
+ def test_server_extracted
183
+ assert_key_transfered(:server, 'example.org', @options)
184
+ end
185
+
186
+ def test_port_extracted
187
+ assert_key_transfered(:port, 555, @options)
188
+ end
189
+
190
+ def test_server_defaults_to_default_host
191
+ assert_equal DEFAULT_HOST, @default_options[:server]
192
+ end
193
+
194
+ def test_port_defaults_to_80_if_use_ssl_is_false
195
+ assert_equal 80, @default_options[:port]
196
+ end
197
+
198
+ def test_port_is_set_to_443_if_use_ssl_is_true
199
+ options = generate_options(:use_ssl => true)
200
+ assert_equal 443, options[:port]
201
+ end
202
+
203
+ def test_explicit_port_trumps_use_ssl
204
+ options = generate_options(:port => 555, :use_ssl => true)
205
+ assert_equal 555, options[:port]
206
+ end
207
+
208
+ def test_invalid_options_raise
209
+ assert_raises(InvalidConnectionOption) do
210
+ generate_options(:host => 'campfire.s3.amazonaws.com')
211
+ end
212
+ end
213
+
214
+ def test_not_specifying_all_required_proxy_settings_raises
215
+ assert_raises(ArgumentError) do
216
+ generate_options(:proxy => {})
217
+ end
218
+ end
219
+
220
+ def test_not_specifying_proxy_option_at_all_does_not_raise
221
+ assert_nothing_raised do
222
+ generate_options
223
+ end
224
+ end
225
+
226
+ def test_specifying_all_required_proxy_settings
227
+ assert_nothing_raised do
228
+ generate_options(:proxy => sample_proxy_settings)
229
+ end
230
+ end
231
+
232
+ def test_only_host_setting_is_required
233
+ assert_nothing_raised do
234
+ generate_options(:proxy => {:host => 'http://google.com'})
235
+ end
236
+ end
237
+
238
+ def test_proxy_settings_are_extracted
239
+ options = generate_options(:proxy => sample_proxy_settings)
240
+ assert_equal sample_proxy_settings.values.map {|value| value.to_s}.sort, options.proxy_settings.map {|value| value.to_s}.sort
241
+ end
242
+
243
+ def test_recognizing_that_the_settings_want_to_connect_through_a_proxy
244
+ options = generate_options(:proxy => sample_proxy_settings)
245
+ assert options.connecting_through_proxy?
246
+ end
247
+
248
+ private
249
+ def assert_key_transfered(key, value, options)
250
+ assert_equal value, options[key]
251
+ end
252
+
253
+ def generate_options(options = {})
254
+ Connection::Options.new(options)
255
+ end
256
+ end