le88-aws-s3 0.6.3.1310482014

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 +334 -0
  5. data/bin/s3sh +6 -0
  6. data/bin/setup.rb +10 -0
  7. data/lib/aws/s3/acl.rb +636 -0
  8. data/lib/aws/s3/authentication.rb +221 -0
  9. data/lib/aws/s3/base.rb +240 -0
  10. data/lib/aws/s3/bittorrent.rb +58 -0
  11. data/lib/aws/s3/bucket.rb +319 -0
  12. data/lib/aws/s3/connection.rb +278 -0
  13. data/lib/aws/s3/error.rb +69 -0
  14. data/lib/aws/s3/exceptions.rb +133 -0
  15. data/lib/aws/s3/extensions.rb +340 -0
  16. data/lib/aws/s3/logging.rb +314 -0
  17. data/lib/aws/s3/object.rb +612 -0
  18. data/lib/aws/s3/owner.rb +44 -0
  19. data/lib/aws/s3/parsing.rb +99 -0
  20. data/lib/aws/s3/response.rb +180 -0
  21. data/lib/aws/s3/service.rb +51 -0
  22. data/lib/aws/s3/version.rb +12 -0
  23. data/lib/aws/s3.rb +60 -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 +114 -0
  31. data/test/base_test.rb +136 -0
  32. data/test/bucket_test.rb +74 -0
  33. data/test/connection_test.rb +215 -0
  34. data/test/error_test.rb +70 -0
  35. data/test/extensions_test.rb +340 -0
  36. data/test/fixtures/buckets.yml +133 -0
  37. data/test/fixtures/errors.yml +34 -0
  38. data/test/fixtures/headers.yml +3 -0
  39. data/test/fixtures/logging.yml +15 -0
  40. data/test/fixtures/loglines.yml +5 -0
  41. data/test/fixtures/logs.yml +7 -0
  42. data/test/fixtures/policies.yml +16 -0
  43. data/test/fixtures.rb +89 -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 +179 -0
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,215 @@
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 test_url_for_with_and_without_authenticated_urls
99
+ connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :server => 'example.org')
100
+ authenticated = lambda {|url| url['?AWSAccessKeyId']}
101
+ assert authenticated[connection.url_for('/foo')]
102
+ assert authenticated[connection.url_for('/foo', :authenticated => true)]
103
+ assert !authenticated[connection.url_for('/foo', :authenticated => false)]
104
+ end
105
+
106
+ def test_connecting_through_a_proxy
107
+ connection = nil
108
+ assert_nothing_raised do
109
+ connection = Connection.new(keys.merge(:proxy => sample_proxy_settings))
110
+ end
111
+ assert connection.http.proxy?
112
+ end
113
+
114
+ def test_request_only_escapes_the_path_the_first_time_it_runs_and_not_subsequent_times
115
+ connection = Connection.new(@keys)
116
+ unescaped_path = 'path with spaces'
117
+ escaped_path = 'path%20with%20spaces'
118
+
119
+ flexmock(Connection).should_receive(:prepare_path).with(unescaped_path).once.and_return(escaped_path).ordered
120
+ flexmock(connection.http).should_receive(:request).and_raise(Errno::EPIPE).ordered
121
+ flexmock(connection.http).should_receive(:request).ordered
122
+ connection.request :put, unescaped_path
123
+ end
124
+
125
+ def test_if_request_has_no_body_then_the_content_length_is_set_to_zero
126
+ # References bug: http://rubyforge.org/tracker/index.php?func=detail&aid=13052&group_id=2409&atid=9356
127
+ connection = Connection.new(@keys)
128
+ flexmock(Net::HTTP::Put).new_instances.should_receive(:content_length=).once.with(0).ordered
129
+ flexmock(connection.http).should_receive(:request).once.ordered
130
+ connection.request :put, 'path does not matter'
131
+ end
132
+ end
133
+
134
+ class ConnectionOptionsTest < Test::Unit::TestCase
135
+
136
+ def setup
137
+ @options = generate_options(:server => 'example.org', :port => 555)
138
+ @default_options = generate_options
139
+ end
140
+
141
+ def test_server_extracted
142
+ assert_key_transfered(:server, 'example.org', @options)
143
+ end
144
+
145
+ def test_port_extracted
146
+ assert_key_transfered(:port, 555, @options)
147
+ end
148
+
149
+ def test_server_defaults_to_default_host
150
+ assert_equal DEFAULT_HOST, @default_options[:server]
151
+ end
152
+
153
+ def test_port_defaults_to_80_if_use_ssl_is_false
154
+ assert_equal 80, @default_options[:port]
155
+ end
156
+
157
+ def test_port_is_set_to_443_if_use_ssl_is_true
158
+ options = generate_options(:use_ssl => true)
159
+ assert_equal 443, options[:port]
160
+ end
161
+
162
+ def test_explicit_port_trumps_use_ssl
163
+ options = generate_options(:port => 555, :use_ssl => true)
164
+ assert_equal 555, options[:port]
165
+ end
166
+
167
+ def test_invalid_options_raise
168
+ assert_raises(InvalidConnectionOption) do
169
+ generate_options(:host => 'campfire.s3.amazonaws.com')
170
+ end
171
+ end
172
+
173
+ def test_not_specifying_all_required_proxy_settings_raises
174
+ assert_raises(ArgumentError) do
175
+ generate_options(:proxy => {})
176
+ end
177
+ end
178
+
179
+ def test_not_specifying_proxy_option_at_all_does_not_raise
180
+ assert_nothing_raised do
181
+ generate_options
182
+ end
183
+ end
184
+
185
+ def test_specifying_all_required_proxy_settings
186
+ assert_nothing_raised do
187
+ generate_options(:proxy => sample_proxy_settings)
188
+ end
189
+ end
190
+
191
+ def test_only_host_setting_is_required
192
+ assert_nothing_raised do
193
+ generate_options(:proxy => {:host => 'http://google.com'})
194
+ end
195
+ end
196
+
197
+ def test_proxy_settings_are_extracted
198
+ options = generate_options(:proxy => sample_proxy_settings)
199
+ assert_equal sample_proxy_settings.values.map {|value| value.to_s}.sort, options.proxy_settings.map {|value| value.to_s}.sort
200
+ end
201
+
202
+ def test_recognizing_that_the_settings_want_to_connect_through_a_proxy
203
+ options = generate_options(:proxy => sample_proxy_settings)
204
+ assert options.connecting_through_proxy?
205
+ end
206
+
207
+ private
208
+ def assert_key_transfered(key, value, options)
209
+ assert_equal value, options[key]
210
+ end
211
+
212
+ def generate_options(options = {})
213
+ Connection::Options.new(options)
214
+ end
215
+ end
@@ -0,0 +1,70 @@
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
+ @container.send(:remove_const, :NotImplemented) if @container.const_defined?(:NotImplemented)
8
+ end
9
+
10
+ def test_error_class_is_automatically_generated
11
+ assert !@container.const_defined?('NotImplemented')
12
+ error = Error.new(Parsing::XmlParser.new(Fixtures::Errors.not_implemented))
13
+ assert @container.const_defined?('NotImplemented')
14
+ end
15
+
16
+ def test_error_contains_attributes
17
+ assert_equal 'Access Denied', @error.message
18
+ end
19
+
20
+ def test_error_is_raisable_as_exception
21
+ assert_raises(@container::AccessDenied) do
22
+ @error.raise
23
+ end
24
+ end
25
+
26
+ def test_error_message_is_passed_along_to_exception
27
+ @error.raise
28
+ rescue @container::AccessDenied => e
29
+ assert_equal 'Access Denied', e.message
30
+ end
31
+
32
+ def test_response_is_passed_along_to_exception
33
+ response = Error::Response.new(FakeResponse.new(:code => 409, :body => Fixtures::Errors.access_denied))
34
+ response.error.raise
35
+ rescue @container::ResponseError => e
36
+ assert e.response
37
+ assert_kind_of Error::Response, e.response
38
+ assert_equal response.error, e.response.error
39
+ end
40
+
41
+ def test_exception_class_clash
42
+ assert !@container.const_defined?(:NotImplemented)
43
+ # Create a class that does not inherit from exception that has the same name as the class
44
+ # the Error instance is about to attempt to find or create
45
+ @container.const_set(:NotImplemented, Class.new)
46
+ assert @container.const_defined?(:NotImplemented)
47
+
48
+ assert_raises(ExceptionClassClash) do
49
+ Error.new(Parsing::XmlParser.new(Fixtures::Errors.not_implemented))
50
+ end
51
+ end
52
+
53
+ def test_error_response_handles_attributes_with_no_value
54
+ mock_connection_for(Bucket, :returns => {:body => Fixtures::Errors.error_with_no_message, :code => 500})
55
+
56
+ begin
57
+ Bucket.create('foo', 'invalid-argument' => 'bad juju')
58
+ rescue ResponseError => error
59
+ end
60
+
61
+ assert_nothing_raised do
62
+ error.response.error.message
63
+ end
64
+ assert_nil error.response.error.message
65
+
66
+ assert_raises(NoMethodError) do
67
+ error.response.error.non_existant_method
68
+ end
69
+ end
70
+ end