Skip to content
Newer
Older
100644 137 lines (114 sloc) 4.35 KB
d390ede @marcel Moving aws-s3 into git
authored
1 require File.dirname(__FILE__) + '/test_helper'
2
3 class BaseTest < Test::Unit::TestCase
4 def test_connection_established
5 assert_raises(NoConnectionEstablished) do
6 Base.connection
7 end
8
9 Base.establish_connection!(:access_key_id => '123', :secret_access_key => 'abc')
10 assert_kind_of Connection, Base.connection
11
12 instance = Base.new
13 assert_equal instance.send(:connection), Base.connection
14 assert_equal instance.send(:http), Base.connection.http
15 end
16
17 def test_respond_with
18 assert_equal Base::Response, Base.send(:response_class)
19 Base.send(:respond_with, Bucket::Response) do
20 assert_equal Bucket::Response, Base.send(:response_class)
21 end
22 assert_equal Base::Response, Base.send(:response_class)
23 end
24
25 def test_request_tries_again_when_encountering_an_internal_error
d9af94a @marcel Port hand rolled mock code over to using flexmock
authored
26 mock_connection_for(Bucket, :returns => [
27 # First request is an internal error
28 {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
29 # Second request is a success
30 {:body => Fixtures::Buckets.empty_bucket, :code => 200}
31 ])
32 bucket = nil # Block scope hack
33 assert_nothing_raised do
34 bucket = Bucket.find('marcel')
35 end
36 # Don't call objects 'cause we don't want to make another request
37 assert bucket.object_cache.empty?
d390ede @marcel Moving aws-s3 into git
authored
38 end
39
40 def test_request_tries_up_to_three_times
d9af94a @marcel Port hand rolled mock code over to using flexmock
authored
41 mock_connection_for(Bucket, :returns => [
42 # First request is an internal error
43 {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
44 # Second request is also an internal error
45 {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
46 # Ditto third
47 {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
48 # Fourth works
49 {:body => Fixtures::Buckets.empty_bucket, :code => 200}
50 ])
51 bucket = nil # Block scope hack
52 assert_nothing_raised do
53 bucket = Bucket.find('marcel')
d390ede @marcel Moving aws-s3 into git
authored
54 end
d9af94a @marcel Port hand rolled mock code over to using flexmock
authored
55 # Don't call objects 'cause we don't want to make another request
56 assert bucket.object_cache.empty?
d390ede @marcel Moving aws-s3 into git
authored
57 end
58
59 def test_request_tries_again_three_times_and_gives_up
d9af94a @marcel Port hand rolled mock code over to using flexmock
authored
60 mock_connection_for(Bucket, :returns => [
61 # First request is an internal error
62 {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
63 # Second request is also an internal error
64 {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
65 # Ditto third
66 {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
67 # Ditto fourth
68 {:body => Fixtures::Errors.internal_error, :code => 500, :error => true},
69 ])
70 assert_raises(InternalError) do
71 Bucket.find('marcel')
d390ede @marcel Moving aws-s3 into git
authored
72 end
73 end
74 end
75
76 class MultiConnectionsTest < Test::Unit::TestCase
77 class ClassToTestSettingCurrentBucket < Base
78 set_current_bucket_to 'foo'
79 end
80
81 def setup
82 Base.send(:connections).clear
83 end
84
0f3a832 @marcel Avoid using the teardown method in conjunction with flexmock
authored
85 def test_default_connection_options_are_used_for_subsequent_connections
d390ede @marcel Moving aws-s3 into git
authored
86 assert !Base.connected?
87
88 assert_raises(MissingAccessKey) do
89 Base.establish_connection!
90 end
91
92 assert !Base.connected?
93
94 assert_raises(NoConnectionEstablished) do
95 Base.connection
96 end
97
98 assert_nothing_raised do
99 Base.establish_connection!(:access_key_id => '123', :secret_access_key => 'abc')
100 end
101
102 assert Base.connected?
103
104 assert_nothing_raised do
105 Base.connection
106 end
107
108 # All subclasses are currently using the default connection
0f3a832 @marcel Avoid using the teardown method in conjunction with flexmock
authored
109 assert_equal Base.connection, Bucket.connection
d390ede @marcel Moving aws-s3 into git
authored
110
111 # No need to pass in the required options. The default connection will supply them
112 assert_nothing_raised do
113 Bucket.establish_connection!(:server => 'foo.s3.amazonaws.com')
114 end
115
116 assert Base.connection != Bucket.connection
117 assert_equal '123', Bucket.connection.access_key_id
118 assert_equal 'foo', Bucket.connection.subdomain
119 end
120
121 def test_current_bucket
122 Base.establish_connection!(:access_key_id => '123', :secret_access_key => 'abc')
123 assert_raises(CurrentBucketNotSpecified) do
124 Base.current_bucket
125 end
126
127 S3Object.establish_connection!(:server => 'foo-bucket.s3.amazonaws.com')
128 assert_nothing_raised do
129 assert_equal 'foo-bucket', S3Object.current_bucket
130 end
131 end
132
133 def test_setting_the_current_bucket
134 assert_equal 'foo', ClassToTestSettingCurrentBucket.current_bucket
135 end
136 end
Something went wrong with that request. Please try again.