isaacfeliu-aws-s3 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
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
+ Bucket.request_always_returns :body => Fixtures::Buckets.empty_bucket, :code => 200 do
19
+ bucket = Bucket.find('marcel_molina')
20
+ assert bucket.empty?
21
+ end
22
+ end
23
+
24
+ def test_bucket_with_one_file
25
+ Bucket.request_always_returns :body => Fixtures::Buckets.bucket_with_one_key, :code => 200 do
26
+ bucket = Bucket.find('marcel_molina')
27
+ assert !bucket.empty?
28
+ assert_equal 1, bucket.size
29
+ assert_equal %w(tongue_overload.jpg), bucket.objects.map {|object| object.key}
30
+ assert bucket['tongue_overload.jpg']
31
+ end
32
+ end
33
+
34
+ def test_bucket_with_more_than_one_file
35
+ Bucket.request_always_returns :body => Fixtures::Buckets.bucket_with_more_than_one_key, :code => 200 do
36
+ bucket = Bucket.find('marcel_molina')
37
+ assert !bucket.empty?
38
+ assert_equal 2, bucket.size
39
+ assert_equal %w(beluga_baby.jpg tongue_overload.jpg), bucket.objects.map {|object| object.key}.sort
40
+ assert bucket['tongue_overload.jpg']
41
+ end
42
+ end
43
+
44
+ def test_bucket_path
45
+ assert_equal '/bucket_name?max-keys=2', Bucket.send(:path, 'bucket_name', :max_keys => 2)
46
+ assert_equal '/bucket_name', Bucket.send(:path, 'bucket_name', {})
47
+ end
48
+ end
@@ -0,0 +1,190 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ConnectionTest < Test::Unit::TestCase
4
+ def setup
5
+ @keys = {:access_key_id => '123', :secret_access_key => 'abc'}
6
+ end
7
+
8
+ def test_creating_a_connection
9
+ connection = Connection.new(@keys)
10
+ assert_kind_of Net::HTTP, connection.http
11
+ end
12
+
13
+ def test_use_ssl_option_is_set_in_connection
14
+ connection = Connection.new(@keys.merge(:use_ssl => true))
15
+ assert connection.http.use_ssl?
16
+ end
17
+
18
+ def test_setting_port_to_443_implies_use_ssl
19
+ connection = Connection.new(@keys.merge(:port => 443))
20
+ assert connection.http.use_ssl?
21
+ end
22
+
23
+ def test_protocol
24
+ connection = Connection.new(@keys)
25
+ assert_equal 'http://', connection.protocol
26
+ connection = Connection.new(@keys.merge(:use_ssl => true))
27
+ assert_equal 'https://', connection.protocol
28
+ end
29
+
30
+ def test_connection_is_persistent_by_default
31
+ connection = Connection.new(@keys)
32
+ assert connection.persistent?
33
+
34
+ connection = Connection.new(@keys.merge(:persistent => false))
35
+ assert !connection.persistent?
36
+ end
37
+
38
+ def test_server_and_port_are_passed_onto_connection
39
+ connection = Connection.new(@keys)
40
+ options = connection.instance_variable_get('@options')
41
+ assert_equal connection.http.address, options[:server]
42
+ assert_equal connection.http.port, options[:port]
43
+ end
44
+
45
+ def test_not_including_required_access_keys_raises
46
+ assert_raises(MissingAccessKey) do
47
+ Connection.new
48
+ end
49
+
50
+ assert_raises(MissingAccessKey) do
51
+ Connection.new(:access_key_id => '123')
52
+ end
53
+
54
+ assert_nothing_raised do
55
+ Connection.new(@keys)
56
+ end
57
+ end
58
+
59
+ def test_access_keys_extracted
60
+ connection = Connection.new(@keys)
61
+ assert_equal '123', connection.access_key_id
62
+ assert_equal 'abc', connection.secret_access_key
63
+ end
64
+
65
+ def test_request_method_class_lookup
66
+ c = Connection.new(@keys)
67
+ expectations = {
68
+ :get => Net::HTTP::Get, :post => Net::HTTP::Post,
69
+ :put => Net::HTTP::Put, :delete => Net::HTTP::Delete,
70
+ :head => Net::HTTP::Head
71
+ }
72
+
73
+ expectations.each do |verb, klass|
74
+ assert_equal klass, c.send(:request_method, verb)
75
+ end
76
+ end
77
+
78
+ def test_url_for_uses_default_protocol_server_and_port
79
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :port => 80)
80
+ assert_match %r(^http://s3\.amazonaws\.com/foo\?), connection.url_for('/foo')
81
+
82
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :use_ssl => true, :port => 443)
83
+ assert_match %r(^https://s3\.amazonaws\.com/foo\?), connection.url_for('/foo')
84
+ end
85
+
86
+ def test_url_for_remembers_custom_protocol_server_and_port
87
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :server => 'example.org', :port => 555, :use_ssl => true)
88
+ assert_match %r(^https://example\.org:555/foo\?), connection.url_for('/foo')
89
+ end
90
+
91
+ def test_url_for_with_and_without_authenticated_urls
92
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :server => 'example.org')
93
+ authenticated = lambda {|url| url['?AWSAccessKeyId']}
94
+ assert authenticated[connection.url_for('/foo')]
95
+ assert authenticated[connection.url_for('/foo', :authenticated => true)]
96
+ assert !authenticated[connection.url_for('/foo', :authenticated => false)]
97
+ end
98
+
99
+ def test_connecting_through_a_proxy
100
+ connection = nil
101
+ assert_nothing_raised do
102
+ connection = Connection.new(@keys.merge(:proxy => sample_proxy_settings))
103
+ end
104
+ assert connection.http.proxy?
105
+ end
106
+ end
107
+
108
+ class ConnectionOptionsTest < Test::Unit::TestCase
109
+
110
+ def setup
111
+ @options = generate_options(:server => 'example.org', :port => 555)
112
+ @default_options = generate_options
113
+ end
114
+
115
+ def test_server_extracted
116
+ assert_key_transfered(:server, 'example.org', @options)
117
+ end
118
+
119
+ def test_port_extracted
120
+ assert_key_transfered(:port, 555, @options)
121
+ end
122
+
123
+ def test_server_defaults_to_default_host
124
+ assert_equal DEFAULT_HOST, @default_options[:server]
125
+ end
126
+
127
+ def test_port_defaults_to_80_if_use_ssl_is_false
128
+ assert_equal 80, @default_options[:port]
129
+ end
130
+
131
+ def test_port_is_set_to_443_if_use_ssl_is_true
132
+ options = generate_options(:use_ssl => true)
133
+ assert_equal 443, options[:port]
134
+ end
135
+
136
+ def test_explicit_port_trumps_use_ssl
137
+ options = generate_options(:port => 555, :use_ssl => true)
138
+ assert_equal 555, options[:port]
139
+ end
140
+
141
+ def test_invalid_options_raise
142
+ assert_raises(InvalidConnectionOption) do
143
+ generate_options(:host => 'campfire.s3.amazonaws.com')
144
+ end
145
+ end
146
+
147
+ def test_not_specifying_all_required_proxy_settings_raises
148
+ assert_raises(ArgumentError) do
149
+ generate_options(:proxy => {})
150
+ end
151
+ end
152
+
153
+ def test_not_specifying_proxy_option_at_all_does_not_raise
154
+ assert_nothing_raised do
155
+ generate_options
156
+ end
157
+ end
158
+
159
+ def test_specifying_all_required_proxy_settings
160
+ assert_nothing_raised do
161
+ generate_options(:proxy => sample_proxy_settings)
162
+ end
163
+ end
164
+
165
+ def test_only_host_setting_is_required
166
+ assert_nothing_raised do
167
+ generate_options(:proxy => {:host => 'http://google.com'})
168
+ end
169
+ end
170
+
171
+ def test_proxy_settings_are_extracted
172
+ options = generate_options(:proxy => sample_proxy_settings)
173
+ assert_equal sample_proxy_settings.values.map {|value| value.to_s}.sort, options.proxy_settings.map {|value| value.to_s}.sort
174
+ end
175
+
176
+ def test_recognizing_that_the_settings_want_to_connect_through_a_proxy
177
+ options = generate_options(:proxy => sample_proxy_settings)
178
+ assert options.connecting_through_proxy?
179
+ end
180
+
181
+ private
182
+ def assert_key_transfered(key, value, options)
183
+ assert_equal value, options[key]
184
+ assert !options.instance_variable_get('@options').has_key?(key)
185
+ end
186
+
187
+ def generate_options(options = {})
188
+ Connection::Options.new(options)
189
+ end
190
+ end
@@ -0,0 +1,75 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ErrorTest < Test::Unit::TestCase
4
+ def setup
5
+ @container = AWS::S3
6
+ @error = Error.new(Parsing::XmlParser.new(Fixtures::Errors.access_denied))
7
+ end
8
+
9
+ def teardown
10
+ @container.send(:remove_const, :NotImplemented) if @container.const_defined?(:NotImplemented)
11
+ end
12
+
13
+ def test_error_class_is_automatically_generated
14
+ assert !@container.const_defined?('NotImplemented')
15
+ error = Error.new(Parsing::XmlParser.new(Fixtures::Errors.not_implemented))
16
+ assert @container.const_defined?('NotImplemented')
17
+ end
18
+
19
+ def test_error_contains_attributes
20
+ assert_equal 'Access Denied', @error.message
21
+ end
22
+
23
+ def test_error_is_raisable_as_exception
24
+ assert_raises(@container::AccessDenied) do
25
+ @error.raise
26
+ end
27
+ end
28
+
29
+ def test_error_message_is_passed_along_to_exception
30
+ @error.raise
31
+ rescue @container::AccessDenied => e
32
+ assert_equal 'Access Denied', e.message
33
+ end
34
+
35
+ def test_response_is_passed_along_to_exception
36
+ response = Error::Response.new(FakeResponse.new(:code => 409, :body => Fixtures::Errors.access_denied))
37
+ response.error.raise
38
+ rescue @container::ResponseError => e
39
+ assert e.response
40
+ assert_kind_of Error::Response, e.response
41
+ assert_equal response.error, e.response.error
42
+ end
43
+
44
+ def test_exception_class_clash
45
+ assert !@container.const_defined?(:NotImplemented)
46
+ # Create a class that does not inherit from exception that has the same name as the class
47
+ # the Error instance is about to attempt to find or create
48
+ @container.const_set(:NotImplemented, Class.new)
49
+ assert @container.const_defined?(:NotImplemented)
50
+
51
+ assert_raises(ExceptionClassClash) do
52
+ Error.new(Parsing::XmlParser.new(Fixtures::Errors.not_implemented))
53
+ end
54
+ end
55
+
56
+ def test_error_response_handles_attributes_with_no_value
57
+ Bucket.in_test_mode do
58
+ Bucket.request_returns :body => Fixtures::Errors.error_with_no_message, :code => 500
59
+
60
+ begin
61
+ Bucket.create('foo', 'invalid-argument' => 'bad juju')
62
+ rescue ResponseError => error
63
+ end
64
+
65
+ assert_nothing_raised do
66
+ error.response.error.message
67
+ end
68
+ assert_nil error.response.error.message
69
+
70
+ assert_raises(NoMethodError) do
71
+ error.response.error.non_existant_method
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,331 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class HashExtensionsTest < Test::Unit::TestCase
4
+ def test_to_query_string
5
+ # Because hashes aren't ordered, I'm mostly testing against hashes with just one key
6
+ symbol_keys = {:one => 1}
7
+ string_keys = {'one' => 1}
8
+ expected = '?one=1'
9
+ [symbol_keys, string_keys].each do |hash|
10
+ assert_equal expected, hash.to_query_string
11
+ end
12
+ end
13
+
14
+ def test_empty_hash_returns_no_query_string
15
+ assert_equal '', {}.to_query_string
16
+ end
17
+
18
+ def test_include_question_mark
19
+ hash = {:one => 1}
20
+ assert_equal '?one=1', hash.to_query_string
21
+ assert_equal 'one=1', hash.to_query_string(false)
22
+ end
23
+
24
+ def test_elements_joined_by_ampersand
25
+ hash = {:one => 1, :two => 2}
26
+ qs = hash.to_query_string
27
+ assert qs['one=1&two=2'] || qs['two=2&one=1']
28
+ end
29
+
30
+ def test_normalized_options
31
+ expectations = [
32
+ [{:foo_bar => 1}, {'foo-bar' => '1'}],
33
+ [{'foo_bar' => 1}, {'foo-bar' => '1'}],
34
+ [{'foo-bar' => 1}, {'foo-bar' => '1'}],
35
+ [{}, {}]
36
+ ]
37
+
38
+ expectations.each do |(before, after)|
39
+ assert_equal after, before.to_normalized_options
40
+ end
41
+ end
42
+ end
43
+
44
+ class StringExtensionsTest < Test::Unit::TestCase
45
+ def test_previous
46
+ expectations = {'abc' => 'abb', '123' => '122', '1' => '0'}
47
+ expectations.each do |before, after|
48
+ assert_equal after, before.previous
49
+ end
50
+ end
51
+
52
+ def test_to_header
53
+ transformations = {
54
+ 'foo' => 'foo',
55
+ :foo => 'foo',
56
+ 'foo-bar' => 'foo-bar',
57
+ 'foo_bar' => 'foo-bar',
58
+ :foo_bar => 'foo-bar',
59
+ 'Foo-Bar' => 'foo-bar',
60
+ 'Foo_Bar' => 'foo-bar'
61
+ }
62
+
63
+ transformations.each do |before, after|
64
+ assert_equal after, before.to_header
65
+ end
66
+ end
67
+
68
+ def test_utf8?
69
+ assert !"318597/620065/GTL_75\24300_A600_A610.zip".utf8?
70
+ assert "318597/620065/GTL_75£00_A600_A610.zip".utf8?
71
+ end
72
+
73
+ def test_remove_extended
74
+ assert "318597/620065/GTL_75\24300_A600_A610.zip".remove_extended.utf8?
75
+ assert "318597/620065/GTL_75£00_A600_A610.zip".remove_extended.utf8?
76
+ end
77
+ end
78
+
79
+ class CoercibleStringTest < Test::Unit::TestCase
80
+
81
+ def test_coerce
82
+ coercions = [
83
+ ['1', 1],
84
+ ['false', false],
85
+ ['true', true],
86
+ ['2006-10-29T23:14:47.000Z', Time.parse('2006-10-29T23:14:47.000Z')],
87
+ ['Hello!', 'Hello!'],
88
+ ['false23', 'false23'],
89
+ ['03 1-2-3-Apple-Tree.mp3', '03 1-2-3-Apple-Tree.mp3'],
90
+ ['0815', '0815'] # This number isn't coerced because the leading zero would be lost
91
+ ]
92
+
93
+ coercions.each do |before, after|
94
+ assert_nothing_raised do
95
+ assert_equal after, CoercibleString.coerce(before)
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ class KerneltExtensionsTest < Test::Unit::TestCase
102
+ class Foo
103
+ def foo
104
+ __method__
105
+ end
106
+
107
+ def bar
108
+ foo
109
+ end
110
+
111
+ def baz
112
+ bar
113
+ end
114
+ end
115
+
116
+ class Bar
117
+ def foo
118
+ calling_method
119
+ end
120
+
121
+ def bar
122
+ calling_method
123
+ end
124
+
125
+ def calling_method
126
+ __method__(1)
127
+ end
128
+ end
129
+
130
+ def test___method___works_regardless_of_nesting
131
+ f = Foo.new
132
+ [:foo, :bar, :baz].each do |method|
133
+ assert_equal 'foo', f.send(method)
134
+ end
135
+ end
136
+
137
+ def test___method___depth
138
+ b = Bar.new
139
+ assert_equal 'foo', b.foo
140
+ assert_equal 'bar', b.bar
141
+ end
142
+ end
143
+
144
+ class ModuleExtensionsTest < Test::Unit::TestCase
145
+ class Foo
146
+ def foo(reload = false)
147
+ memoize(reload) do
148
+ Time.now
149
+ end
150
+ end
151
+
152
+ def bar(reload = false)
153
+ memoize(reload, :baz) do
154
+ Time.now
155
+ end
156
+ end
157
+
158
+ def quux
159
+ Time.now
160
+ end
161
+ memoized :quux
162
+ end
163
+
164
+ def setup
165
+ @instance = Foo.new
166
+ end
167
+
168
+ def test_memoize
169
+ assert !@instance.instance_variables.include?('@foo')
170
+ cached_result = @instance.foo
171
+ assert_equal cached_result, @instance.foo
172
+ assert @instance.instance_variables.include?('@foo')
173
+ assert_equal cached_result, @instance.send(:instance_variable_get, :@foo)
174
+ assert_not_equal cached_result, new_cache = @instance.foo(:reload)
175
+ assert_equal new_cache, @instance.foo
176
+ assert_equal new_cache, @instance.send(:instance_variable_get, :@foo)
177
+ end
178
+
179
+ def test_customizing_memoize_storage
180
+ assert !@instance.instance_variables.include?('@bar')
181
+ assert !@instance.instance_variables.include?('@baz')
182
+ cached_result = @instance.bar
183
+ assert !@instance.instance_variables.include?('@bar')
184
+ assert @instance.instance_variables.include?('@baz')
185
+ assert_equal cached_result, @instance.bar
186
+ assert_equal cached_result, @instance.send(:instance_variable_get, :@baz)
187
+ assert_nil @instance.send(:instance_variable_get, :@bar)
188
+ end
189
+
190
+ def test_memoized
191
+ assert !@instance.instance_variables.include?('@quux')
192
+ cached_result = @instance.quux
193
+ assert_equal cached_result, @instance.quux
194
+ assert @instance.instance_variables.include?('@quux')
195
+ assert_equal cached_result, @instance.send(:instance_variable_get, :@quux)
196
+ assert_not_equal cached_result, new_cache = @instance.quux(:reload)
197
+ assert_equal new_cache, @instance.quux
198
+ assert_equal new_cache, @instance.send(:instance_variable_get, :@quux)
199
+ end
200
+
201
+ def test_constant_setting
202
+ some_module = Module.new
203
+ assert !some_module.const_defined?(:FOO)
204
+ assert_nothing_raised do
205
+ some_module.constant :FOO, 'bar'
206
+ end
207
+
208
+ assert some_module.const_defined?(:FOO)
209
+ assert_nothing_raised do
210
+ some_module::FOO
211
+ some_module.foo
212
+ end
213
+ assert_equal 'bar', some_module::FOO
214
+ assert_equal 'bar', some_module.foo
215
+
216
+ assert_nothing_raised do
217
+ some_module.constant :FOO, 'baz'
218
+ end
219
+
220
+ assert_equal 'bar', some_module::FOO
221
+ assert_equal 'bar', some_module.foo
222
+ end
223
+ end
224
+
225
+ class AttributeProxyTest < Test::Unit::TestCase
226
+ class BlindProxyUsingDefaultAttributesHash
227
+ include SelectiveAttributeProxy
228
+ proxy_to :exlusively => false
229
+ end
230
+
231
+ class BlindProxyUsingCustomAttributeHash
232
+ include SelectiveAttributeProxy
233
+ proxy_to :settings
234
+ end
235
+
236
+ class ProxyUsingPassedInAttributeHash
237
+ include SelectiveAttributeProxy
238
+
239
+ def initialize(attributes = {})
240
+ @attributes = attributes
241
+ end
242
+ end
243
+
244
+ class RestrictedProxy
245
+ include SelectiveAttributeProxy
246
+
247
+ private
248
+ def proxiable_attribute?(name)
249
+ %w(foo bar baz).include?(name)
250
+ end
251
+ end
252
+
253
+ class NonExclusiveProxy
254
+ include SelectiveAttributeProxy
255
+ proxy_to :settings, :exclusively => false
256
+ end
257
+
258
+ def test_using_all_defaults
259
+ b = BlindProxyUsingDefaultAttributesHash.new
260
+ assert_nothing_raised do
261
+ b.foo = 'bar'
262
+ end
263
+
264
+ assert_nothing_raised do
265
+ b.foo
266
+ end
267
+
268
+ assert_equal 'bar', b.foo
269
+ end
270
+
271
+ def test_storage_is_autovivified
272
+ b = BlindProxyUsingDefaultAttributesHash.new
273
+ assert_nothing_raised do
274
+ b.send(:attributes)['foo'] = 'bar'
275
+ end
276
+
277
+ assert_nothing_raised do
278
+ b.foo
279
+ end
280
+
281
+ assert_equal 'bar', b.foo
282
+ end
283
+
284
+ def test_limiting_which_attributes_are_proxiable
285
+ r = RestrictedProxy.new
286
+ assert_nothing_raised do
287
+ r.foo = 'bar'
288
+ end
289
+
290
+ assert_nothing_raised do
291
+ r.foo
292
+ end
293
+
294
+ assert_equal 'bar', r.foo
295
+
296
+ assert_raises(NoMethodError) do
297
+ r.quux = 'foo'
298
+ end
299
+
300
+ assert_raises(NoMethodError) do
301
+ r.quux
302
+ end
303
+ end
304
+
305
+ def test_proxying_is_exclusive_by_default
306
+ p = ProxyUsingPassedInAttributeHash.new('foo' => 'bar')
307
+ assert_nothing_raised do
308
+ p.foo
309
+ p.foo = 'baz'
310
+ end
311
+
312
+ assert_equal 'baz', p.foo
313
+
314
+ assert_raises(NoMethodError) do
315
+ p.quux
316
+ end
317
+ end
318
+
319
+ def test_setting_the_proxy_as_non_exclusive
320
+ n = NonExclusiveProxy.new
321
+ assert_nothing_raised do
322
+ n.foo = 'baz'
323
+ end
324
+
325
+ assert_nothing_raised do
326
+ n.foo
327
+ end
328
+
329
+ assert_equal 'baz', n.foo
330
+ end
331
+ end