scashin133-s3 0.3.8

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,187 @@
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
+
26
+ @xml_body = <<-EOXML
27
+ <?xml version="1.0" encoding="UTF-8"?>
28
+ <CopyObjectResult> <LastModified>#{Time.now.httpdate}</LastModified> <ETag>"etag"</ETag> </CopyObjectResult>
29
+ EOXML
30
+ @response_xml = Net::HTTPOK.new("1.1", "200", "OK")
31
+ @response_xml.stubs(:body).returns(@xml_body)
32
+ end
33
+
34
+ test "initializing" do
35
+ assert_raise ArgumentError do S3::Object.send(:new, nil, :key => "") end # should not allow empty key
36
+ assert_raise ArgumentError do S3::Object.send(:new, nil, :key => "//") end # should not allow key with double slash
37
+
38
+ assert_nothing_raised do
39
+ S3::Object.send(:new, nil, :key => "Lena.png")
40
+ S3::Object.send(:new, nil, :key => "Lena playboy.png")
41
+ S3::Object.send(:new, nil, :key => "Lena Söderberg.png")
42
+ S3::Object.send(:new, nil, :key => "/images/pictures/test images/Lena not full.png")
43
+ end
44
+ end
45
+
46
+ test "==" do
47
+ expected = false
48
+ actual = @object_lena == nil
49
+ assert_equal(expected, actual)
50
+ end
51
+
52
+ test "full key" do
53
+ expected = "images/Lena.png"
54
+ actual = @object_lena.full_key
55
+ assert_equal expected, actual
56
+ end
57
+
58
+ test "url" do
59
+ bucket1 = S3::Bucket.send(:new, @service, "images")
60
+
61
+ object11 = S3::Object.send(:new, bucket1, :key => "Lena.png")
62
+ expected = "http://images.s3.amazonaws.com/Lena.png"
63
+ actual = object11.url
64
+ assert_equal expected, actual
65
+
66
+ object12 = S3::Object.send(:new, bucket1, :key => "Lena Söderberg.png")
67
+ expected = "http://images.s3.amazonaws.com/Lena%20S%C3%B6derberg.png"
68
+ actual = object12.url
69
+ assert_equal expected, actual
70
+
71
+ bucket2 = S3::Bucket.send(:new, @service, "images_new")
72
+
73
+ object21 = S3::Object.send(:new, bucket2, :key => "Lena.png")
74
+ expected = "http://s3.amazonaws.com/images_new/Lena.png"
75
+ actual = object21.url
76
+ assert_equal expected, actual
77
+ end
78
+
79
+ test "cname url" do
80
+ bucket1 = S3::Bucket.send(:new, @service, "images.example.com")
81
+
82
+ object11 = S3::Object.send(:new, bucket1, :key => "Lena.png")
83
+ expected = "http://images.example.com/Lena.png"
84
+ actual = object11.cname_url
85
+ assert_equal expected, actual
86
+
87
+ object12 = S3::Object.send(:new, bucket1, :key => "Lena Söderberg.png")
88
+ expected = "http://images.example.com/Lena%20S%C3%B6derberg.png"
89
+ actual = object12.cname_url
90
+ assert_equal expected, actual
91
+
92
+ bucket2 = S3::Bucket.send(:new, @service, "images_new")
93
+
94
+ object21 = S3::Object.send(:new, bucket2, :key => "Lena.png")
95
+ expected = nil
96
+ actual = object21.cname_url
97
+ assert_equal expected, actual
98
+ end
99
+
100
+ test "destroy" do
101
+ @object_lena.expects(:object_request).with(:delete)
102
+ assert @object_lena.destroy
103
+ end
104
+
105
+ test "save" do
106
+ @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)
107
+ assert @object_lena.save
108
+ end
109
+
110
+ test "save with cache control headers" do
111
+ assert_equal "max-age=315360000", @object_mac.cache_control
112
+ @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)
113
+ assert @object_mac.save
114
+ end
115
+
116
+ test "content and parse headers" do
117
+ @object_lena.expects(:object_request).with(:get, {}).returns(@response_binary)
118
+
119
+ expected = /test/n
120
+ actual = @object_lena.content(true)
121
+ assert_match expected, actual
122
+ assert_equal "image/png", @object_lena.content_type
123
+
124
+ assert @object_lena.content
125
+
126
+ @object_lena.expects(:object_request).with(:get, {}).returns(@response_binary)
127
+ assert @object_lena.content(true)
128
+ end
129
+
130
+ test "retrieve" do
131
+ @object_lena.expects(:object_request).with(:head, {}).returns(@response_binary)
132
+ assert @object_lena.retrieve
133
+ end
134
+
135
+ test "exists" do
136
+ @object_lena.expects(:retrieve).returns(true)
137
+ assert @object_lena.exists?
138
+
139
+ @object_carmen.expects(:retrieve).raises(S3::Error::NoSuchKey.new(nil, nil))
140
+ assert ! @object_carmen.exists?
141
+ end
142
+
143
+ test "ACL writer" do
144
+ expected = nil
145
+ actual = @object_lena.acl
146
+ assert_equal expected, actual
147
+
148
+ assert @object_lena.acl = :public_read
149
+
150
+ expected = "public-read"
151
+ actual = @object_lena.acl
152
+ assert_equal expected, actual
153
+
154
+ assert @object_lena.acl = :private
155
+
156
+ expected = "private"
157
+ actual = @object_lena.acl
158
+ assert_equal expected, actual
159
+ end
160
+
161
+ test "storage-class writer" do
162
+ expected = nil
163
+ actual = @object_lena.storage_class
164
+ assert_equal expected, actual
165
+
166
+ assert @object_lena.storage_class = :standard
167
+
168
+ expected = "STANDARD"
169
+ actual = @object_lena.storage_class
170
+ assert_equal expected, actual
171
+
172
+ assert @object_lena.storage_class = :reduced_redundancy
173
+
174
+ expected = "REDUCED_REDUNDANCY"
175
+ actual = @object_lena.storage_class
176
+ assert_equal expected, actual
177
+ end
178
+
179
+ test "copy" do
180
+ @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)
181
+
182
+ new_object = @object_lena.copy(:key => "Lena-copy.png")
183
+
184
+ assert_equal "Lena-copy.png", new_object.key
185
+ assert_equal "Lena.png", @object_lena.key
186
+ end
187
+ 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