rackspace-cloudfiles 1.3.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,190 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class CloudfilesContainerTest < Test::Unit::TestCase
4
+
5
+ def test_object_creation
6
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path')
7
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5'}
8
+ response.stubs(:code).returns('204')
9
+ connection.stubs(:cfreq => response)
10
+ @container = CloudFiles::Container.new(connection, 'test_container')
11
+ assert_equal @container.name, 'test_container'
12
+ assert_equal @container.class, CloudFiles::Container
13
+ assert_equal @container.public?, true
14
+ end
15
+
16
+ def test_object_creation_no_such_container
17
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path')
18
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5'}
19
+ response.stubs(:code).returns('999')
20
+ connection.stubs(:cfreq => response)
21
+ assert_raise(NoSuchContainerException) do
22
+ @container = CloudFiles::Container.new(connection, 'test_container')
23
+ end
24
+ end
25
+
26
+ def test_object_creation_no_cdn
27
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path')
28
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5'}
29
+ response.stubs(:code).returns('204').then.returns('999')
30
+ connection.stubs(:cfreq => response)
31
+ @container = CloudFiles::Container.new(connection, 'test_container')
32
+ assert_equal @container.name, 'test_container'
33
+ assert_equal @container.cdn_enabled, false
34
+ assert_equal @container.public?, false
35
+ end
36
+
37
+ def test_to_s
38
+ build_net_http_object
39
+ assert_equal @container.to_s, 'test_container'
40
+ end
41
+
42
+ def test_make_private_succeeds
43
+ build_net_http_object(:code => '201')
44
+ assert_nothing_raised do
45
+ @container.make_private
46
+ end
47
+ end
48
+
49
+ def test_make_private_fails
50
+ build_net_http_object(:code => '999')
51
+ assert_raises(NoSuchContainerException) do
52
+ @container.make_private
53
+ end
54
+ end
55
+
56
+ def test_make_public_succeeds
57
+ build_net_http_object(:code => '201')
58
+ assert_nothing_raised do
59
+ @container.make_public
60
+ end
61
+ end
62
+
63
+ def test_make_public_fails
64
+ build_net_http_object(:code => '999')
65
+ assert_raises(NoSuchContainerException) do
66
+ @container.make_public
67
+ end
68
+ end
69
+
70
+ def test_empty_is_false
71
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path')
72
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5'}
73
+ response.stubs(:code).returns('204')
74
+ connection.stubs(:cfreq => response)
75
+ @container = CloudFiles::Container.new(connection, 'test_container')
76
+ assert_equal @container.empty?, false
77
+ end
78
+
79
+ def test_empty_is_true
80
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path')
81
+ response = {'x-container-bytes-used' => '0', 'x-container-object-count' => '0'}
82
+ response.stubs(:code).returns('204')
83
+ connection.stubs(:cfreq => response)
84
+ @container = CloudFiles::Container.new(connection, 'test_container')
85
+ assert_equal @container.empty?, true
86
+ end
87
+
88
+ def test_object_fetch
89
+ build_net_http_object(:code => '204', :response => {'last-modified' => 'Wed, 28 Jan 2009 16:16:26 GMT'})
90
+ object = @container.object('good_object')
91
+ assert_equal object.class, CloudFiles::StorageObject
92
+ end
93
+
94
+ def test_create_object
95
+ build_net_http_object()
96
+ object = @container.create_object('new_object')
97
+ assert_equal object.class, CloudFiles::StorageObject
98
+ end
99
+
100
+ def test_object_exists_true
101
+ build_net_http_object
102
+ assert_equal @container.object_exists?('good_object'), true
103
+ end
104
+
105
+ def test_object_exists_false
106
+ build_net_http_object(:code => '999')
107
+ assert_equal @container.object_exists?('bad_object'), false
108
+ end
109
+
110
+ def test_delete_object_succeeds
111
+ build_net_http_object
112
+ assert_nothing_raised do
113
+ @container.delete_object('good_object')
114
+ end
115
+ end
116
+
117
+ def test_delete_invalid_object_fails
118
+ build_net_http_object(:code => '404')
119
+ assert_raise(NoSuchObjectException) do
120
+ @container.delete_object('nonexistent_object')
121
+ end
122
+ end
123
+
124
+ def test_delete_invalid_response_code_fails
125
+ build_net_http_object(:code => '999')
126
+ assert_raise(InvalidResponseException) do
127
+ @container.delete_object('broken_object')
128
+ end
129
+ end
130
+
131
+ def test_fetch_objects
132
+ build_net_http_object(:code => '200', :body => "foo\nbar\nbaz")
133
+ objects = @container.objects
134
+ assert_equal objects.class, Array
135
+ assert_equal objects.size, 3
136
+ assert_equal objects.first, 'foo'
137
+ end
138
+
139
+ def test_fetch_object_detail
140
+ body = %{<?xml version="1.0" encoding="UTF-8"?>
141
+ <container name="video">
142
+ <object><name>kisscam.mov</name><hash>96efd5a0d78b74cfe2a911c479b98ddd</hash><bytes>9196332</bytes><content_type>video/quicktime</content_type><last_modified>2008-12-18T10:34:43.867648</last_modified></object>
143
+ <object><name>penaltybox.mov</name><hash>d2a4c0c24d8a7b4e935bee23080e0685</hash><bytes>24944966</bytes><content_type>video/quicktime</content_type><last_modified>2008-12-18T10:35:19.273927</last_modified></object>
144
+ </container>
145
+ }
146
+ build_net_http_object(:code => '200', :body => body)
147
+ details = @container.objects_detail
148
+ assert_equal details.size, 2
149
+ assert_equal details['kisscam.mov'][:bytes], '9196332'
150
+ end
151
+
152
+ def test_fetch_object_detail_empty
153
+ build_net_http_object
154
+ details = @container.objects_detail
155
+ assert_equal details, {}
156
+ end
157
+
158
+ def test_fetch_object_detail_error
159
+ build_net_http_object(:code => '999')
160
+ assert_raise(InvalidResponseException) do
161
+ details = @container.objects_detail
162
+ end
163
+ end
164
+
165
+ private
166
+
167
+ def build_net_http_object(args={:code => '204' })
168
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
169
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path')
170
+ args[:response] = {} unless args[:response]
171
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token', 'last-modified' => Time.now.to_s}.merge(args[:response])
172
+ response.stubs(:code).returns(args[:code])
173
+ response.stubs(:body).returns args[:body] || nil
174
+ connection.stubs(:cfreq => response)
175
+ #server = mock()
176
+ #server.stubs(:verify_mode= => true)
177
+ #server.stubs(:start => true)
178
+ #server.stubs(:use_ssl=).returns(true)
179
+ #server.stubs(:get).returns(response)
180
+ #server.stubs(:post).returns(response)
181
+ #server.stubs(:put).returns(response)
182
+ #server.stubs(:head).returns(response)
183
+ #server.stubs(:delete).returns(response)
184
+ #Net::HTTP.stubs(:new).returns(server)
185
+ @container = CloudFiles::Container.new(connection, 'test_container')
186
+ @container.stubs(:connection).returns(connection)
187
+ end
188
+
189
+
190
+ end
@@ -0,0 +1,170 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class CloudfilesStorageObjectTest < Test::Unit::TestCase
4
+
5
+ def test_object_creation
6
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path')
7
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5', 'last-modified' => Time.now.to_s}
8
+ response.stubs(:code).returns('204')
9
+ connection.stubs(:cfreq => response)
10
+ container = CloudFiles::Container.new(connection, 'test_container')
11
+ @object = CloudFiles::StorageObject.new(container, 'test_object')
12
+ assert_equal @object.name, 'test_object'
13
+ assert_equal @object.class, CloudFiles::StorageObject
14
+ assert_equal @object.to_s, 'test_object'
15
+ end
16
+
17
+ def test_object_creation_with_invalid_name
18
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path')
19
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5', 'last-modified' => Time.now.to_s}
20
+ response.stubs(:code).returns('204')
21
+ connection.stubs(:cfreq => response)
22
+ container = CloudFiles::Container.new(connection, 'test_container')
23
+ assert_raises SyntaxException do
24
+ @object = CloudFiles::StorageObject.new(container, 'test_object?')
25
+ end
26
+ end
27
+
28
+
29
+ def test_public_url_exists
30
+ build_net_http_object(:public => true, :name => 'test object')
31
+ assert_equal @object.public_url, "http://cdn.test.example/test%20object"
32
+ end
33
+
34
+ def test_public_url_does_not_exist
35
+ build_net_http_object
36
+ assert_equal @object.public_url, nil
37
+ end
38
+
39
+ def test_data_succeeds
40
+ build_net_http_object(:code => '200', :body => 'This is good data')
41
+ assert_equal @object.data, 'This is good data'
42
+ end
43
+
44
+ def test_data_fails
45
+ build_net_http_object(:code => '999', :body => 'This is bad data')
46
+ assert_raise(NoSuchObjectException) do
47
+ @object.data
48
+ end
49
+ end
50
+
51
+ def test_data_stream_succeeds
52
+ build_net_http_object(:code => '200', :body => 'This is good data')
53
+ data = ""
54
+ assert_nothing_raised do
55
+ @object.data_stream { |chunk|
56
+ data += chunk
57
+ }
58
+ end
59
+ end
60
+
61
+ def data_stream_fails
62
+ build_net_http_object(:code => '999', :body => 'This is bad data')
63
+ data = ""
64
+ assert_raise(NoSuchObjectException) do
65
+ @object.data_stream { |chunk|
66
+ data += chunk
67
+ }
68
+ end
69
+ end
70
+
71
+ def test_set_metadata_succeeds
72
+ CloudFiles::StorageObject.any_instance.stubs(:populate).returns(true)
73
+ build_net_http_object(:code => '202')
74
+ assert_nothing_raised do
75
+ @object.set_metadata({'Foo' =>'bar'})
76
+ end
77
+ end
78
+
79
+ def test_set_metadata_invalid_object
80
+ build_net_http_object(:code => '404')
81
+ assert_raise(NoSuchObjectException) do
82
+ @object.set_metadata({'Foo' =>'bar'})
83
+ end
84
+ end
85
+
86
+ def test_set_metadata_fails
87
+ build_net_http_object(:code => '999')
88
+ assert_raise(InvalidResponseException) do
89
+ @object.set_metadata({'Foo' =>'bar'})
90
+ end
91
+ end
92
+
93
+ def test_write_succeeds
94
+ CloudFiles::StorageObject.any_instance.stubs(:populate).returns(true)
95
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
96
+ build_net_http_object(:code => '201')
97
+ assert_nothing_raised do
98
+ @object.write("This is test data")
99
+ end
100
+ end
101
+
102
+ def test_load_from_filename_succeeds
103
+ require 'tempfile'
104
+ out = Tempfile.new('test')
105
+ out.write("This is test data")
106
+ out.close
107
+ CloudFiles::StorageObject.any_instance.stubs(:populate).returns(true)
108
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
109
+ build_net_http_object(:code => '201')
110
+ assert_nothing_raised do
111
+ @object.load_from_filename(out.path)
112
+ end
113
+ end
114
+
115
+ def test_write_sets_mime_type
116
+ CloudFiles::StorageObject.any_instance.stubs(:populate).returns(true)
117
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
118
+ build_net_http_object(:name => 'myfile.xml', :code => '201')
119
+ assert_nothing_raised do
120
+ @object.write("This is test data")
121
+ end
122
+ end
123
+
124
+ def test_write_with_no_data_dies
125
+ build_net_http_object
126
+ assert_raise(SyntaxException) do
127
+ @object.write
128
+ end
129
+ end
130
+
131
+ def test_write_with_invalid_content_length_dies
132
+ build_net_http_object(:code => '412')
133
+ assert_raise(InvalidResponseException) do
134
+ @object.write('Test Data')
135
+ end
136
+ end
137
+
138
+ def test_write_with_mismatched_md5_dies
139
+ build_net_http_object(:code => '422')
140
+ assert_raise(MisMatchedChecksumException) do
141
+ @object.write('Test Data')
142
+ end
143
+ end
144
+
145
+ def test_write_with_invalid_response_dies
146
+ build_net_http_object(:code => '999')
147
+ assert_raise(InvalidResponseException) do
148
+ @object.write('Test Data')
149
+ end
150
+ end
151
+
152
+ private
153
+
154
+ def build_net_http_object(args={:code => '204' })
155
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
156
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path')
157
+ args[:response] = {} unless args[:response]
158
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token', 'last-modified' => Time.now.to_s}.merge(args[:response])
159
+ response.stubs(:code).returns(args[:code])
160
+ response.stubs(:body).returns args[:body] || nil
161
+ connection.stubs(:cfreq => response)
162
+ container = CloudFiles::Container.new(connection, 'test_container')
163
+ container.stubs(:connection).returns(connection)
164
+ container.stubs(:public?).returns(args[:public] || false)
165
+ container.stubs(:cdn_url).returns('http://cdn.test.example')
166
+ @object = CloudFiles::StorageObject.new(container, args[:name] || 'test_object')
167
+ end
168
+
169
+
170
+ end
@@ -0,0 +1,5 @@
1
+ require 'test/unit'
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+ require 'cloudfiles'
4
+ require 'rubygems'
5
+ require 'mocha'
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rackspace-cloudfiles
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.0.2
5
+ platform: ruby
6
+ authors:
7
+ - H. Wade Minter, Rackspace Hosting
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-14 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mime-types
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "1.0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: echoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: A Ruby version of the Mosso Cloud Files API.
36
+ email: wade.minter@rackspace.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - lib/cloudfiles/authentication.rb
43
+ - lib/cloudfiles/connection.rb
44
+ - lib/cloudfiles/container.rb
45
+ - lib/cloudfiles/storage_object.rb
46
+ - lib/cloudfiles.rb
47
+ - README.rdoc
48
+ - TODO
49
+ files:
50
+ - cloudfiles.gemspec
51
+ - lib/cloudfiles/authentication.rb
52
+ - lib/cloudfiles/connection.rb
53
+ - lib/cloudfiles/container.rb
54
+ - lib/cloudfiles/storage_object.rb
55
+ - lib/cloudfiles.rb
56
+ - Manifest
57
+ - Rakefile
58
+ - README.rdoc
59
+ - test/cf-testunit.rb
60
+ - test/cloudfiles_authentication_test.rb
61
+ - test/cloudfiles_connection_test.rb
62
+ - test/cloudfiles_container_test.rb
63
+ - test/cloudfiles_storage_object_test.rb
64
+ - test/test_helper.rb
65
+ - TODO
66
+ has_rdoc: true
67
+ homepage: http://www.mosso.com/cloudfiles.jsp
68
+ post_install_message:
69
+ rdoc_options:
70
+ - --line-numbers
71
+ - --inline-source
72
+ - --title
73
+ - Cloudfiles
74
+ - --main
75
+ - README.rdoc
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "1.2"
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project: cloudfiles
93
+ rubygems_version: 1.2.0
94
+ signing_key:
95
+ specification_version: 2
96
+ summary: A Ruby API into Mosso Cloud Files
97
+ test_files:
98
+ - test/cloudfiles_authentication_test.rb
99
+ - test/cloudfiles_connection_test.rb
100
+ - test/cloudfiles_container_test.rb
101
+ - test/cloudfiles_storage_object_test.rb
102
+ - test/test_helper.rb