steamcannon-s3 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +27 -0
- data/LICENSE +20 -0
- data/README.rdoc +22 -0
- data/Rakefile +20 -0
- data/bin/s3 +187 -0
- data/extra/s3_attachment_fu.rb +159 -0
- data/extra/s3_paperclip.rb +156 -0
- data/lib/s3/bucket.rb +153 -0
- data/lib/s3/buckets_extension.rb +27 -0
- data/lib/s3/connection.rb +224 -0
- data/lib/s3/exceptions.rb +110 -0
- data/lib/s3/object.rb +249 -0
- data/lib/s3/objects_extension.rb +37 -0
- data/lib/s3/parser.rb +48 -0
- data/lib/s3/service.rb +82 -0
- data/lib/s3/signature.rb +229 -0
- data/lib/s3/version.rb +3 -0
- data/lib/s3.rb +26 -0
- data/s3.gemspec +27 -0
- data/test/bucket_test.rb +215 -0
- data/test/connection_test.rb +214 -0
- data/test/object_test.rb +179 -0
- data/test/service_test.rb +111 -0
- data/test/signature_test.rb +205 -0
- data/test/test_helper.rb +6 -0
- metadata +168 -0
data/s3.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.expand_path("../lib/s3/version", __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "steamcannon-s3"
|
7
|
+
s.version = S3::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Jakub Kuźma"]
|
10
|
+
s.email = ["qoobaa@gmail.com"]
|
11
|
+
s.homepage = "http://jah.pl/projects/s3.html"
|
12
|
+
s.summary = "Library for accessing S3 objects and buckets, with command line tool"
|
13
|
+
s.description = "S3 library provides access to Amazon's Simple Storage Service. It supports both: European and US buckets through REST API."
|
14
|
+
|
15
|
+
s.required_rubygems_version = ">= 1.3.6"
|
16
|
+
s.rubyforge_project = "s3"
|
17
|
+
|
18
|
+
s.add_dependency "trollop"
|
19
|
+
s.add_dependency "proxies"
|
20
|
+
s.add_development_dependency "test-unit", ">= 2.0"
|
21
|
+
s.add_development_dependency "mocha"
|
22
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
23
|
+
|
24
|
+
s.files = `git ls-files`.split("\n")
|
25
|
+
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
26
|
+
s.require_path = "lib"
|
27
|
+
end
|
data/test/bucket_test.rb
ADDED
@@ -0,0 +1,215 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class BucketTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@bucket_vhost = S3::Bucket.send(:new, nil, "Data-Bucket")
|
6
|
+
@bucket_path = S3::Bucket.send(:new, nil, "Data_Bucket")
|
7
|
+
@bucket = @bucket_vhost
|
8
|
+
|
9
|
+
@bucket_location = "EU"
|
10
|
+
@bucket_location_body = <<-EOLocation
|
11
|
+
<?xml version="1.0" encoding="UTF-8"?>\n<LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">EU</LocationConstraint>
|
12
|
+
EOLocation
|
13
|
+
|
14
|
+
@response_location = Net::HTTPOK.new("1.1", "200", "OK")
|
15
|
+
@response_location.stubs(:body).returns(@bucket_location_body)
|
16
|
+
|
17
|
+
@bucket_owned_by_you_body = <<-EOOwnedByYou
|
18
|
+
<?xml version="1.0" encoding="UTF-8"?>\n<Error> <Code>BucketAlreadyOwnedByYou</Code> <Message>Your previous request to create the named bucket succeeded and you already own it.</Message> <BucketName>bucket</BucketName> <RequestId>117D08EA0EC6E860</RequestId> <HostId>4VpMSvmJ+G5+DLtVox6O5cZNgdPlYcjCu3l0n4HjDe01vPxxuk5eTAtcAkUynRyV</HostId> </Error>
|
19
|
+
EOOwnedByYou
|
20
|
+
|
21
|
+
@reponse_owned_by_you = Net::HTTPConflict.new("1.1", "409", "Conflict")
|
22
|
+
@reponse_owned_by_you.stubs(:body).returns(@bucket_owned_by_you_body)
|
23
|
+
|
24
|
+
@bucket_already_exists_body = <<-EOAlreadyExists
|
25
|
+
<?xml version="1.0" encoding="UTF-8"?>\n<Error> <Code>BucketAlreadyExists</Code> <Message>The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again.</Message> <BucketName>bucket</BucketName> <RequestId>4C154D32807C92BD</RequestId> <HostId>/xyHQgXcUXTZQhoO+NUBzbaxbFrIhKlyuaRHFnmcId0bMePvY9Zwg+dyk2LYE4g5</HostId> </Error>
|
26
|
+
EOAlreadyExists
|
27
|
+
|
28
|
+
@reponse_already_exists = Net::HTTPConflict.new("1.1", "409", "Conflict")
|
29
|
+
@response_already_exists.stubs(:body).returns(@bucket_already_exists_body)
|
30
|
+
|
31
|
+
@objects_list_empty = []
|
32
|
+
@objects_list = [
|
33
|
+
S3::Object.send(:new, @bucket, :key => "obj1"),
|
34
|
+
S3::Object.send(:new, @bucket, :key => "obj2")
|
35
|
+
]
|
36
|
+
|
37
|
+
@response_objects_list_empty_body = <<-EOEmpty
|
38
|
+
<?xml version="1.0" encoding="UTF-8"?>\n<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <Name>bucket</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <IsTruncated>false</IsTruncated> </ListBucketResult>
|
39
|
+
EOEmpty
|
40
|
+
|
41
|
+
@response_objects_list_empty = Net::HTTPOK.new("1.1", "200", "OK")
|
42
|
+
@response_objects_list_empty.stubs(:body).returns(@response_objects_list_empty_body)
|
43
|
+
|
44
|
+
@response_objects_list_body = <<-EOObjects
|
45
|
+
<?xml version="1.0" encoding="UTF-8"?>\n<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <Name>bucket</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <IsTruncated>false</IsTruncated> <Contents> <Key>obj1</Key> <LastModified>2009-07-03T10:17:33.000Z</LastModified> <ETag>"99519cdf14c255e580e1b7bca85a458c"</ETag> <Size>1729</Size> <Owner> <ID>df864aeb6f42be43f1d9e60aaabe3f15e245b035a4b79d1cfe36c4deaec67205</ID> <DisplayName>owner</DisplayName> </Owner> <StorageClass>STANDARD</StorageClass> </Contents> <Contents> <Key>obj2</Key> <LastModified>2009-07-03T11:17:33.000Z</LastModified> <ETag>"99519cdf14c255e586e1b12bca85a458c"</ETag> <Size>179</Size> <Owner> <ID>df864aeb6f42be43f1d9e60aaabe3f17e247b037a4b79d1cfe36c4deaec67205</ID> <DisplayName>owner</DisplayName> </Owner> <StorageClass>STANDARD</StorageClass> </Contents> </ListBucketResult>
|
46
|
+
EOObjects
|
47
|
+
|
48
|
+
@response_objects_list = Net::HTTPOK.new("1.1", "200", "OK")
|
49
|
+
@response_objects_list.stubs(:body).returns(@response_objects_list_body)
|
50
|
+
end
|
51
|
+
|
52
|
+
test "name valid" do
|
53
|
+
assert_raise ArgumentError do S3::Bucket.send(:new, nil, "") end # should not be valid with empty name
|
54
|
+
assert_raise ArgumentError do S3::Bucket.send(:new, nil, "10.0.0.1") end # should not be valid with IP as name
|
55
|
+
assert_raise ArgumentError do S3::Bucket.send(:new, nil, "as") end # should not be valid with name shorter than 3 characters
|
56
|
+
assert_raise ArgumentError do S3::Bucket.send(:new, nil, "a" * 256) end # should not be valid with name longer than 255 characters
|
57
|
+
assert_raise ArgumentError do S3::Bucket.send(:new, nil, ".asdf") end # should not allow special characters as first character
|
58
|
+
assert_raise ArgumentError do S3::Bucket.send(:new, nil, "-asdf") end # should not allow special characters as first character
|
59
|
+
assert_raise ArgumentError do S3::Bucket.send(:new, nil, "_asdf") end # should not allow special characters as first character
|
60
|
+
|
61
|
+
assert_nothing_raised do
|
62
|
+
S3::Bucket.send(:new, nil, "a-a-")
|
63
|
+
S3::Bucket.send(:new, nil, "a.a.")
|
64
|
+
S3::Bucket.send(:new, nil, "a_a_")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
test "path prefix" do
|
69
|
+
expected = ""
|
70
|
+
actual = @bucket_vhost.path_prefix
|
71
|
+
assert_equal expected, actual
|
72
|
+
|
73
|
+
expected = "Data_Bucket/"
|
74
|
+
actual = @bucket_path.path_prefix
|
75
|
+
assert_equal expected, actual
|
76
|
+
end
|
77
|
+
|
78
|
+
test "host" do
|
79
|
+
expected = "Data-Bucket.s3.amazonaws.com"
|
80
|
+
actual = @bucket_vhost.host
|
81
|
+
assert_equal expected, actual
|
82
|
+
|
83
|
+
expected = "s3.amazonaws.com"
|
84
|
+
actual = @bucket_path.host
|
85
|
+
assert_equal expected, actual
|
86
|
+
end
|
87
|
+
|
88
|
+
test "vhost" do
|
89
|
+
assert @bucket_vhost.vhost?
|
90
|
+
assert ! @bucket_path.vhost?
|
91
|
+
end
|
92
|
+
|
93
|
+
test "exists" do
|
94
|
+
@bucket.expects(:retrieve).returns(@bucket_vhost)
|
95
|
+
assert @bucket.exists?
|
96
|
+
|
97
|
+
@bucket.expects(:retrieve).raises(S3::Error::NoSuchBucket.new(nil, nil))
|
98
|
+
assert ! @bucket.exists?
|
99
|
+
end
|
100
|
+
|
101
|
+
test "location and parse location" do
|
102
|
+
@bucket.expects(:bucket_request).with(:get, { :params => { :location => nil } }).returns(@response_location)
|
103
|
+
|
104
|
+
expected = @bucket_location
|
105
|
+
actual = @bucket.location
|
106
|
+
assert_equal expected, actual
|
107
|
+
|
108
|
+
@bucket.stubs(:bucket_request).with(:get, { :params => { :location => nil } })
|
109
|
+
actual = @bucket.location
|
110
|
+
assert_equal expected, actual
|
111
|
+
end
|
112
|
+
|
113
|
+
test "save" do
|
114
|
+
@bucket.expects(:bucket_request).with(:put, { :headers => {} })
|
115
|
+
assert @bucket.save
|
116
|
+
# mock ensures that bucket_request was called
|
117
|
+
end
|
118
|
+
|
119
|
+
test "save failure owned by you" do
|
120
|
+
@bucket.expects(:bucket_request).with(:put, { :headers => {} }).raises(S3::Error::BucketAlreadyOwnedByYou.new(409, @response_owned_by_you))
|
121
|
+
assert_raise S3::Error::BucketAlreadyOwnedByYou do
|
122
|
+
@bucket.save
|
123
|
+
end
|
124
|
+
|
125
|
+
@bucket.expects(:bucket_request).with(:put, { :headers => {} }).raises(S3::Error::BucketAlreadyExists.new(409, @response_already_exists))
|
126
|
+
assert_raise S3::Error::BucketAlreadyExists do
|
127
|
+
@bucket.save
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
test "objects" do
|
132
|
+
@bucket.expects(:list_bucket).returns(@objects_list_empty)
|
133
|
+
expected = @objects_list_empty
|
134
|
+
actual = @bucket.objects
|
135
|
+
assert_equal expected, actual
|
136
|
+
|
137
|
+
@bucket.stubs(:list_bucket).returns(@objects_list_empty)
|
138
|
+
actual = @bucket.objects
|
139
|
+
assert_equal expected, actual
|
140
|
+
|
141
|
+
@bucket.stubs(:list_bucket).returns(@objects_list)
|
142
|
+
|
143
|
+
expected = @objects_list
|
144
|
+
actual = @bucket.objects
|
145
|
+
assert_equal expected, actual
|
146
|
+
end
|
147
|
+
|
148
|
+
test "list bucket and parse objects" do
|
149
|
+
@bucket.expects(:bucket_request).with(:get, :params => { :test=>true }).returns(@response_objects_list_empty)
|
150
|
+
expected = @objects_list_empty
|
151
|
+
actual = @bucket.objects.find_all(:test => true)
|
152
|
+
assert_equal expected, actual
|
153
|
+
|
154
|
+
@bucket.expects(:bucket_request).with(:get, :params => { :test => true }).returns(@response_objects_list)
|
155
|
+
expected = @objects_list
|
156
|
+
actual = @bucket.objects.find_all(:test => true)
|
157
|
+
assert_equal expected, actual
|
158
|
+
end
|
159
|
+
|
160
|
+
test "destroy" do
|
161
|
+
@bucket.expects(:bucket_request).with(:delete)
|
162
|
+
assert @bucket.destroy
|
163
|
+
end
|
164
|
+
|
165
|
+
test "objects build" do
|
166
|
+
@bucket.stubs(:bucket_request)
|
167
|
+
|
168
|
+
expected = "object_name"
|
169
|
+
actual = @bucket.objects.build("object_name")
|
170
|
+
assert_kind_of S3::Object, actual
|
171
|
+
assert_equal expected, actual.key
|
172
|
+
end
|
173
|
+
|
174
|
+
test "objects find first" do
|
175
|
+
assert_nothing_raised do
|
176
|
+
S3::Object.any_instance.stubs(:retrieve).returns(S3::Object.send(:new, nil, :key => "obj2"))
|
177
|
+
expected = "obj2"
|
178
|
+
actual = @bucket.objects.find_first("obj2")
|
179
|
+
assert_equal "obj2", actual.key
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
test "objects find first fail" do
|
184
|
+
assert_raise S3::Error::NoSuchKey do
|
185
|
+
S3::Object.any_instance.stubs(:retrieve).raises(S3::Error::NoSuchKey.new(404, nil))
|
186
|
+
@bucket.objects.find_first("obj3")
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
test "objects find all on empty list" do
|
191
|
+
@bucket.stubs(:list_bucket).returns(@objects_list_empty)
|
192
|
+
assert_nothing_raised do
|
193
|
+
expected = @objects_list_empty
|
194
|
+
actual = @bucket.objects.find_all
|
195
|
+
assert_equal expected, actual
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
test "objects find all" do
|
200
|
+
@bucket.stubs(:list_bucket).returns(@objects_list)
|
201
|
+
assert_nothing_raised do
|
202
|
+
expected = @objects_list
|
203
|
+
actual = @bucket.objects.find_all
|
204
|
+
assert_equal expected, actual
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
test "objects destroy all" do
|
209
|
+
@bucket.stubs(:list_bucket).returns(@objects_list)
|
210
|
+
@bucket.objects.each do |obj|
|
211
|
+
obj.expects(:destroy)
|
212
|
+
end
|
213
|
+
@bucket.objects.destroy_all
|
214
|
+
end
|
215
|
+
end
|
@@ -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
|
data/test/object_test.rb
ADDED
@@ -0,0 +1,179 @@
|
|
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
|
+
|
15
|
+
@response_binary = Net::HTTPOK.new("1.1", "200", "OK")
|
16
|
+
@response_binary.stubs(:body).returns("test".respond_to?(:force_encoding) ? "test".force_encoding(Encoding::BINARY) : "test")
|
17
|
+
@response_binary["etag"] = ""
|
18
|
+
@response_binary["content-type"] = "image/png"
|
19
|
+
@response_binary["content-disposition"] = "inline"
|
20
|
+
@response_binary["content-encoding"] = nil
|
21
|
+
@response_binary["last-modified"] = Time.now.httpdate
|
22
|
+
@response_binary["content-length"] = 20
|
23
|
+
|
24
|
+
@xml_body = <<-EOXML
|
25
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
26
|
+
<CopyObjectResult> <LastModified>#{Time.now.httpdate}</LastModified> <ETag>"etag"</ETag> </CopyObjectResult>
|
27
|
+
EOXML
|
28
|
+
@response_xml = Net::HTTPOK.new("1.1", "200", "OK")
|
29
|
+
@response_xml.stubs(:body).returns(@xml_body)
|
30
|
+
end
|
31
|
+
|
32
|
+
test "initializing" do
|
33
|
+
assert_raise ArgumentError do S3::Object.send(:new, nil, :key => "") end # should not allow empty key
|
34
|
+
assert_raise ArgumentError do S3::Object.send(:new, nil, :key => "//") end # should not allow key with double slash
|
35
|
+
|
36
|
+
assert_nothing_raised do
|
37
|
+
S3::Object.send(:new, nil, :key => "Lena.png")
|
38
|
+
S3::Object.send(:new, nil, :key => "Lena playboy.png")
|
39
|
+
S3::Object.send(:new, nil, :key => "Lena Söderberg.png")
|
40
|
+
S3::Object.send(:new, nil, :key => "/images/pictures/test images/Lena not full.png")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
test "==" do
|
45
|
+
expected = false
|
46
|
+
actual = @object_lena == nil
|
47
|
+
assert_equal(expected, actual)
|
48
|
+
end
|
49
|
+
|
50
|
+
test "full key" do
|
51
|
+
expected = "images/Lena.png"
|
52
|
+
actual = @object_lena.full_key
|
53
|
+
assert_equal expected, actual
|
54
|
+
end
|
55
|
+
|
56
|
+
test "url" do
|
57
|
+
bucket1 = S3::Bucket.send(:new, @service, "images")
|
58
|
+
|
59
|
+
object11 = S3::Object.send(:new, bucket1, :key => "Lena.png")
|
60
|
+
expected = "http://images.s3.amazonaws.com/Lena.png"
|
61
|
+
actual = object11.url
|
62
|
+
assert_equal expected, actual
|
63
|
+
|
64
|
+
object12 = S3::Object.send(:new, bucket1, :key => "Lena Söderberg.png")
|
65
|
+
expected = "http://images.s3.amazonaws.com/Lena%20S%C3%B6derberg.png"
|
66
|
+
actual = object12.url
|
67
|
+
assert_equal expected, actual
|
68
|
+
|
69
|
+
bucket2 = S3::Bucket.send(:new, @service, "images_new")
|
70
|
+
|
71
|
+
object21 = S3::Object.send(:new, bucket2, :key => "Lena.png")
|
72
|
+
expected = "http://s3.amazonaws.com/images_new/Lena.png"
|
73
|
+
actual = object21.url
|
74
|
+
assert_equal expected, actual
|
75
|
+
end
|
76
|
+
|
77
|
+
test "cname url" do
|
78
|
+
bucket1 = S3::Bucket.send(:new, @service, "images.example.com")
|
79
|
+
|
80
|
+
object11 = S3::Object.send(:new, bucket1, :key => "Lena.png")
|
81
|
+
expected = "http://images.example.com/Lena.png"
|
82
|
+
actual = object11.cname_url
|
83
|
+
assert_equal expected, actual
|
84
|
+
|
85
|
+
object12 = S3::Object.send(:new, bucket1, :key => "Lena Söderberg.png")
|
86
|
+
expected = "http://images.example.com/Lena%20S%C3%B6derberg.png"
|
87
|
+
actual = object12.cname_url
|
88
|
+
assert_equal expected, actual
|
89
|
+
|
90
|
+
bucket2 = S3::Bucket.send(:new, @service, "images_new")
|
91
|
+
|
92
|
+
object21 = S3::Object.send(:new, bucket2, :key => "Lena.png")
|
93
|
+
expected = nil
|
94
|
+
actual = object21.cname_url
|
95
|
+
assert_equal expected, actual
|
96
|
+
end
|
97
|
+
|
98
|
+
test "destroy" do
|
99
|
+
@object_lena.expects(:object_request).with(:delete)
|
100
|
+
assert @object_lena.destroy
|
101
|
+
end
|
102
|
+
|
103
|
+
test "save" do
|
104
|
+
@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)
|
105
|
+
assert @object_lena.save
|
106
|
+
end
|
107
|
+
|
108
|
+
test "content and parse headers" do
|
109
|
+
@object_lena.expects(:object_request).with(:get, {}).returns(@response_binary)
|
110
|
+
|
111
|
+
expected = /test/n
|
112
|
+
actual = @object_lena.content(true)
|
113
|
+
assert_match expected, actual
|
114
|
+
assert_equal "image/png", @object_lena.content_type
|
115
|
+
|
116
|
+
assert @object_lena.content
|
117
|
+
|
118
|
+
@object_lena.expects(:object_request).with(:get, {}).returns(@response_binary)
|
119
|
+
assert @object_lena.content(true)
|
120
|
+
end
|
121
|
+
|
122
|
+
test "retrieve" do
|
123
|
+
@object_lena.expects(:object_request).with(:head, {}).returns(@response_binary)
|
124
|
+
assert @object_lena.retrieve
|
125
|
+
end
|
126
|
+
|
127
|
+
test "exists" do
|
128
|
+
@object_lena.expects(:retrieve).returns(true)
|
129
|
+
assert @object_lena.exists?
|
130
|
+
|
131
|
+
@object_carmen.expects(:retrieve).raises(S3::Error::NoSuchKey.new(nil, nil))
|
132
|
+
assert ! @object_carmen.exists?
|
133
|
+
end
|
134
|
+
|
135
|
+
test "ACL writer" do
|
136
|
+
expected = nil
|
137
|
+
actual = @object_lena.acl
|
138
|
+
assert_equal expected, actual
|
139
|
+
|
140
|
+
assert @object_lena.acl = :public_read
|
141
|
+
|
142
|
+
expected = "public-read"
|
143
|
+
actual = @object_lena.acl
|
144
|
+
assert_equal expected, actual
|
145
|
+
|
146
|
+
assert @object_lena.acl = :private
|
147
|
+
|
148
|
+
expected = "private"
|
149
|
+
actual = @object_lena.acl
|
150
|
+
assert_equal expected, actual
|
151
|
+
end
|
152
|
+
|
153
|
+
test "storage-class writer" do
|
154
|
+
expected = nil
|
155
|
+
actual = @object_lena.storage_class
|
156
|
+
assert_equal expected, actual
|
157
|
+
|
158
|
+
assert @object_lena.storage_class = :standard
|
159
|
+
|
160
|
+
expected = "STANDARD"
|
161
|
+
actual = @object_lena.storage_class
|
162
|
+
assert_equal expected, actual
|
163
|
+
|
164
|
+
assert @object_lena.storage_class = :reduced_redundancy
|
165
|
+
|
166
|
+
expected = "REDUCED_REDUNDANCY"
|
167
|
+
actual = @object_lena.storage_class
|
168
|
+
assert_equal expected, actual
|
169
|
+
end
|
170
|
+
|
171
|
+
test "copy" do
|
172
|
+
@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)
|
173
|
+
|
174
|
+
new_object = @object_lena.copy(:key => "Lena-copy.png")
|
175
|
+
|
176
|
+
assert_equal "Lena-copy.png", new_object.key
|
177
|
+
assert_equal "Lena.png", @object_lena.key
|
178
|
+
end
|
179
|
+
end
|