seanwalbran-scashin133-s3 0.3.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,214 @@
1
+ require "test_helper"
2
+
3
+ class ConnectionTest < Test::Unit::TestCase
4
+ def setup
5
+ @connection = S3::Connection.new(
6
+ :access_key_id => "12345678901234567890",
7
+ :secret_access_key => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDF"
8
+ )
9
+ @http_request = Net::HTTP.new("")
10
+ @response_ok = Net::HTTPOK.new("1.1", "200", "OK")
11
+ @response_not_found = Net::HTTPNotFound.new("1.1", "404", "Not Found")
12
+ @response_error = Net::HTTPInternalServerError.new("1.1", "500", "Internal Server Error")
13
+ @response_temporary_redirect = Net::HTTPInternalServerError.new("1.1", "307", "Temporary Redirect")
14
+ @connection.stubs(:http).returns(@http_request)
15
+
16
+ @http_request.stubs(:start).returns(@response_ok)
17
+ end
18
+
19
+ test "handle response not modify response when ok" do
20
+ assert_nothing_raised do
21
+ response = @connection.request(
22
+ :get,
23
+ :host => "s3.amazonaws.com",
24
+ :path => "/"
25
+ )
26
+ assert_equal @response_ok, response
27
+ end
28
+ end
29
+
30
+ test "handle response throws exception when error" do
31
+ response_body = <<-EOFakeBody
32
+ <?xml version=\"1.0\" encoding=\"UTF-8\"?>
33
+ <Error>
34
+ <Code>NoSuchBucket</Code>
35
+ <Message>The specified bucket does not exist</Message>
36
+ </Error>
37
+ EOFakeBody
38
+
39
+ @http_request.stubs(:start).returns(@response_not_found)
40
+ @response_not_found.stubs(:body).returns(response_body)
41
+
42
+ assert_raise S3::Error::NoSuchBucket do
43
+ response = @connection.request(
44
+ :get,
45
+ :host => "data.example.com.s3.amazonaws.com",
46
+ :path => "/"
47
+ )
48
+ end
49
+ end
50
+
51
+ test "handle response throws standard exception when error" do
52
+ @http_request.stubs(:start).returns(@response_error)
53
+ @response_error.stubs(:body)
54
+ assert_raise S3::Error::ResponseError do
55
+ response = @connection.request(
56
+ :get,
57
+ :host => "data.example.com.s3.amazonaws.com",
58
+ :path => "/"
59
+ )
60
+ end
61
+
62
+ @response_error.stubs(:body).returns("")
63
+ assert_raise S3::Error::ResponseError do
64
+ response = @connection.request(
65
+ :get,
66
+ :host => "data.example.com.s3.amazonaws.com",
67
+ :path => "/"
68
+ )
69
+ end
70
+ end
71
+
72
+ test "parse params empty" do
73
+ expected = ""
74
+ actual = S3::Connection.parse_params({})
75
+ assert_equal expected, actual
76
+ end
77
+
78
+ test "parse params only interesting params" do
79
+ expected = ""
80
+ actual = S3::Connection.parse_params(:param1 => "1", :maxkeys => "2")
81
+ assert_equal expected, actual
82
+ end
83
+
84
+ test "parse params remove underscore" do
85
+ expected = "max-keys=100"
86
+ actual = S3::Connection.parse_params(:max_keys => 100)
87
+ assert_equal expected, actual
88
+ end
89
+
90
+ test "parse params with and without values" do
91
+ params = S3::Connection.parse_params(:max_keys => 100, :prefix => nil)
92
+
93
+ splitted_params = params.split("&")
94
+ assert_equal 2, splitted_params.length
95
+ assert splitted_params.include?("max-keys=100")
96
+ assert splitted_params.include?("prefix")
97
+ end
98
+
99
+ test "headers empty" do
100
+ expected = {}
101
+ actual = S3::Connection.parse_headers({})
102
+ assert_equal expected, actual
103
+ end
104
+
105
+ test "parse only interesting headers" do
106
+ expected = {}
107
+ actual = S3::Connection.parse_headers(
108
+ :accept => "text/*, text/html, text/html;level=1, */*",
109
+ :accept_charset => "iso-8859-2, unicode-1-1;q=0.8"
110
+ )
111
+ assert_equal expected, actual
112
+ end
113
+
114
+ test "parse headers remove underscore" do
115
+ expected = {
116
+ "content-type" => nil,
117
+ "x-amz-acl" => nil,
118
+ "x-amz-storage-class" => nil,
119
+ "if-modified-since" => nil,
120
+ "if-unmodified-since" => nil,
121
+ "if-match" => nil,
122
+ "if-none-match" => nil,
123
+ "content-disposition" => nil,
124
+ "content-encoding" => nil
125
+ }
126
+ actual = S3::Connection.parse_headers(
127
+ :content_type => nil,
128
+ :x_amz_acl => nil,
129
+ :x_amz_storage_class => nil,
130
+ :if_modified_since => nil,
131
+ :if_unmodified_since => nil,
132
+ :if_match => nil,
133
+ :if_none_match => nil,
134
+ :content_disposition => nil,
135
+ :content_encoding => nil
136
+ )
137
+ assert_equal expected, actual
138
+ end
139
+
140
+ test "parse headers with values" do
141
+ expected = {
142
+ "content-type" => "text/html",
143
+ "x-amz-acl" => "public-read",
144
+ "x-amz-storage-class" => "STANDARD",
145
+ "if-modified-since" => "today",
146
+ "if-unmodified-since" => "tomorrow",
147
+ "if-match" => "1234",
148
+ "if-none-match" => "1243",
149
+ "content-disposition" => "inline",
150
+ "content-encoding" => "gzip"
151
+ }
152
+ actual = S3::Connection.parse_headers(
153
+ :content_type => "text/html",
154
+ :x_amz_acl => "public-read",
155
+ :x_amz_storage_class => "STANDARD",
156
+ :if_modified_since => "today",
157
+ :if_unmodified_since => "tomorrow",
158
+ :if_match => "1234",
159
+ :if_none_match => "1243",
160
+ :content_disposition => "inline",
161
+ :content_encoding => "gzip"
162
+ )
163
+ assert_equal expected, actual
164
+ end
165
+
166
+ test "parse headers with range" do
167
+ expected = {
168
+ "range" => "bytes=0-100"
169
+ }
170
+ actual = S3::Connection.parse_headers(
171
+ :range => 0..100
172
+ )
173
+ assert_equal expected, actual
174
+ end
175
+
176
+ test "response.body is nil on TemporaryRedirect" do
177
+ @http_request.stubs(:start).returns(@response_temporary_redirect)
178
+ @response_temporary_redirect.stubs(:body).returns(nil)
179
+
180
+ assert_nothing_raised do
181
+ response = @connection.request(
182
+ :get,
183
+ :host => "data.example.com.s3.amazonaws.com",
184
+ :path => "/"
185
+ )
186
+ assert_equal nil, response
187
+ end
188
+ end
189
+
190
+ test "response body with new host on TemporaryRedirect" do
191
+ response_body = <<-EOFakeBody
192
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
193
+ <Error>
194
+ <Code>TemporaryRedirect</Code>
195
+ <Message>Please re-send this request to the specified temporary endpoint. Continue to use the original request endpoint for future requests.</Message>
196
+ <RequestId>24A0BB91158D470B</RequestId>
197
+ <Bucket>data.example.com</Bucket>
198
+ <HostId>DFcq9ktw5HvWZLduutz8fnVzqtXLwIZcAezc7mgyS7lJ2ux+RChY4qAJGa2fQDjV</HostId>
199
+ <Endpoint>data.example.com.s3-external-3.amazonaws.com</Endpoint>
200
+ </Error>"
201
+ EOFakeBody
202
+
203
+ @response_temporary_redirect.stubs(:body).returns(response_body)
204
+
205
+ assert_nothing_raised do
206
+ response = @connection.request(
207
+ :get,
208
+ :host => "data.example.com.s3.amazonaws.com",
209
+ :path => "/"
210
+ )
211
+ assert_equal @response_ok, response
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,205 @@
1
+ # encoding: utf-8
2
+ require "test_helper"
3
+
4
+ class ObjectTest < Test::Unit::TestCase
5
+ def setup
6
+ @service = S3::Service.new(
7
+ :access_key_id => "1234",
8
+ :secret_access_key => "1337"
9
+ )
10
+ @bucket_images = S3::Bucket.send(:new, @service, "images")
11
+ @object_lena = S3::Object.send(:new, @bucket_images, :key => "Lena.png")
12
+ @object_lena.content = "test"
13
+ @object_carmen = S3::Object.send(:new, @bucket_images, :key => "Carmen.png")
14
+ @object_mac = S3::Object.send(:new, @bucket_images, :key => "Mac.png", :cache_control => "max-age=315360000")
15
+ @object_mac.content = "test2"
16
+
17
+ @response_binary = Net::HTTPOK.new("1.1", "200", "OK")
18
+ @response_binary.stubs(:body).returns("test".respond_to?(:force_encoding) ? "test".force_encoding(Encoding::BINARY) : "test")
19
+ @response_binary["etag"] = ""
20
+ @response_binary["content-type"] = "image/png"
21
+ @response_binary["content-disposition"] = "inline"
22
+ @response_binary["content-encoding"] = nil
23
+ @response_binary["last-modified"] = Time.now.httpdate
24
+ @response_binary["content-length"] = 20
25
+ @response_binary["x-amz-meta-test"] = "metadata"
26
+
27
+ @xml_body = <<-EOXML
28
+ <?xml version="1.0" encoding="UTF-8"?>
29
+ <CopyObjectResult> <LastModified>#{Time.now.httpdate}</LastModified> <ETag>"etag"</ETag> </CopyObjectResult>
30
+ EOXML
31
+ @response_xml = Net::HTTPOK.new("1.1", "200", "OK")
32
+ @response_xml.stubs(:body).returns(@xml_body)
33
+ end
34
+
35
+ test "initializing" do
36
+ assert_raise ArgumentError do S3::Object.send(:new, nil, :key => "") end # should not allow empty key
37
+ assert_raise ArgumentError do S3::Object.send(:new, nil, :key => "//") end # should not allow key with double slash
38
+
39
+ assert_nothing_raised do
40
+ S3::Object.send(:new, nil, :key => "Lena.png")
41
+ S3::Object.send(:new, nil, :key => "Lena playboy.png")
42
+ S3::Object.send(:new, nil, :key => "Lena Söderberg.png")
43
+ S3::Object.send(:new, nil, :key => "/images/pictures/test images/Lena not full.png")
44
+ end
45
+ end
46
+
47
+ test "==" do
48
+ expected = false
49
+ actual = @object_lena == nil
50
+ assert_equal(expected, actual)
51
+ end
52
+
53
+ test "full key" do
54
+ expected = "images/Lena.png"
55
+ actual = @object_lena.full_key
56
+ assert_equal expected, actual
57
+ end
58
+
59
+ test "url" do
60
+ bucket1 = S3::Bucket.send(:new, @service, "images")
61
+
62
+ object11 = S3::Object.send(:new, bucket1, :key => "Lena.png")
63
+ expected = "http://images.s3.amazonaws.com/Lena.png"
64
+ actual = object11.url
65
+ assert_equal expected, actual
66
+
67
+ object12 = S3::Object.send(:new, bucket1, :key => "Lena Söderberg.png")
68
+ expected = "http://images.s3.amazonaws.com/Lena%20S%C3%B6derberg.png"
69
+ actual = object12.url
70
+ assert_equal expected, actual
71
+
72
+ bucket2 = S3::Bucket.send(:new, @service, "images_new")
73
+
74
+ object21 = S3::Object.send(:new, bucket2, :key => "Lena.png")
75
+ expected = "http://s3.amazonaws.com/images_new/Lena.png"
76
+ actual = object21.url
77
+ assert_equal expected, actual
78
+ end
79
+
80
+ test "cname url" do
81
+ bucket1 = S3::Bucket.send(:new, @service, "images.example.com")
82
+
83
+ object11 = S3::Object.send(:new, bucket1, :key => "Lena.png")
84
+ expected = "http://images.example.com/Lena.png"
85
+ actual = object11.cname_url
86
+ assert_equal expected, actual
87
+
88
+ object12 = S3::Object.send(:new, bucket1, :key => "Lena Söderberg.png")
89
+ expected = "http://images.example.com/Lena%20S%C3%B6derberg.png"
90
+ actual = object12.cname_url
91
+ assert_equal expected, actual
92
+
93
+ bucket2 = S3::Bucket.send(:new, @service, "images_new")
94
+
95
+ object21 = S3::Object.send(:new, bucket2, :key => "Lena.png")
96
+ expected = nil
97
+ actual = object21.cname_url
98
+ assert_equal expected, actual
99
+ end
100
+
101
+ test "destroy" do
102
+ @object_lena.expects(:object_request).with(:delete)
103
+ assert @object_lena.destroy
104
+ end
105
+
106
+ test "save" do
107
+ @object_lena.expects(:object_request).with(:put, :body=>"test", :headers=>{ :x_amz_acl=>"public-read", :x_amz_storage_class=>"STANDARD", :content_type=>"application/octet-stream" }).returns(@response_binary)
108
+ assert @object_lena.save
109
+ end
110
+
111
+ test "save with cache control headers" do
112
+ assert_equal "max-age=315360000", @object_mac.cache_control
113
+ @object_mac.expects(:object_request).with(:put, :body=>"test2", :headers=>{ :x_amz_acl=>"public-read", :x_amz_storage_class=>"STANDARD", :content_type=>"application/octet-stream", :cache_control=>"max-age=315360000" }).returns(@response_binary)
114
+ assert @object_mac.save
115
+ end
116
+
117
+ test "content and parse headers" do
118
+ @object_lena.expects(:object_request).with(:get, {}).returns(@response_binary)
119
+
120
+ expected = /test/n
121
+ actual = @object_lena.content(true)
122
+ assert_match expected, actual
123
+ assert_equal "image/png", @object_lena.content_type
124
+
125
+ assert @object_lena.content
126
+
127
+ @object_lena.expects(:object_request).with(:get, {}).returns(@response_binary)
128
+ assert @object_lena.content(true)
129
+ end
130
+
131
+ test "retrieve" do
132
+ @object_lena.expects(:object_request).with(:head, {}).returns(@response_binary)
133
+ assert @object_lena.retrieve
134
+ end
135
+
136
+ test "retrieve headers" do
137
+ @object_lena.expects(:object_request).twice.with(:head, {}).returns(@response_binary)
138
+ assert @object_lena.retrieve
139
+
140
+ meta = {"x-amz-meta-test" => ["metadata"]}
141
+ assert_equal meta, @object_lena.retrieve.metadata
142
+ end
143
+
144
+ test "exists" do
145
+ @object_lena.expects(:retrieve).returns(true)
146
+ assert @object_lena.exists?
147
+
148
+ @object_carmen.expects(:retrieve).raises(S3::Error::NoSuchKey.new(nil, nil))
149
+ assert ! @object_carmen.exists?
150
+ end
151
+
152
+ test "ACL writer" do
153
+ expected = nil
154
+ actual = @object_lena.acl
155
+ assert_equal expected, actual
156
+
157
+ assert @object_lena.acl = :public_read
158
+
159
+ expected = "public-read"
160
+ actual = @object_lena.acl
161
+ assert_equal expected, actual
162
+
163
+ assert @object_lena.acl = :private
164
+
165
+ expected = "private"
166
+ actual = @object_lena.acl
167
+ assert_equal expected, actual
168
+ end
169
+
170
+ test "storage-class writer" do
171
+ expected = nil
172
+ actual = @object_lena.storage_class
173
+ assert_equal expected, actual
174
+
175
+ assert @object_lena.storage_class = :standard
176
+
177
+ expected = "STANDARD"
178
+ actual = @object_lena.storage_class
179
+ assert_equal expected, actual
180
+
181
+ assert @object_lena.storage_class = :reduced_redundancy
182
+
183
+ expected = "REDUCED_REDUNDANCY"
184
+ actual = @object_lena.storage_class
185
+ assert_equal expected, actual
186
+ end
187
+
188
+ test "replace" do
189
+ @bucket_images.expects(:bucket_request).with(:put, :path => "Lena-copy.png", :headers => { :x_amz_acl => "public-read", :content_type => "application/octet-stream", :x_amz_copy_source => "images/Lena.png", :x_amz_metadata_directive => "REPLACE" }).returns(@response_xml)
190
+
191
+ new_object = @object_lena.copy(:key => "Lena-copy.png")
192
+
193
+ assert_equal "Lena-copy.png", new_object.key
194
+ assert_equal "Lena.png", @object_lena.key
195
+ end
196
+
197
+ test "copy" do
198
+ @bucket_images.expects(:bucket_request).with(:put, :path => "Lena-copy.png", :headers => { :x_amz_acl => "public-read", :content_type => "application/octet-stream", :x_amz_copy_source => "images/Lena.png", :x_amz_metadata_directive => "COPY" }).returns(@response_xml)
199
+
200
+ new_object = @object_lena.copy(:key => "Lena-copy.png", :replace => false)
201
+
202
+ assert_equal "Lena-copy.png", new_object.key
203
+ assert_equal "Lena.png", @object_lena.key
204
+ end
205
+ end
@@ -0,0 +1,111 @@
1
+ require "test_helper"
2
+
3
+ class ServiceTest < Test::Unit::TestCase
4
+ def setup
5
+ @buckets_list_body = <<-EOBuckets
6
+ <?xml version="1.0" encoding="UTF-8"?>\n<ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <Owner> <ID>123u1odhkhfoadf</ID> <DisplayName>JohnDoe</DisplayName> </Owner> <Buckets> <Bucket> <Name>data.example.com</Name> <CreationDate>2009-07-02T11:56:58.000Z</CreationDate> </Bucket> <Bucket> <Name>images</Name> <CreationDate>2009-06-05T12:26:33.000Z</CreationDate> </Bucket> </Buckets> </ListAllMyBucketsResult>
7
+ EOBuckets
8
+
9
+ @bucket_not_exists = <<-EOBucketNoexists
10
+ <?xml version="1.0" encoding="UTF-8"?>\n<Error> <Code>NoSuchBucket</Code> <Message>The specified bucket does not exists</Message> <BucketName>data2.example.com</BucketName> <RequestId>8D7519AAE74E9E99</RequestId> <HostId>DvKnnNSMnPHd1oXukyRaFNv8Lg/bpwhuUtY8Kj7eDLbaIrIT8JebSnHwi89AK1P+</HostId> </Error>
11
+ EOBucketNoexists
12
+
13
+ @bucket_exists = <<-EOBucketexists
14
+ <?xml version="1.0" encoding="UTF-8"?>\n<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <Name>data.synergypeople.net</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <IsTruncated>false</IsTruncated> </ListBucketResult>
15
+ EOBucketexists
16
+
17
+ @service_empty_buckets_list = S3::Service.new(
18
+ :access_key_id => "12345678901234567890",
19
+ :secret_access_key => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDF"
20
+ )
21
+ @response_empty_buckets_list = Net::HTTPOK.new("1.1", "200", "OK")
22
+ @service_empty_buckets_list.stubs(:service_request).returns(@response_empty_buckets_list)
23
+ @response_empty_buckets_list.stubs(:body).returns(@buckets_empty_list_body)
24
+
25
+ @service_buckets_list = S3::Service.new(
26
+ :access_key_id => "12345678901234567890",
27
+ :secret_access_key => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDF"
28
+ )
29
+ @response_buckets_list = Net::HTTPOK.new("1.1", "200", "OK")
30
+ @service_buckets_list.stubs(:service_request).returns(@response_buckets_list)
31
+ @response_buckets_list.stubs(:body).returns(@buckets_list_body)
32
+
33
+ @service_bucket_exists = S3::Service.new(
34
+ :access_key_id => "12345678901234567890",
35
+ :secret_access_key => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDF"
36
+ )
37
+ @response_bucket_exists = Net::HTTPNotFound.new("1.1", "200", "OK")
38
+ @service_bucket_exists.stubs(:service_request).returns(@response_bucket_exists)
39
+ @response_bucket_exists.stubs(:body).returns(@bucket_exists)
40
+
41
+ @service_bucket_not_exists = S3::Service.new(
42
+ :access_key_id => "12345678901234567890",
43
+ :secret_access_key => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDF"
44
+ )
45
+ @response_bucket_not_exists = Net::HTTPNotFound.new("1.1", "404", "Not Found")
46
+ @service_bucket_not_exists.stubs(:service_request).raises(S3::Error::NoSuchBucket.new(404, @response_bucket_not_exists))
47
+ @response_bucket_not_exists.stubs(:body).returns(@bucket_not_exists)
48
+
49
+ @buckets_empty_list = []
50
+ @buckets_empty_list_body = <<-EOEmptyBuckets
51
+ <?xml version="1.0" encoding="UTF-8"?>\n<ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <Owner> <ID>123u1odhkhfoadf</ID> <DisplayName>JohnDoe</DisplayName> </Owner> <Buckets> </Buckets> </ListAllMyBucketsResult>
52
+ EOEmptyBuckets
53
+
54
+ @buckets_list = [
55
+ S3::Bucket.send(:new, @service_buckets_list, "data.example.com"),
56
+ S3::Bucket.send(:new, @service_buckets_list, "images")
57
+ ]
58
+ end
59
+
60
+ test "buckets and parse buckets empty" do
61
+ expected = @buckets_empty_list
62
+ actual = @service_empty_buckets_list.buckets
63
+ assert_equal expected.length, actual.length
64
+ assert_equal expected, actual
65
+ end
66
+
67
+ test "buckets and parse buckets" do
68
+ expected = @buckets_list
69
+ # ugly hack
70
+ actual = @service_buckets_list.buckets.map { |obj| obj }
71
+ assert_equal expected, actual
72
+ end
73
+
74
+ test "buckets build" do
75
+ @service_empty_buckets_list.stubs(:service_request)
76
+
77
+ expected = "bucket_name"
78
+ actual = @service_empty_buckets_list.buckets.build("bucket_name")
79
+ assert_kind_of S3::Bucket, actual
80
+ assert_equal expected, actual.name
81
+ end
82
+
83
+ test "buckets find first" do
84
+ assert_nothing_raised do
85
+ actual = @service_buckets_list.buckets.find_first("data.example.com")
86
+ assert_equal "data.example.com", actual.name
87
+ end
88
+ end
89
+
90
+ test "buckets find first fail" do
91
+ assert_raise S3::Error::NoSuchBucket do
92
+ @service_bucket_not_exists.buckets.find_first("data2.example.com")
93
+ end
94
+ end
95
+
96
+ test "buckets find all on empty list" do
97
+ assert_nothing_raised do
98
+ expected = @buckets_empty_list
99
+ actual = @service_empty_buckets_list.buckets.find_all
100
+ assert_equal expected, actual
101
+ end
102
+ end
103
+
104
+ test "buckets find all" do
105
+ assert_nothing_raised do
106
+ expected = @buckets_list
107
+ actual = @service_buckets_list.buckets.find_all
108
+ assert_equal expected, actual
109
+ end
110
+ end
111
+ end