cloudfiles-sagamore 1.5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ module CloudFiles
2
+ VERSION = '1.5.0.1'
3
+ end
@@ -0,0 +1,157 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Major's live tests that go against the real Cloud Files system. Requires a valid username and API key to function.
4
+
5
+ require File.dirname(__FILE__) + '/../lib/cloudfiles'
6
+
7
+ username = "YOUR_USERNAME"
8
+ apikey = "YOUR_API_KEY"
9
+
10
+ def assert_test(testtext,bool)
11
+ booltext = (bool)? " PASS" : "*FAIL*" ;
12
+ (testtext+"... ").ljust(50)+booltext
13
+ end
14
+
15
+ filename = File.dirname(__FILE__) + '/../lib/cloudfiles.rb'
16
+
17
+ # Test initial connection
18
+ cf = CloudFiles::Connection.new(username,apikey)
19
+ puts assert_test("Connecting to CloudFiles",cf.class == CloudFiles::Connection)
20
+
21
+ # Test container creation
22
+ testingcontainer = "RubyCFTest"
23
+ cntnr = cf.create_container(testingcontainer)
24
+ puts assert_test("Creating test container",cntnr.class == CloudFiles::Container)
25
+
26
+ # Checking container size
27
+ bytes = cntnr.bytes
28
+ puts assert_test(" Checking container size",bytes == 0)
29
+
30
+ # Checking container count
31
+ count = cntnr.count
32
+ puts assert_test(" Checking container count",count == 0)
33
+
34
+ # Add a file to the container - standard method
35
+ cloudfilesfilesize = File.read(filename).length
36
+ cloudfilesmd5 = Digest::MD5.hexdigest(File.read(filename))
37
+ headers = { "ETag" => cloudfilesmd5, "Content-Type" => "text/ruby", "X-Object-Meta-Testmeta" => "value" }
38
+ myobj = cntnr.create_object("cloudfiles-standard.rb")
39
+ myobj.write(File.read(filename), headers)
40
+ puts assert_test(" Uploading object (read into memory)",myobj.class == CloudFiles::StorageObject)
41
+ cntnr.refresh
42
+
43
+ # Check if object exists
44
+ bool = cntnr.object_exists?("cloudfiles-standard.rb")
45
+ puts assert_test(" Checking for object existence",bool)
46
+
47
+ # Checking container size
48
+ bytes = cntnr.bytes
49
+ puts assert_test(" Checking container size with #{bytes} vs #{cloudfilesfilesize}",bytes == cloudfilesfilesize)
50
+
51
+ # Checking container count
52
+ count = cntnr.count
53
+ puts assert_test(" Checking container count",count == 1)
54
+
55
+ # Add a file to the container - stream method
56
+ headers = { "ETag" => cloudfilesmd5, "Content-Type" => "text/ruby", "X-Object-Meta-Testmeta" => "value" }
57
+ f = IO.read(filename)
58
+ myobj = cntnr.create_object("cloudfiles-stream.rb")
59
+ myobj.write(File.read(filename), headers)
60
+ puts assert_test(" Uploading object (read from stream)",myobj.class == CloudFiles::StorageObject)
61
+ cntnr.refresh
62
+
63
+ # Check if object exists
64
+ bool = cntnr.object_exists?("cloudfiles-stream.rb")
65
+ puts assert_test(" Checking for object existence",bool)
66
+
67
+ # Checking container size
68
+ bytes = cntnr.bytes
69
+ puts assert_test(" Checking container size",bytes == (cloudfilesfilesize*2))
70
+
71
+ # Checking container count
72
+ count = cntnr.count
73
+ puts assert_test(" Checking container count",count == 2)
74
+
75
+ # Check file size
76
+ bytes = myobj.bytes.to_i
77
+ puts assert_test(" Checking object size",bytes == cloudfilesfilesize)
78
+
79
+ # Check content type
80
+ content_type = myobj.content_type
81
+ puts assert_test(" Checking object content type",content_type == "text/ruby")
82
+
83
+ # Check metadata
84
+ metadata = myobj.metadata
85
+ puts assert_test(" Checking object metadata",metadata["testmeta"] == "value")
86
+
87
+ # Set new metadata
88
+ bool = myobj.set_metadata({ "testmeta2" => "differentvalue"})
89
+ puts assert_test(" Setting new object metadata",bool)
90
+
91
+ # Check new metadata
92
+ myobj.refresh
93
+ metadata = myobj.metadata
94
+ puts assert_test(" Checking new object metadata",metadata["testmeta2"] == "differentvalue")
95
+
96
+ # Get data via standard method
97
+ data = myobj.data
98
+ puts assert_test(" Retrieving object data (read into memory)",Digest::MD5.hexdigest(data) == cloudfilesmd5)
99
+
100
+ # Get data via stream
101
+ data = ""
102
+ myobj.data_stream { |chunk|
103
+ data += chunk.to_s
104
+ }
105
+ puts assert_test(" Retrieving object data (read from stream)",Digest::MD5.hexdigest(data) == cloudfilesmd5)
106
+
107
+ # Check md5sum
108
+ etag = myobj.etag
109
+ puts assert_test(" Checking object's md5sum",etag == cloudfilesmd5)
110
+
111
+ # Make container public
112
+ bool = cntnr.make_public
113
+ puts assert_test(" Making container public",bool)
114
+
115
+ # Verify that container is public
116
+ bool = cntnr.public?
117
+ puts assert_test(" Verifying container is public",bool)
118
+
119
+ # Getting CDN URL
120
+ cdnurl = cntnr.cdn_url
121
+ puts assert_test(" Getting CDN URL",cdnurl)
122
+
123
+ # Setting CDN URL
124
+ bool = cntnr.make_public(:ttl => 7200)
125
+ puts assert_test(" Setting CDN TTL",bool)
126
+
127
+ # Make container private
128
+ bool = cntnr.make_private
129
+ puts assert_test(" Making container private",bool)
130
+
131
+ # Check if container is empty
132
+ bool = cntnr.empty?
133
+ puts assert_test(" Checking if container empty",bool == false)
134
+
135
+ # Remove standard object
136
+ bool = cntnr.delete_object("cloudfiles-standard.rb")
137
+ puts assert_test(" Deleting first object",bool)
138
+
139
+ # Remove stream object
140
+ bool = cntnr.delete_object("cloudfiles-stream.rb")
141
+ puts assert_test(" Deleting second object",bool)
142
+ cntnr.refresh
143
+
144
+ # Check if container is empty
145
+ bool = cntnr.empty?
146
+ puts assert_test(" Checking if container empty",bool)
147
+
148
+ # Remove testing container
149
+ bool = cf.delete_container(testingcontainer)
150
+ puts assert_test("Removing container",bool)
151
+
152
+ # Check to see if container exists
153
+ bool = cf.container_exists?(testingcontainer)
154
+ puts assert_test("Checking container existence",bool == false)
155
+
156
+
157
+
@@ -0,0 +1,44 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'test_helper'
3
+
4
+ class CloudfilesAuthenticationTest < Test::Unit::TestCase
5
+ def test_good_authentication
6
+ response = ['http://cdn.example.com/storage', 'dummy_token', {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token'}]
7
+ SwiftClient.stubs(:get_auth).returns(response)
8
+ @connection = stub(:authuser => 'dummy_user', :authkey => 'dummy_key', :cdnmgmthost= => true, :cdnmgmtpath= => true, :cdnmgmtport= => true, :cdnmgmtscheme= => true, :storagehost= => true, :storagepath= => true, :storageport= => true, :storagescheme= => true, :authtoken= => true, :authok= => true, :snet? => false, :auth_url => 'https://auth.api.rackspacecloud.com/v1.0', :cdn_available? => true, :cdn_available= => true, :snet => false)
9
+ result = CloudFiles::Authentication.new(@connection)
10
+ assert_equal result.class, CloudFiles::Authentication
11
+ end
12
+
13
+ def test_snet_authentication
14
+ response = ['http://cdn.example.com/storage', 'dummy_token', {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token'}]
15
+ SwiftClient.stubs(:get_auth).returns(response)
16
+ @connection = stub(:authuser => 'dummy_user', :authkey => 'dummy_key', :cdnmgmthost= => true, :cdnmgmtpath= => true, :cdnmgmtport= => true, :cdnmgmtscheme= => true, :storagehost= => true, :storagepath= => true, :storageport= => true, :storagescheme= => true, :authtoken= => true, :authok= => true, :snet? => true, :auth_url => 'https://auth.api.rackspacecloud.com/v1.0', :cdn_available? => true, :cdn_available= => true, :snet => true)
17
+ result = CloudFiles::Authentication.new(@connection)
18
+ assert_equal result.class, CloudFiles::Authentication
19
+ end
20
+
21
+ def test_bad_authentication
22
+ SwiftClient.stubs(:get_auth).returns(nil)
23
+ @connection = stub(:authuser => 'bad_user', :authkey => 'bad_key', :authok= => true, :authtoken= => true, :auth_url => 'https://auth.api.rackspacecloud.com/v1.0', :cdn_available? => true, :snet? => false)
24
+ assert_raises(CloudFiles::Exception::Authentication) do
25
+ result = CloudFiles::Authentication.new(@connection)
26
+ end
27
+ end
28
+
29
+ def test_bad_hostname
30
+ Net::HTTP.stubs(:new).raises(CloudFiles::Exception::Connection)
31
+ @connection = stub(:proxy_host => nil, :proxy_port => nil, :authuser => 'bad_user', :authkey => 'bad_key', :authok= => true, :authtoken= => true, :auth_url => 'https://auth.api.rackspacecloud.com/v1.0', :cdn_available? => true, :snet? => false)
32
+ assert_raises(CloudFiles::Exception::Connection) do
33
+ result = CloudFiles::Authentication.new(@connection)
34
+ end
35
+ end
36
+
37
+ def test_authentication_general_exception
38
+ SwiftClient.stubs(:get_auth).raises(ClientException.new('foobar'))
39
+ @connection = stub(:proxy_host => nil, :proxy_port => nil, :authuser => 'bad_user', :authkey => 'bad_key', :authok= => true, :authtoken= => true, :auth_url => 'https://auth.api.rackspacecloud.com/v1.0', :cdn_available? => true, :snet? => false)
40
+ assert_raises(CloudFiles::Exception::Connection) do
41
+ result = CloudFiles::Authentication.new(@connection)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,797 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'test_helper'
3
+
4
+ class SwiftClientTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @url = "http://foo.bar:1234/auth/v1.0"
8
+ @user = "foo_user"
9
+ @key = "foo_key"
10
+ @token = 'foo_token'
11
+ Net::HTTP.any_instance.stubs(:new).stubs({:port => 1234, :address => 'foo.bar', :class => Net::HTTP})
12
+ @parsed, @conn = SwiftClient.http_connection(@url)
13
+ end
14
+
15
+ def test_client_exception
16
+ foo = ClientException.new("foobar", :http_status => "404")
17
+ assert_equal "foobar 404", foo.to_s
18
+ end
19
+
20
+ def test_chunked_connection_wrapper
21
+ file = mock("File")
22
+ file.stubs(:read).returns("this ", "is so", "me da", "ta!", "")
23
+ file.stubs(:eof?).returns(true)
24
+ file.stubs(:eof!).returns(true)
25
+ chunk = ChunkedConnectionWrapper.new(file, 5)
26
+ assert_equal "this ", chunk.read(123)
27
+ assert_equal "is so", chunk.read(123)
28
+ assert_equal "me da", chunk.read(123)
29
+ assert_equal "ta!", chunk.read(123)
30
+ assert_equal true, chunk.eof?
31
+ assert_equal true, chunk.eof!
32
+ end
33
+
34
+ def test_query
35
+ query = Query.new("foo=bar&baz=quu")
36
+ query.add("chunky", "bacon")
37
+ assert_match /chunky=bacon/, query.to_s
38
+ assert_match /foo=bar/, query.to_s
39
+ assert_match /baz=quu/, query.to_s
40
+ assert query.has_key? "chunky"
41
+ query.delete("chunky")
42
+ assert_equal false, query.has_key?("chunky")
43
+ end
44
+
45
+ def test_http_connection
46
+ parsed, conn = SwiftClient.http_connection("http://foo.bar:1234/auth/v1.0")
47
+ assert_equal 'http', parsed.scheme
48
+ assert_equal 1234, parsed.port
49
+ assert_equal 'foo.bar', parsed.host
50
+ assert_equal '/auth/v1.0', parsed.path
51
+ assert_equal 'foo.bar', conn.address
52
+ assert_equal 1234, conn.port
53
+ assert_equal Net::HTTP, conn.class
54
+ end
55
+
56
+ def test_http_connection_with_ssl
57
+ parsed, conn = SwiftClient.http_connection("https://foo.bar:443/auth/v1.0")
58
+ assert_equal 'https', parsed.scheme
59
+ assert_equal 443, parsed.port
60
+ assert_equal 'foo.bar', parsed.host
61
+ assert_equal '/auth/v1.0', parsed.path
62
+ assert_equal 'foo.bar', conn.address
63
+ assert_equal 443, conn.port
64
+ assert_equal Net::HTTP, conn.class
65
+ assert_equal true, conn.use_ssl?
66
+ assert_equal OpenSSL::SSL::VERIFY_NONE, conn.verify_mode
67
+ end
68
+
69
+ def test_http_connection_with_bad_scheme
70
+ assert_raises(ClientException) do
71
+ parsed, conn = SwiftClient.http_connection("ftp://foo.bar:443/auth/v1.0")
72
+ end
73
+ end
74
+
75
+ def test_auth
76
+ response = stub(
77
+ :code => "200",
78
+ :header => {'x-storage-url' => 'http://foo.bar:1234/v1/AUTH_test', 'x-storage-token' => 'AUTH_test', 'x-auth-token' => 'AUTH_test', 'content-length' => 0, 'date' => 'Tue, 11 Oct 2011 20:54:06 GMT'}
79
+ )
80
+ conn = mock("Net::HTTP")
81
+ conn.stubs(:started?).returns(true)
82
+ conn.stubs(:get).returns(response)
83
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
84
+ assert_nothing_raised do
85
+ storage_url, token, headers = SwiftClient.get_auth(@url, @user, @key)
86
+ assert_equal "http://foo.bar:1234/v1/AUTH_test", storage_url
87
+ assert_equal 'AUTH_test', token
88
+ end
89
+ end
90
+
91
+ def test_auth_with_snet
92
+ response = stub(
93
+ :code => "200",
94
+ :header => {'x-storage-url' => 'http://foo.bar:1234/v1/AUTH_test', 'x-storage-token' => 'AUTH_test', 'x-auth-token' => 'AUTH_test', 'content-length' => 0, 'date' => 'Tue, 11 Oct 2011 20:54:06 GMT'}
95
+ )
96
+ conn = mock("Net::HTTP")
97
+ conn.stubs(:started?).returns(true)
98
+ conn.stubs(:get).returns(response)
99
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
100
+ assert_nothing_raised do
101
+ storage_url, token, headers = SwiftClient.get_auth(@url, @user, @key, true)
102
+ assert_equal "http://snet-foo.bar:1234/v1/AUTH_test", storage_url
103
+ assert_equal 'AUTH_test', token
104
+ end
105
+ end
106
+
107
+ def test_auth_fails
108
+ response = stub(:code => '500', :message => "Internal Server Error")
109
+ conn = mock("Net::HTTP")
110
+ conn.stubs(:address).returns('foobar.com')
111
+ conn.stubs(:port).returns(123)
112
+ conn.stubs(:started?).returns(false)
113
+ conn.stubs(:start).returns(nil)
114
+ conn.stubs(:get).returns(response)
115
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
116
+
117
+ assert_raise(ClientException) do
118
+ SwiftClient.get_auth(@url, @user, @key)
119
+ end
120
+ end
121
+
122
+ #test HEAD account
123
+ def test_head_account
124
+ response = stub(
125
+ :code => "200",
126
+ :header => {'x-account-object-count' => 123, 'x-account-bytes-used' => 123456, 'x-account-container-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
127
+ )
128
+ conn = mock("Net::HTTP")
129
+ conn.stubs(:started?).returns(true)
130
+ conn.stubs(:head).returns(response)
131
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
132
+ assert_nothing_raised do
133
+ headers = SwiftClient.head_account(@url, @token)
134
+ assert_equal headers['x-account-object-count'], 123
135
+ assert_equal headers['x-account-bytes-used'], 123456
136
+ assert_equal headers['x-account-container-count'], 12
137
+ end
138
+ end
139
+
140
+ def test_head_account_fail
141
+ response = stub(
142
+ :code => "500",
143
+ :message => "Internal Error"
144
+ )
145
+ conn = mock("Net::HTTP")
146
+ conn.stubs(:address).returns("foobar.com")
147
+ conn.stubs(:port).returns(123)
148
+ conn.stubs(:started?).returns(true)
149
+ conn.stubs(:head).returns(response)
150
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
151
+ assert_raise(ClientException) do
152
+ headers = SwiftClient.head_account(@url, @token)
153
+ end
154
+ end
155
+
156
+ #test GET account
157
+ def test_get_account
158
+ response = stub(
159
+ :code => "200",
160
+ :body => '[ { "name":".CDN_ACCESS_LOGS", "count":1, "bytes":1234 }, { "name":"First", "count":2, "bytes":2345 }, { "name":"Second", "count":3, "bytes":3456 }, { "name":"Third", "count":4, "bytes":4567 } ]',
161
+ :header => {'x-account-object-count' => 123, 'x-account-bytes-used' => 123456, 'x-account-container-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
162
+ )
163
+ conn = mock("Net::HTTP")
164
+ conn.stubs(:started?).returns(true)
165
+ conn.stubs(:get).returns(response)
166
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
167
+ assert_nothing_raised do
168
+ headers, result = SwiftClient.get_account(@url, @token)
169
+ assert_equal headers, response.header
170
+ assert_equal result, JSON.parse(response.body)
171
+ end
172
+ end
173
+
174
+ def test_get_account_no_content
175
+ response = stub(
176
+ :code => "204",
177
+ :body => nil,
178
+ :header => {'x-account-object-count' => 123, 'x-account-bytes-used' => 123456, 'x-account-container-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
179
+ )
180
+ conn = mock("Net::HTTP")
181
+ conn.stubs(:started?).returns(true)
182
+ conn.stubs(:get).returns(response)
183
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
184
+ assert_nothing_raised do
185
+ headers, result = SwiftClient.get_account(@url, @token)
186
+ assert_equal headers, response.header
187
+ assert_equal result, []
188
+ end
189
+ end
190
+
191
+ def test_get_account_marker
192
+ response = stub(
193
+ :code => "200",
194
+ :body => '[ { "name":"Second", "count":3, "bytes":3456 }, { "name":"Third", "count":4, "bytes":4567 } ]',
195
+ :header => {'x-account-object-count' => 123, 'x-account-bytes-used' => 123456, 'x-account-container-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
196
+ )
197
+ conn = mock("Net::HTTP")
198
+ conn.stubs(:started?).returns(true)
199
+ conn.stubs(:get).returns(response)
200
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
201
+ assert_nothing_raised do
202
+ headers, result = SwiftClient.get_account(@url, @token, "First")
203
+ assert_equal headers, response.header
204
+ assert_equal result, JSON.parse(response.body)
205
+ end
206
+ end
207
+
208
+ def test_get_account_limit
209
+ response = stub(
210
+ :code => "200",
211
+ :body => '[ { "name":".CDN_ACCESS_LOGS", "count":1, "bytes":1234 }, { "name":"First", "count":2, "bytes":2345 } ]',
212
+ :header => {'x-account-object-count' => 123, 'x-account-bytes-used' => 123456, 'x-account-container-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
213
+ )
214
+ conn = mock("Net::HTTP")
215
+ conn.stubs(:started?).returns(true)
216
+ conn.stubs(:get).returns(response)
217
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
218
+ assert_nothing_raised do
219
+ headers, result = SwiftClient.get_account(@url, @token, nil, 2)
220
+ assert_equal headers, response.header
221
+ assert_equal result, JSON.parse(response.body)
222
+ end
223
+ end
224
+
225
+ def test_get_account_prefix
226
+ response = stub(
227
+ :code => "200",
228
+ :body => '[ { "name":"First", "count":2, "bytes":2345 } ]',
229
+ :header => {'x-account-object-count' => 123, 'x-account-bytes-used' => 123456, 'x-account-container-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
230
+ )
231
+ conn = mock("Net::HTTP")
232
+ conn.stubs(:started?).returns(true)
233
+ conn.stubs(:get).returns(response)
234
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
235
+ assert_nothing_raised do
236
+ headers, result = SwiftClient.get_account(@url, @token, nil, nil, "F")
237
+ assert_equal headers, response.header
238
+ assert_equal result, JSON.parse(response.body)
239
+ end
240
+ end
241
+
242
+ def test_get_account_full_listing
243
+ response1 = stub(
244
+ :code => "200",
245
+ :body => '[ { "name":".CDN_ACCESS_LOGS", "count":1, "bytes":1234 }, { "name":"First", "count":2, "bytes":2345 }, { "name":"Second", "count":3, "bytes":3456 }, { "name":"Third", "count":4, "bytes":4567 } ]',
246
+ :header => {'x-account-object-count' => 123, 'x-account-bytes-used' => 123456, 'x-account-container-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
247
+ )
248
+ response2 = stub(
249
+ :code => "200",
250
+ :body => '[ { "name":"Fourth", "count":1, "bytes":1234 }, { "name":"Fifth", "count":2, "bytes":2345 }, { "name":"Sixth", "count":3, "bytes":3456 }, { "name":"Seventh", "count":4, "bytes":4567 } ]',
251
+ :header => {'x-account-object-count' => 123, 'x-account-bytes-used' => 123456, 'x-account-container-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
252
+ )
253
+ response3 = stub(
254
+ :code => "200",
255
+ :body => '[]',
256
+ :header => {'x-account-object-count' => 123, 'x-account-bytes-used' => 123456, 'x-account-container-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
257
+ )
258
+
259
+ conn = mock("Net::HTTP")
260
+ conn.stubs(:started?).returns(true)
261
+ conn.stubs(:get).at_least(3).at_most(3).returns(response1, response2, response3)
262
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
263
+ assert_nothing_raised do
264
+ headers, result = SwiftClient.get_account(@url, @token, nil, nil, nil, nil, true)
265
+ assert_equal headers, response1.header
266
+ assert_equal JSON.parse(response1.body) + JSON.parse(response2.body) + JSON.parse(response3.body), result
267
+ end
268
+ end
269
+
270
+ def test_get_account_fail
271
+ response = stub(
272
+ :code => "500",
273
+ :message => "Internal Error"
274
+ )
275
+ conn = mock("Net::HTTP")
276
+ conn.stubs(:address).returns("foobar.com")
277
+ conn.stubs(:port).returns(123)
278
+ conn.stubs(:started?).returns(true)
279
+ conn.stubs(:get).returns(response)
280
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
281
+ assert_raise(ClientException) do
282
+ headers = SwiftClient.get_account(@url, @token)
283
+ end
284
+ end
285
+
286
+ #test POST account
287
+ def test_post_account
288
+ response = stub(:code => 204, :body => nil)
289
+ conn = mock("Net::HTTP")
290
+ conn.stubs(:started?).returns(true)
291
+ conn.stubs(:post).returns(response)
292
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
293
+ assert_nothing_raised do
294
+ SwiftClient.post_account(@url, @token, {"foo" => "bar"})
295
+ end
296
+ end
297
+
298
+ def test_post_account_fail
299
+ response = stub(
300
+ :code => "500",
301
+ :message => "Internal Error"
302
+ )
303
+ conn = mock("Net::HTTP")
304
+ conn.stubs(:address).returns("foobar.com")
305
+ conn.stubs(:port).returns(123)
306
+ conn.stubs(:started?).returns(true)
307
+ conn.stubs(:post).returns(response)
308
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
309
+ assert_raise(ClientException) do
310
+ headers = SwiftClient.post_account(@url, @token, {"foo" => "bar"})
311
+ end
312
+ end
313
+
314
+ #test HEAD container
315
+ def test_head_container
316
+ response = stub(
317
+ :code => "204",
318
+ :header => {'x-container-object-count' => 6, 'x-container-meta-baz' => 'que', 'x-container-meta-foo' => 'bar', 'x-container-bytes-used' => 123456, 'accept-ranges' => 'bytes', 'content-length' => 83, 'content-type' => 'text/plain; charset=utf-8', 'x-trans-id' => 'txfoo123', 'date' => 'Thu, 13 Oct 2011 22:29:14 GMT'}
319
+ )
320
+ conn = mock("Net::HTTP")
321
+ conn.stubs(:started?).returns(true)
322
+ conn.stubs(:head).returns(response)
323
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
324
+ assert_nothing_raised do
325
+ headers = SwiftClient.head_container(@url, @token, 'test_container')
326
+ assert_equal headers, response.header
327
+ end
328
+ end
329
+
330
+ def test_head_container_fail
331
+ response = stub(:code => "500", :message => "failed")
332
+ conn = mock("Net::HTTP")
333
+ conn.stubs(:address).returns("foobar.com")
334
+ conn.stubs(:port).returns(123)
335
+ conn.stubs(:started?).returns(true)
336
+ conn.stubs(:head).returns(response)
337
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
338
+
339
+ assert_raise(ClientException) do
340
+ headers, result = SwiftClient.head_container(@url, @token, 'test_container')
341
+ end
342
+ end
343
+
344
+ #test GET container
345
+ def test_get_container
346
+ response = stub(
347
+ :code => "200",
348
+ :header => {'x-container-object-count' => 6, 'x-container-meta-baz' => 'que', 'x-container-meta-foo' => 'bar', 'x-container-bytes-used' => 123456, 'accept-ranges' => 'bytes', 'content-length' => 83, 'content-type' => 'text/plain; charset=utf-8', 'x-trans-id' => 'txfoo123', 'date' => 'Thu, 13 Oct 2011 22:29:14 GMT'},
349
+ :body => '[ { "name":"foo.mp4", "hash":"foo_hash", "bytes":1234567, "content_type":"video/mp4", "last_modified":"2011-06-21T19:49:06.607670" }, { "name":"bar.ogv", "hash":"bar_hash", "bytes":987654, "content_type":"video/ogg", "last_modified":"2011-06-21T19:48:57.504050" }, { "name":"baz.webm", "hash":"baz_hash", "bytes":1239874, "content_type":"application/octet-stream", "last_modified":"2011-06-21T19:48:43.923990" }, { "name":"fobarbaz.html", "hash":"foobarbaz_hash", "bytes":12893467, "content_type":"text/html", "last_modified":"2011-06-21T19:54:36.555070" } ]'
350
+ )
351
+ conn = mock("Net::HTTP")
352
+ conn.stubs(:started?).returns(true)
353
+ conn.stubs(:get).returns(response)
354
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
355
+ assert_nothing_raised do
356
+ headers, result = SwiftClient.get_container(@url, @token, 'test_container')
357
+ assert_equal headers, response.header
358
+ assert_equal result, JSON.parse(response.body)
359
+ end
360
+ end
361
+
362
+
363
+ def test_get_container_no_content
364
+ response = stub(
365
+ :code => "204",
366
+ :body => nil,
367
+ :header => {'x-container-object-count' => 123, 'x-container-bytes-used' => 123456, 'x-container-object-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
368
+ )
369
+ conn = mock("Net::HTTP")
370
+ conn.stubs(:started?).returns(true)
371
+ conn.stubs(:get).returns(response)
372
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
373
+ assert_nothing_raised do
374
+ headers, result = SwiftClient.get_container(@url, @token, 'test_container')
375
+ assert_equal headers, response.header
376
+ assert_equal result, []
377
+ end
378
+ end
379
+
380
+ def test_get_container_marker
381
+ response = stub(
382
+ :code => "200",
383
+ :body => '[ { "name":"bar.ogv", "hash":"bar_hash", "bytes":987654, "content_type":"video/ogg", "last_modified":"2011-06-21T19:48:57.504050" }, { "name":"baz.webm", "hash":"baz_hash", "bytes":1239874, "content_type":"application/octet-stream", "last_modified":"2011-06-21T19:48:43.923990" }, { "name":"fobarbaz.html", "hash":"foobarbaz_hash", "bytes":12893467, "content_type":"text/html", "last_modified":"2011-06-21T19:54:36.555070" } ]',
384
+ :header => {'x-container-object-count' => 123, 'x-container-bytes-used' => 123456, 'x-container-object-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
385
+ )
386
+ conn = mock("Net::HTTP")
387
+ conn.stubs(:started?).returns(true)
388
+ conn.stubs(:get).returns(response)
389
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
390
+ assert_nothing_raised do
391
+ headers, result = SwiftClient.get_container(@url, @token, "test_container", "foo.mp4")
392
+ assert_equal headers, response.header
393
+ assert_equal result, JSON.parse(response.body)
394
+ end
395
+ end
396
+
397
+ def test_get_container_limit
398
+ response = stub(
399
+ :code => "200",
400
+ :body => '[ { "name":"foo.mp4", "hash":"foo_hash", "bytes":1234567, "content_type":"video/mp4", "last_modified":"2011-06-21T19:49:06.607670" }, { "name":"bar.ogv", "hash":"bar_hash", "bytes":987654, "content_type":"video/ogg", "last_modified":"2011-06-21T19:48:57.504050" } ]',
401
+ :header => {'x-container-object-count' => 123, 'x-container-bytes-used' => 123456, 'x-container-object-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
402
+ )
403
+ conn = mock("Net::HTTP")
404
+ conn.stubs(:started?).returns(true)
405
+ conn.stubs(:get).returns(response)
406
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
407
+ assert_nothing_raised do
408
+ headers, result = SwiftClient.get_container(@url, @token, 'test_container', nil, 2)
409
+ assert_equal headers, response.header
410
+ assert_equal result, JSON.parse(response.body)
411
+ assert_equal result.length, 2
412
+ end
413
+ end
414
+
415
+ def test_get_container_prefix
416
+ response = stub(
417
+ :code => "200",
418
+ :body => '[ { "name":"bar.ogv", "hash":"bar_hash", "bytes":987654, "content_type":"video/ogg", "last_modified":"2011-06-21T19:48:57.504050" }, { "name":"baz.webm", "hash":"baz_hash", "bytes":1239874, "content_type":"application/octet-stream", "last_modified":"2011-06-21T19:48:43.923990" }]',
419
+ :header => {'x-container-object-count' => 123, 'x-container-bytes-used' => 123456, 'x-container-object-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'}
420
+ )
421
+ conn = mock("Net::HTTP")
422
+ conn.stubs(:started?).returns(true)
423
+ conn.stubs(:get).returns(response)
424
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
425
+ assert_nothing_raised do
426
+ headers, result = SwiftClient.get_container(@url, @token, 'test_container', nil, nil, "b")
427
+ assert_equal headers, response.header
428
+ assert_equal result, JSON.parse(response.body)
429
+ end
430
+ end
431
+
432
+ def test_get_container_full_listing
433
+ response1 = stub(
434
+ :code => "200",
435
+ :header => {'x-container-object-count' => 6, 'x-container-meta-baz' => 'que', 'x-container-meta-foo' => 'bar', 'x-container-bytes-used' => 123456, 'accept-ranges' => 'bytes', 'content-length' => 83, 'content-type' => 'text/plain; charset=utf-8', 'x-trans-id' => 'txfoo123', 'date' => 'Thu, 13 Oct 2011 22:29:14 GMT'},
436
+ :body => '[ { "name":"foo.mp4", "hash":"foo_hash", "bytes":1234567, "content_type":"video/mp4", "last_modified":"2011-06-21T19:49:06.607670" }, { "name":"bar.ogv", "hash":"bar_hash", "bytes":987654, "content_type":"video/ogg", "last_modified":"2011-06-21T19:48:57.504050" }, { "name":"baz.webm", "hash":"baz_hash", "bytes":1239874, "content_type":"application/octet-stream", "last_modified":"2011-06-21T19:48:43.923990" }, { "name":"fobarbaz.html", "hash":"foobarbaz_hash", "bytes":12893467, "content_type":"text/html", "last_modified":"2011-06-21T19:54:36.555070" } ]'
437
+ )
438
+ response2 = stub(
439
+ :code => "200",
440
+ :header => {'x-container-object-count' => 6, 'x-container-meta-baz' => 'que', 'x-container-meta-foo' => 'bar', 'x-container-bytes-used' => 123456, 'accept-ranges' => 'bytes', 'content-length' => 83, 'content-type' => 'text/plain; charset=utf-8', 'x-trans-id' => 'txfoo123', 'date' => 'Thu, 13 Oct 2011 22:29:14 GMT'},
441
+ :body => '[ { "name":"foo2.mp4", "hash":"foo_hash", "bytes":1234567, "content_type":"video/mp4", "last_modified":"2011-06-21T19:49:06.607670" }, { "name":"bar2.ogv", "hash":"bar_hash", "bytes":987654, "content_type":"video/ogg", "last_modified":"2011-06-21T19:48:57.504050" }, { "name":"baz2.webm", "hash":"baz_hash", "bytes":1239874, "content_type":"application/octet-stream", "last_modified":"2011-06-21T19:48:43.923990" }, { "name":"fobarbaz2.html", "hash":"foobarbaz_hash", "bytes":12893467, "content_type":"text/html", "last_modified":"2011-06-21T19:54:36.555070" } ]'
442
+ )
443
+ response3 = stub(
444
+ :code => "200",
445
+ :header => {'x-container-object-count' => 6, 'x-container-meta-baz' => 'que', 'x-container-meta-foo' => 'bar', 'x-container-bytes-used' => 123456, 'accept-ranges' => 'bytes', 'content-length' => 83, 'content-type' => 'text/plain; charset=utf-8', 'x-trans-id' => 'txfoo123', 'date' => 'Thu, 13 Oct 2011 22:29:14 GMT'},
446
+ :body => '[]'
447
+ )
448
+
449
+ conn = mock("Net::HTTP")
450
+ conn.stubs(:started?).returns(true)
451
+ conn.stubs(:get).at_least(3).at_most(3).returns(response1, response2, response3)
452
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
453
+ assert_nothing_raised do
454
+ headers, result = SwiftClient.get_container(@url, @token, 'test_container', nil, nil, nil, nil, nil, true)
455
+ assert_equal headers, response1.header
456
+ assert_equal JSON.parse(response1.body) + JSON.parse(response2.body) + JSON.parse(response3.body), result
457
+ end
458
+ end
459
+
460
+ def test_get_container_fail
461
+ response = stub(:code => "500", :message => "failed")
462
+ conn = mock("Net::HTTP")
463
+ conn.stubs(:address).returns("foobar.com")
464
+ conn.stubs(:port).returns(123)
465
+ conn.stubs(:started?).returns(true)
466
+ conn.stubs(:get).returns(response)
467
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
468
+
469
+ assert_raise(ClientException) do
470
+ headers, result = SwiftClient.get_container(@url, @token, 'test_container')
471
+ end
472
+ end
473
+
474
+ #test PUT container
475
+ def test_put_container
476
+ response = stub(:code => "200")
477
+ conn = mock("Net::HTTP")
478
+ conn.stubs(:started?).returns(true)
479
+ conn.stubs(:put).returns(response)
480
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
481
+
482
+ assert_nothing_raised do
483
+ headers, result = SwiftClient.put_container(@url, @token, 'test_container')
484
+ end
485
+ end
486
+
487
+ def test_put_container_fail
488
+ response = stub(:code => "500", :message => "failed")
489
+ conn = mock("Net::HTTP")
490
+ conn.stubs(:address).returns("foobar.com")
491
+ conn.stubs(:port).returns(123)
492
+ conn.stubs(:started?).returns(true)
493
+ conn.stubs(:put).returns(response)
494
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
495
+
496
+ assert_raise(ClientException) do
497
+ headers, result = SwiftClient.put_container(@url, @token, 'test_container')
498
+ end
499
+ end
500
+
501
+ #test POST container
502
+ def test_post_container
503
+ response = stub(:code => "200")
504
+ conn = mock("Net::HTTP")
505
+ conn.stubs(:started?).returns(true)
506
+ conn.stubs(:post).returns(response)
507
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
508
+
509
+ assert_nothing_raised do
510
+ headers, result = SwiftClient.post_container(@url, @token, 'test_container', {'x-container-metadata-foo' => 'bar'})
511
+ end
512
+ end
513
+
514
+ def test_post_container_fail
515
+ response = stub(:code => "500", :message => "failed")
516
+ conn = mock("Net::HTTP")
517
+ conn.stubs(:address).returns("foobar.com")
518
+ conn.stubs(:port).returns(123)
519
+ conn.stubs(:started?).returns(true)
520
+ conn.stubs(:post).returns(response)
521
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
522
+
523
+ assert_raise(ClientException) do
524
+ headers, result = SwiftClient.post_container(@url, @token, 'test_container', {'x-container-metadata-foo' => 'bar'})
525
+ end
526
+ end
527
+
528
+ #test DELETE container
529
+ def test_delete_container
530
+ response = stub(:code => "200", :body => "")
531
+ conn = mock("Net::HTTP")
532
+ conn.stubs(:started?).returns(true)
533
+ conn.stubs(:delete).returns(response)
534
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
535
+
536
+ assert_nothing_raised do
537
+ SwiftClient.delete_container(@url, @token, 'test_container')
538
+ end
539
+ end
540
+
541
+ def test_delete_container_fails
542
+ response = stub(:code => "500", :message => "failed")
543
+ conn = mock("Net::HTTP")
544
+ conn.stubs(:address).returns("foobar.com")
545
+ conn.stubs(:port).returns(123)
546
+ conn.stubs(:started?).returns(true)
547
+ conn.stubs(:delete).returns(response)
548
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
549
+
550
+ assert_raise(ClientException) do
551
+ SwiftClient.delete_container(@url, @token, 'test_container')
552
+ end
553
+ end
554
+
555
+ #test HEAD object
556
+ def test_head_object
557
+ response = stub(:code => "204", :header => {'etag' => 'etag_foobarbaz', 'accept-ranges' => 'bytes', 'content-length' => 67773, 'content-type' => 'application/javascript', 'x-trans-id' => 'txfoobar', 'date' => 'Fri, 14 Oct 2011 01:21:42 GMT'})
558
+ conn = mock("Net::HTTP")
559
+ conn.stubs(:port).returns(123)
560
+ conn.stubs(:started?).returns(true)
561
+ conn.stubs(:head).returns(response)
562
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
563
+
564
+ headers = SwiftClient.head_object(@url, @token, 'test_container', 'test_object')
565
+ assert_equal response.header, headers
566
+ end
567
+
568
+ def test_head_object_fails
569
+ response = stub(:code => "500", :message => "failed")
570
+ conn = mock("Net::HTTP")
571
+ conn.stubs(:address).returns("foobar.com")
572
+ conn.stubs(:port).returns(123)
573
+ conn.stubs(:started?).returns(true)
574
+ conn.stubs(:head).returns(response)
575
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
576
+
577
+ assert_raise(ClientException) do
578
+ headers = SwiftClient.head_object(@url, @token, 'test_container', 'test_object')
579
+ end
580
+ end
581
+
582
+ #test GET object
583
+ def test_get_object
584
+ response = stub(
585
+ :code => "200",
586
+ :header => {'etag' => 'etag_foobarbaz', 'accept-ranges' => 'bytes', 'content-length' => 67773, 'content-type' => 'application/javascript', 'x-trans-id' => 'txfoobar', 'date' => 'Fri, 14 Oct 2011 01:21:42 GMT'},
587
+ :body => "this is the body"
588
+ )
589
+ conn = mock("Net::HTTP")
590
+ conn.stubs(:port).returns(123)
591
+ conn.stubs(:address).returns("foobar.com")
592
+ conn.stubs(:started?).returns(true)
593
+ conn.stubs(:request_get).returns(response)
594
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
595
+
596
+ assert_nothing_raised do
597
+ headers, body = SwiftClient.get_object(@url, @token, 'test_container', 'test_object')
598
+ assert_equal response.header, headers
599
+ assert_equal body, "this is the body"
600
+ end
601
+ end
602
+
603
+ def test_get_object_fails
604
+ response = stub(:code => "404", :message => "object not found", :body => "")
605
+ conn = mock("Net::HTTP")
606
+ conn.stubs(:port).returns(123)
607
+ conn.stubs(:address).returns("foobar.com")
608
+ conn.stubs(:started?).returns(true)
609
+ conn.stubs(:request_get).returns(response)
610
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
611
+
612
+ assert_raise(ClientException) do
613
+ headers, body = SwiftClient.get_object(@url, @token, 'test_container', 'test_object')
614
+ end
615
+ end
616
+
617
+ #test PUT object
618
+ def test_put_object
619
+ response = stub(
620
+ :code => "201",
621
+ :header => {"Content-Length" => 118, "Content-Type" => "text/html; charset=UTF-8", "Etag" => "asfasdfsdafasd2313241ukyhuyhj", "X-Trans-Id" => "txfoo1231231231232123"},
622
+ :message => "created",
623
+ :body => ""
624
+ )
625
+ conn = mock("Net::HTTP")
626
+ conn.stubs(:port).returns(123)
627
+ conn.stubs(:address).returns("foobar.com")
628
+ conn.stubs(:started?).returns(true)
629
+ conn.stubs(:put).returns(response)
630
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
631
+ assert_nothing_raised do
632
+ SwiftClient.put_object(@url, @token, 'test_container', 'test_object', 'some data to put', 16)
633
+ end
634
+ end
635
+
636
+ def test_put_object_fails
637
+ response = stub(
638
+ :code => "500",
639
+ :message => "internal server error",
640
+ :body => ""
641
+ )
642
+ conn = mock("Net::HTTP")
643
+ conn.stubs(:port).returns(123)
644
+ conn.stubs(:address).returns("foobar.com")
645
+ conn.stubs(:started?).returns(true)
646
+ conn.stubs(:put).returns(response)
647
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
648
+ assert_raise(ClientException) do
649
+ SwiftClient.put_object(@url, @token, 'test_container', 'test_object', 'some data to put', 16)
650
+ end
651
+ end
652
+
653
+ #test POST object
654
+ def test_post_object
655
+ response = stub(
656
+ :code => "201",
657
+ :header => {"Content-Length" => 118, "Content-Type" => "text/html; charset=UTF-8", "Etag" => "asfasdfsdafasd2313241ukyhuyhj", "X-Trans-Id" => "txfoo1231231231232123", "X-Object-Meta-Foo" => "Bar"},
658
+ :message => "created",
659
+ :body => ""
660
+ )
661
+ conn = mock("Net::HTTP")
662
+ conn.stubs(:port).returns(123)
663
+ conn.stubs(:address).returns("foobar.com")
664
+ conn.stubs(:started?).returns(true)
665
+ conn.stubs(:post).returns(response)
666
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
667
+ assert_nothing_raised do
668
+ headers = SwiftClient.post_object(@url, @token, 'test_container', 'test_object', {"Foo" => "Bar"})
669
+ end
670
+ end
671
+
672
+ def test_post_object_fails
673
+ response = stub(
674
+ :code => "404",
675
+ :message => "no such object",
676
+ :body => ""
677
+ )
678
+ conn = mock("Net::HTTP")
679
+ conn.stubs(:port).returns(123)
680
+ conn.stubs(:address).returns("foobar.com")
681
+ conn.stubs(:started?).returns(true)
682
+ conn.stubs(:post).returns(response)
683
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
684
+ assert_raise(ClientException) do
685
+ SwiftClient.post_object(@url, @token, 'test_container', 'test_object', {"foo" => "bar"})
686
+ end
687
+ end
688
+
689
+ #test DELETE object
690
+ def test_delete_object
691
+ response = stub(
692
+ :code => "204",
693
+ :message => "no content",
694
+ :body => ""
695
+ )
696
+ conn = mock("Net::HTTP")
697
+ conn.stubs(:started?).returns(true)
698
+ conn.stubs(:delete).returns(response)
699
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
700
+ assert_nothing_raised do
701
+ SwiftClient.delete_object(@url, @token, 'test_container', 'test_object')
702
+ end
703
+ end
704
+
705
+ def test_delete_object_fails
706
+ response = stub(
707
+ :code => "404",
708
+ :message => "no such object",
709
+ :body => ""
710
+ )
711
+ conn = mock("Net::HTTP")
712
+ conn.stubs(:port).returns(123)
713
+ conn.stubs(:address).returns("foobar.com")
714
+ conn.stubs(:started?).returns(true)
715
+ conn.stubs(:delete).returns(response)
716
+ SwiftClient.expects(:http_connection).returns([@parsed, conn])
717
+ assert_raise(ClientException) do
718
+ SwiftClient.delete_object(@url, @token, 'test_container', 'test_object')
719
+ end
720
+ end
721
+
722
+ def test_retry_failse
723
+ auth_response = ['http://foo.bar:1234/v1/AUTH_test', 'AUTH_test', {'x-storage-url' => 'http://foo.bar:1234/v1/AUTH_test', 'x-storage-token' => 'AUTH_test', 'x-auth-token' => 'AUTH_test', 'content-length' => 0, 'date' => 'Tue, 11 Oct 2011 20:54:06 GMT'}]
724
+ SwiftClient.expects(:get_auth).returns(auth_response)
725
+ SwiftClient.any_instance.stubs(:http_connection).raises(ClientException.new("foobar"))
726
+ sc = SwiftClient.new(@url, @user, @key)
727
+ sc.get_auth
728
+ assert_raise(ClientException) do
729
+ sc.get_container("test_container")
730
+ end
731
+ end
732
+
733
+ def test_oop_swiftclient
734
+ auth_response = ['http://foo.bar:1234/v1/AUTH_test', 'AUTH_test', {'x-storage-url' => 'http://foo.bar:1234/v1/AUTH_test', 'x-storage-token' => 'AUTH_test', 'x-auth-token' => 'AUTH_test', 'content-length' => 0, 'date' => 'Tue, 11 Oct 2011 20:54:06 GMT'}]
735
+ account_response = [
736
+ {'x-account-object-count' => 123, 'x-account-bytes-used' => 123456, 'x-account-container-count' => 12, 'accept-ranges' => 'bytes', 'content-length' => 12345, 'content-type' => 'application/json; charset=utf-8', 'x-trans-id' => 'txfoobar', 'date' => 'Thu, 13 Oct 2011 21:04:14 GMT'},
737
+ JSON.parse('[ { "name":".CDN_ACCESS_LOGS", "count":1, "bytes":1234 }, { "name":"First", "count":2, "bytes":2345 }, { "name":"Second", "count":3, "bytes":3456 }, { "name":"Third", "count":4, "bytes":4567 } ]')
738
+ ]
739
+ container_response = [
740
+ {'x-container-object-count' => 6, 'x-container-meta-baz' => 'que', 'x-container-meta-foo' => 'bar', 'x-container-bytes-used' => 123456, 'accept-ranges' => 'bytes', 'content-length' => 83, 'content-type' => 'text/plain; charset=utf-8', 'x-trans-id' => 'txfoo123', 'date' => 'Thu, 13 Oct 2011 22:29:14 GMT'},
741
+ JSON.parse('[ { "name":"foo.mp4", "hash":"foo_hash", "bytes":1234567, "content_type":"video/mp4", "last_modified":"2011-06-21T19:49:06.607670" }, { "name":"bar.ogv", "hash":"bar_hash", "bytes":987654, "content_type":"video/ogg", "last_modified":"2011-06-21T19:48:57.504050" }, { "name":"baz.webm", "hash":"baz_hash", "bytes":1239874, "content_type":"application/octet-stream", "last_modified":"2011-06-21T19:48:43.923990" }, { "name":"fobarbaz.html", "hash":"foobarbaz_hash", "bytes":12893467, "content_type":"text/html", "last_modified":"2011-06-21T19:54:36.555070" } ]')
742
+ ]
743
+ object_response = [
744
+ {'Last-Modified' => 'Tue, 01 Jan 2011 00:00:01 GMT', 'Etag' => 'somelarge123hashthingy123foobar', 'Accept-Ranges' => 'bytes', 'Content-Length' => 29, 'Content-Type' => 'application/x-www-form-urlencoded', 'X-Trans-Id' => 'txffffffff00000001231231232112321', 'Date' => 'Tue, 01 Jan 2011 00:00:02 GMT', 'foo' => 'bar'},
745
+ "some data that is from swift"
746
+ ]
747
+ SwiftClient.expects(:http_connection).returns(Net::HTTPExceptions, [@parsed, @conn])
748
+ SwiftClient.expects(:get_auth).returns(auth_response)
749
+ SwiftClient.expects(:get_account).returns(account_response)
750
+ SwiftClient.expects(:head_account).returns(account_response[0])
751
+ SwiftClient.expects(:post_account).returns(nil)
752
+ SwiftClient.expects(:get_container).returns(container_response)
753
+ SwiftClient.expects(:head_container).returns(container_response[0])
754
+ SwiftClient.expects(:put_container).returns(nil)
755
+ SwiftClient.expects(:post_container).returns(nil)
756
+ SwiftClient.expects(:delete_container).returns(nil)
757
+ SwiftClient.expects(:get_object).returns(object_response)
758
+ SwiftClient.expects(:head_object).returns(object_response[0])
759
+ SwiftClient.expects(:put_object).returns(nil)
760
+ SwiftClient.expects(:post_object).returns(nil)
761
+ SwiftClient.expects(:delete_object).returns(nil)
762
+
763
+ assert_nothing_raised do
764
+ sc = SwiftClient.new(@url, @user, @key)
765
+ #test account
766
+ sc.get_auth
767
+ get_a = sc.get_account
768
+ assert_equal 123456, get_a[0]['x-account-bytes-used']
769
+ assert_equal 123, get_a[0]['x-account-object-count']
770
+ assert_equal 'First', get_a[1][1]['name']
771
+ head_a = sc.head_account
772
+ assert_equal 123456, head_a['x-account-bytes-used']
773
+ assert_equal 123, head_a['x-account-object-count']
774
+ sc.post_account({'foo'=>'bar'})
775
+ #test container
776
+ get_c = sc.get_container('test_container')
777
+ assert_equal 'foo.mp4', get_c[1][0]['name']
778
+ assert_equal 6, get_c[0]['x-container-object-count']
779
+ assert_equal 'bar', get_c[0]['x-container-meta-foo']
780
+ head_c = sc.head_container('test_container')
781
+ assert_equal 6, head_c['x-container-object-count']
782
+ assert_equal 'bar', head_c['x-container-meta-foo']
783
+ sc.put_container('test_container')
784
+ sc.post_container('test_container', {'foo' => 'bar'})
785
+ sc.delete_container('test_container')
786
+ #test object
787
+ get_o = sc.get_object('test_container', 'test_object')
788
+ assert_equal "some data that is from swift", get_o[1]
789
+ assert_equal "somelarge123hashthingy123foobar", get_o[0]['Etag']
790
+ head_o = sc.head_object('test_container', 'test_object')
791
+ assert_equal "somelarge123hashthingy123foobar", head_o['Etag']
792
+ sc.put_object('test_container', 'test_object', 'some data to put up')
793
+ sc.post_object('test_container', 'test_object', {"foo" => "bar"})
794
+ sc.delete_object('test_container', 'test_object')
795
+ end
796
+ end
797
+ end