aws-s3-multi-region 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class LoggingStatusReadingTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @disabled = logging_status(:logging_disabled)
7
+ @enabled = logging_status(:logging_enabled)
8
+ @new_status = Logging::Status.new('target_bucket' => 'foo', 'target_prefix' => 'access-log-')
9
+ end
10
+
11
+ def test_logging_enabled?
12
+ assert !@disabled.logging_enabled?
13
+ assert !@new_status.logging_enabled?
14
+ assert @enabled.logging_enabled?
15
+ end
16
+
17
+ def test_passing_in_prefix_and_bucket
18
+ assert_equal 'foo', @new_status.target_bucket
19
+ assert_equal 'access-log-', @new_status.target_prefix
20
+ assert !@new_status.logging_enabled?
21
+ end
22
+
23
+ private
24
+ def logging_status(fixture)
25
+ Logging::Status.new(Parsing::XmlParser.new(Fixtures::Logging[fixture.to_s]))
26
+ end
27
+ end
28
+
29
+ class LoggingStatusWritingTest < LoggingStatusReadingTest
30
+ def setup
31
+ super
32
+ @disabled = Logging::Status.new(Parsing::XmlParser.new(@disabled.to_xml))
33
+ @enabled = Logging::Status.new(Parsing::XmlParser.new(@enabled.to_xml))
34
+ end
35
+ end
36
+
37
+ class LogTest < Test::Unit::TestCase
38
+ def test_value_converted_to_log_lines
39
+ log_object = S3Object.new
40
+ log_object.value = Fixtures::Logs.simple_log.join
41
+ log = Logging::Log.new(log_object)
42
+ assert_nothing_raised do
43
+ log.lines
44
+ end
45
+
46
+ assert_equal 2, log.lines.size
47
+ assert_kind_of Logging::Log::Line, log.lines.first
48
+ assert_equal 'marcel', log.lines.first.bucket
49
+ end
50
+ end
51
+
52
+ class LogLineTest < Test::Unit::TestCase
53
+ def setup
54
+ @line = Logging::Log::Line.new(Fixtures::Loglines.bucket_get)
55
+ end
56
+
57
+ def test_field_accessors
58
+ expected_results = {
59
+ :owner => Owner.new('id' => 'bb2041a25975c3d4ce9775fe9e93e5b77a6a9fad97dc7e00686191f3790b13f1'),
60
+ :bucket => 'marcel',
61
+ :time => Time.parse('Nov 14 2006 06:36:48 +0000'),
62
+ :remote_ip => '67.165.183.125',
63
+ :request_id => '8B5297D428A05432',
64
+ :requestor => Owner.new('id' => 'bb2041a25975c3d4ce9775fe9e93e5b77a6a9fad97dc7e00686191f3790b13f1'),
65
+ :operation => 'REST.GET.BUCKET',
66
+ :key => nil,
67
+ :request_uri => 'GET /marcel HTTP/1.1',
68
+ :http_status => 200,
69
+ :error_code => nil,
70
+ :bytes_sent => 4534,
71
+ :object_size => nil,
72
+ :total_time => 398,
73
+ :turn_around_time => 395,
74
+ :referrer => nil,
75
+ :user_agent => nil
76
+ }
77
+
78
+ expected_results.each do |field, expected|
79
+ assert_equal expected, @line.send(field)
80
+ end
81
+
82
+ assert_equal expected_results, @line.attributes
83
+ end
84
+
85
+ def test_user_agent
86
+ line = Logging::Log::Line.new(Fixtures::Loglines.browser_get)
87
+ assert_equal 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0', line.user_agent
88
+ end
89
+ end
@@ -0,0 +1,205 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ObjectTest < Test::Unit::TestCase
4
+ def setup
5
+ bucket = Bucket.new(Parsing::XmlParser.new(Fixtures::Buckets.bucket_with_one_key))
6
+ @object = bucket.objects.first
7
+ end
8
+
9
+ def test_header_settings_reader_and_writer
10
+ headers = {'content-type' => 'text/plain'}
11
+ mock_connection_for(S3Object, :returns => {:headers => headers})
12
+
13
+ assert_nothing_raised do
14
+ @object.content_type
15
+ end
16
+
17
+ assert_equal 'text/plain', @object.content_type
18
+
19
+ assert_nothing_raised do
20
+ @object.content_type = 'image/jpg'
21
+ end
22
+
23
+ assert_equal 'image/jpg', @object.content_type
24
+
25
+ assert_raises(NoMethodError) do
26
+ @object.non_existant_header_setting
27
+ end
28
+ end
29
+
30
+ def test_key_name_validation
31
+ assert_raises(InvalidKeyName) do
32
+ S3Object.create(nil, '', 'marcel')
33
+ end
34
+
35
+ assert_raises(InvalidKeyName) do
36
+ huge_name = 'a' * 1500
37
+ S3Object.create(huge_name, '', 'marcel')
38
+ end
39
+ end
40
+
41
+ def test_content_type_inference
42
+ [
43
+ ['foo.jpg', {}, 'image/jpeg'],
44
+ ['foo.txt', {}, 'text/plain'],
45
+ ['foo', {}, nil],
46
+ ['foo.asdf', {}, nil],
47
+ ['foo.jpg', {:content_type => nil}, nil],
48
+ ['foo', {:content_type => 'image/jpg'}, 'image/jpg'],
49
+ ['foo.jpg', {:content_type => 'image/png'}, 'image/png'],
50
+ ['foo.asdf', {:content_type => 'image/jpg'}, 'image/jpg']
51
+ ].each do |key, options, content_type|
52
+ S3Object.send(:infer_content_type!, key, options)
53
+ assert_equal content_type, options[:content_type]
54
+ end
55
+ end
56
+
57
+ def test_object_has_owner
58
+ assert_kind_of Owner, @object.owner
59
+ end
60
+
61
+ def test_owner_attributes_are_accessible
62
+ owner = @object.owner
63
+ assert owner.id
64
+ assert owner.display_name
65
+ assert_equal 'bb2041a25975c3d4ce9775fe9e93e5b77a6a9fad97dc7e00686191f3790b13f1', owner.id
66
+ assert_equal 'mmolina@onramp.net', owner.display_name
67
+ end
68
+
69
+ def test_only_valid_attributes_accessible
70
+ assert_raises(NoMethodError) do
71
+ @object.owner.foo
72
+ end
73
+ end
74
+
75
+ def test_fetching_object_value_generates_value_object
76
+ mock_connection_for(S3Object, :returns => {:body => 'hello!'})
77
+ value = S3Object.value('foo', 'bar')
78
+ assert_kind_of S3Object::Value, value
79
+ assert_equal 'hello!', value
80
+ end
81
+
82
+ def test_fetching_file_by_name_raises_when_heuristic_fails
83
+ mock_connection_for(Bucket, :returns => {:body => Fixtures::Buckets.bucket_with_one_key})
84
+ assert_raises(NoSuchKey) do
85
+ S3Object.find('not_tongue_overload.jpg', 'marcel_molina')
86
+ end
87
+
88
+ object = nil # Block scoping
89
+ assert_nothing_raised do
90
+ object = S3Object.find('tongue_overload.jpg', 'marcel_molina')
91
+ end
92
+ assert_kind_of S3Object, object
93
+ assert_equal 'tongue_overload.jpg', object.key
94
+ end
95
+
96
+ def test_about
97
+ headers = {'content-size' => '12345', 'date' => Time.now.httpdate, 'content-type' => 'application/xml'}
98
+ mock_connection_for(S3Object, :returns => [
99
+ {:headers => headers},
100
+ {:code => 404}
101
+ ]
102
+ )
103
+ about = S3Object.about('foo', 'bar')
104
+ assert_kind_of S3Object::About, about
105
+ assert_equal headers, about
106
+
107
+ assert_raises(NoSuchKey) do
108
+ S3Object.about('foo', 'bar')
109
+ end
110
+ end
111
+
112
+ def test_can_tell_that_an_s3object_does_not_exist
113
+ mock_connection_for(S3Object, :returns => {:code => 404})
114
+ assert_equal false, S3Object.exists?('foo', 'bar')
115
+ end
116
+
117
+ def test_can_tell_that_an_s3object_exists
118
+ mock_connection_for(S3Object, :returns => {:code => 200})
119
+ assert_equal true, S3Object.exists?('foo', 'bar')
120
+ end
121
+
122
+ def test_s3object_equality
123
+ mock_connection_for(Bucket, :returns => {:body => Fixtures::Buckets.bucket_with_more_than_one_key})
124
+ file1, file2 = Bucket.objects('does not matter')
125
+ assert file1 == file1
126
+ assert file2 == file2
127
+ assert !(file1 == file2) # /!\ Parens required /!\
128
+ end
129
+
130
+ def test_inspect
131
+ mock_connection_for(Bucket, :returns => {:body => Fixtures::Buckets.bucket_with_one_key})
132
+ object = S3Object.find('tongue_overload.jpg', 'bucket does not matter')
133
+ assert object.path
134
+ assert_nothing_raised { object.inspect }
135
+ assert object.inspect[object.path]
136
+ end
137
+
138
+ def test_etag
139
+ mock_connection_for(Bucket, :returns => {:body => Fixtures::Buckets.bucket_with_one_key})
140
+ file = S3Object.find('tongue_overload.jpg', 'bucket does not matter')
141
+ assert file.etag
142
+ assert_equal 'f21f7c4e8ea6e34b268887b07d6da745', file.etag
143
+ end
144
+
145
+ def test_fetching_information_about_an_object_that_does_not_exist_raises_no_such_key
146
+ mock_connection_for(S3Object, :returns => {:body => '', :code => 404})
147
+ assert_raises(NoSuchKey) do
148
+ S3Object.about('asdfasdfasdfas-this-does-not-exist', 'bucket does not matter')
149
+ end
150
+ end
151
+ end
152
+
153
+ class MetadataTest < Test::Unit::TestCase
154
+ def setup
155
+ @metadata = S3Object::Metadata.new(Fixtures::Headers.headers_including_one_piece_of_metadata)
156
+ end
157
+
158
+ def test_only_metadata_is_extracted
159
+ assert @metadata.to_headers.size == 1
160
+ assert @metadata.to_headers['x-amz-meta-test']
161
+ assert_equal 'foo', @metadata.to_headers['x-amz-meta-test']
162
+ end
163
+
164
+ def test_setting_new_metadata_normalizes_name
165
+ @metadata[:bar] = 'baz'
166
+ assert @metadata.to_headers.include?('x-amz-meta-bar')
167
+ @metadata['baz'] = 'quux'
168
+ assert @metadata.to_headers.include?('x-amz-meta-baz')
169
+ @metadata['x-amz-meta-quux'] = 'whatever'
170
+ assert @metadata.to_headers.include?('x-amz-meta-quux')
171
+ end
172
+
173
+ def test_clobbering_existing_header
174
+ @metadata[:bar] = 'baz'
175
+ assert_equal 'baz', @metadata.to_headers['x-amz-meta-bar']
176
+ @metadata[:bar] = 'quux'
177
+ assert_equal 'quux', @metadata.to_headers['x-amz-meta-bar']
178
+ @metadata['bar'] = 'foo'
179
+ assert_equal 'foo', @metadata.to_headers['x-amz-meta-bar']
180
+ @metadata['x-amz-meta-bar'] = 'bar'
181
+ assert_equal 'bar', @metadata.to_headers['x-amz-meta-bar']
182
+ end
183
+
184
+ def test_invalid_metadata
185
+ @metadata[:invalid_header] = ' ' * (S3Object::Metadata::SIZE_LIMIT + 1)
186
+ assert_raises InvalidMetadataValue do
187
+ @metadata.to_headers
188
+ end
189
+ end
190
+ end
191
+
192
+ class ValueTest < Test::Unit::TestCase
193
+ def setup
194
+ @response = FakeResponse.new(:body => 'hello there')
195
+ @value = S3Object::Value.new(@response)
196
+ end
197
+
198
+ def test_value_is_set_to_response_body
199
+ assert_equal @response.body, @value
200
+ end
201
+
202
+ def test_response_is_accessible_from_value_object
203
+ assert_equal @response, @value.response
204
+ end
205
+ end
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class TypecastingTest < Test::Unit::TestCase
4
+ # Make it easier to call methods in tests
5
+ Parsing::Typecasting.public_instance_methods.each do |method|
6
+ Parsing::Typecasting.send(:module_function, method)
7
+ end
8
+
9
+ def test_array_with_one_element_that_is_a_hash
10
+ value = [{'Available' => 'true'}]
11
+ assert_equal [{'available' => true}], Parsing::Typecasting.typecast(value)
12
+ end
13
+
14
+ def test_hash_with_one_key_whose_value_is_an_array
15
+ value = {
16
+ 'Bucket' =>
17
+ [
18
+ {'Available' => 'true'}
19
+ ]
20
+ }
21
+
22
+ expected = {
23
+ 'bucket' =>
24
+ [
25
+ {'available' => true}
26
+ ]
27
+ }
28
+ assert_equal expected, Parsing::Typecasting.typecast(value)
29
+ end
30
+
31
+ end
32
+
33
+ class XmlParserTest < Test::Unit::TestCase
34
+ def test_bucket_is_always_forced_to_be_an_array_unless_empty
35
+ one_bucket = Parsing::XmlParser.new(Fixtures::Buckets.bucket_list_with_one_bucket)
36
+ more_than_one = Parsing::XmlParser.new(Fixtures::Buckets.bucket_list_with_more_than_one_bucket)
37
+
38
+ [one_bucket, more_than_one].each do |bucket_list|
39
+ assert_kind_of Array, bucket_list['buckets']['bucket']
40
+ end
41
+
42
+ no_buckets = Parsing::XmlParser.new(Fixtures::Buckets.empty_bucket_list)
43
+ assert no_buckets.has_key?('buckets')
44
+ assert_nil no_buckets['buckets']
45
+ end
46
+
47
+ def test_bucket_contents_are_forced_to_be_an_array_unless_empty
48
+ one_key = Parsing::XmlParser.new(Fixtures::Buckets.bucket_with_one_key)
49
+ more_than_one = Parsing::XmlParser.new(Fixtures::Buckets.bucket_with_more_than_one_key)
50
+ [one_key, more_than_one].each do |bucket_with_contents|
51
+ assert_kind_of Array, bucket_with_contents['contents']
52
+ end
53
+
54
+ no_keys = Parsing::XmlParser.new(Fixtures::Buckets.empty_bucket)
55
+ assert !no_keys.has_key?('contents')
56
+ end
57
+
58
+ def test_policy_grants_are_always_an_array
59
+ policy = Parsing::XmlParser.new(Fixtures::Policies.policy_with_one_grant)
60
+ assert_kind_of Array, policy['access_control_list']['grant']
61
+ end
62
+
63
+ def test_empty_xml_response_is_not_parsed
64
+ assert_equal({}, Parsing::XmlParser.new(''))
65
+ end
66
+ end
@@ -0,0 +1,68 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ class BaseResponseTest < Test::Unit::TestCase
3
+ def setup
4
+ @headers = {'content-type' => 'text/plain', 'date' => Time.now}
5
+ @response = FakeResponse.new()
6
+ @base_response = Base::Response.new(@response)
7
+ end
8
+
9
+ def test_status_predicates
10
+ response = Proc.new {|code| Base::Response.new(FakeResponse.new(:code => code))}
11
+ assert response[200].success?
12
+ assert response[300].redirect?
13
+ assert response[400].client_error?
14
+ assert response[500].server_error?
15
+ end
16
+
17
+ def test_headers_passed_along_from_original_response
18
+ assert_equal @response.headers, @base_response.headers
19
+ assert_equal @response['date'], @base_response['date']
20
+ original_headers, new_headers = {}, {}
21
+ @response.headers.each {|k,v| original_headers[k] = v}
22
+ @base_response.each {|k,v| new_headers[k] = v}
23
+ assert_equal original_headers, new_headers
24
+ end
25
+ end
26
+
27
+ class ErrorResponseTest < Test::Unit::TestCase
28
+ def test_error_responses_are_always_in_error
29
+ assert Error::Response.new(FakeResponse.new).error?
30
+ assert Error::Response.new(FakeResponse.new(:code => 200)).error?
31
+ assert Error::Response.new(FakeResponse.new(:headers => {'content-type' => 'text/plain'})).error?
32
+ end
33
+ end
34
+
35
+ class S3ObjectResponseTest < Test::Unit::TestCase
36
+ def test_etag_extracted
37
+ mock_connection_for(S3Object, :returns => {:headers => {"etag" => %("acbd18db4cc2f85cedef654fccc4a4d8")}}).once
38
+ object_response = S3Object.create('name_does_not_matter', 'data does not matter', 'bucket does not matter')
39
+ assert_equal "acbd18db4cc2f85cedef654fccc4a4d8", object_response.etag
40
+ end
41
+ end
42
+
43
+ class ResponseClassFinderTest < Test::Unit::TestCase
44
+ class CampfireBucket < Bucket
45
+ end
46
+
47
+ class BabyBase < Base
48
+ end
49
+
50
+ def test_on_base
51
+ assert_equal Base::Response, FindResponseClass.for(Base)
52
+ assert_equal Base::Response, FindResponseClass.for(AWS::S3::Base)
53
+
54
+ end
55
+
56
+ def test_on_subclass_with_corresponding_response_class
57
+ assert_equal Bucket::Response, FindResponseClass.for(Bucket)
58
+ assert_equal Bucket::Response, FindResponseClass.for(AWS::S3::Bucket)
59
+ end
60
+
61
+ def test_on_subclass_with_intermediary_parent_that_has_corresponding_response_class
62
+ assert_equal Bucket::Response, FindResponseClass.for(CampfireBucket)
63
+ end
64
+
65
+ def test_on_subclass_with_no_corresponding_response_class_and_no_intermediary_parent
66
+ assert_equal Base::Response, FindResponseClass.for(BabyBase)
67
+ end
68
+ end
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ServiceTest < Test::Unit::TestCase
4
+ def test_bucket_list_with_empty_bucket_list
5
+ mock_connection_for(Service, :returns => {:body => Fixtures::Buckets.empty_bucket_list, :code => 200})
6
+ list = Service.buckets(:reload)
7
+ assert_equal [], list
8
+ end
9
+
10
+ def test_bucket_list_with_bucket_list_containing_one_bucket
11
+ mock_connection_for(Service, :returns => {:body => Fixtures::Buckets.bucket_list_with_one_bucket, :code => 200})
12
+ list = Service.buckets(:reload)
13
+ assert_equal 1, list.size
14
+ assert_equal 'marcel_molina', list.first.name
15
+ end
16
+
17
+ def test_bucket_list_with_bucket_list_containing_more_than_one_bucket
18
+ mock_connection_for(Service, :returns => {:body => Fixtures::Buckets.bucket_list_with_more_than_one_bucket, :code => 200})
19
+ list = Service.buckets(:reload)
20
+ assert_equal 2, list.size
21
+ assert_equal %w(marcel_molina marcel_molina_jr), list.map {|bucket| bucket.name}.sort
22
+ end
23
+ end