forked from marcel/aws-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_test.rb
215 lines (175 loc) · 7.09 KB
/
connection_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require File.dirname(__FILE__) + '/test_helper'
class ConnectionTest < Test::Unit::TestCase
attr_reader :keys
def setup
@keys = {:access_key_id => '123', :secret_access_key => 'abc'}.freeze
end
def test_creating_a_connection
connection = Connection.new(keys)
assert_kind_of Net::HTTP, connection.http
end
def test_use_ssl_option_is_set_in_connection
connection = Connection.new(keys.merge(:use_ssl => true))
assert connection.http.use_ssl?
end
def test_setting_port_to_443_implies_use_ssl
connection = Connection.new(keys.merge(:port => 443))
assert connection.http.use_ssl?
end
def test_protocol
connection = Connection.new(keys)
assert_equal 'http://', connection.protocol
connection = Connection.new(keys.merge(:use_ssl => true))
assert_equal 'https://', connection.protocol
end
def test_url_for_honors_use_ssl_option_if_it_is_false_even_if_connection_has_use_ssl_option_set
# References bug: http://rubyforge.org/tracker/index.php?func=detail&aid=17628&group_id=2409&atid=9356
connection = Connection.new(keys.merge(:use_ssl => true))
assert_match %r(^http://), connection.url_for('/pathdoesnotmatter', :authenticated => false, :use_ssl => false)
end
def test_connection_is_not_persistent_by_default
connection = Connection.new(keys)
assert !connection.persistent?
connection = Connection.new(keys.merge(:persistent => true))
assert connection.persistent?
end
def test_server_and_port_are_passed_onto_connection
connection = Connection.new(keys)
options = connection.instance_variable_get('@options')
assert_equal connection.http.address, options[:server]
assert_equal connection.http.port, options[:port]
end
def test_not_including_required_access_keys_raises
assert_raises(MissingAccessKey) do
Connection.new
end
assert_raises(MissingAccessKey) do
Connection.new(:access_key_id => '123')
end
assert_nothing_raised do
Connection.new(keys)
end
end
def test_access_keys_extracted
connection = Connection.new(keys)
assert_equal '123', connection.access_key_id
assert_equal 'abc', connection.secret_access_key
end
def test_request_method_class_lookup
connection = Connection.new(keys)
expectations = {
:get => Net::HTTP::Get, :post => Net::HTTP::Post,
:put => Net::HTTP::Put, :delete => Net::HTTP::Delete,
:head => Net::HTTP::Head
}
expectations.each do |verb, klass|
assert_equal klass, connection.send(:request_method, verb)
end
end
def test_url_for_uses_default_protocol_server_and_port
connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :port => 80)
assert_match %r(^http://s3\.amazonaws\.com/foo\?), connection.url_for('/foo')
connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :use_ssl => true, :port => 443)
assert_match %r(^https://s3\.amazonaws\.com/foo\?), connection.url_for('/foo')
end
def test_url_for_remembers_custom_protocol_server_and_port
connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :server => 'example.org', :port => 555, :use_ssl => true)
assert_match %r(^https://example\.org:555/foo\?), connection.url_for('/foo')
end
def test_url_for_with_and_without_authenticated_urls
connection = Connection.new(:access_key_id => '123', :secret_access_key => 'abc', :server => 'example.org')
authenticated = lambda {|url| url['?AWSAccessKeyId']}
assert authenticated[connection.url_for('/foo')]
assert authenticated[connection.url_for('/foo', :authenticated => true)]
assert !authenticated[connection.url_for('/foo', :authenticated => false)]
end
def test_connecting_through_a_proxy
connection = nil
assert_nothing_raised do
connection = Connection.new(keys.merge(:proxy => sample_proxy_settings))
end
assert connection.http.proxy?
end
def test_request_only_escapes_the_path_the_first_time_it_runs_and_not_subsequent_times
connection = Connection.new(@keys)
unescaped_path = 'path with spaces'
escaped_path = 'path%20with%20spaces'
flexmock(Connection).should_receive(:prepare_path).with(unescaped_path).once.and_return(escaped_path).ordered
flexmock(connection.http).should_receive(:request).and_raise(Errno::EPIPE).ordered
flexmock(connection.http).should_receive(:request).ordered
connection.request :put, unescaped_path
end
def test_if_request_has_no_body_then_the_content_length_is_set_to_zero
# References bug: http://rubyforge.org/tracker/index.php?func=detail&aid=13052&group_id=2409&atid=9356
connection = Connection.new(@keys)
flexmock(Net::HTTP::Put).new_instances.should_receive(:content_length=).once.with(0).ordered
flexmock(connection.http).should_receive(:request).once.ordered
connection.request :put, 'path does not matter'
end
end
class ConnectionOptionsTest < Test::Unit::TestCase
def setup
@options = generate_options(:server => 'example.org', :port => 555)
@default_options = generate_options
end
def test_server_extracted
assert_key_transfered(:server, 'example.org', @options)
end
def test_port_extracted
assert_key_transfered(:port, 555, @options)
end
def test_server_defaults_to_default_host
assert_equal DEFAULT_HOST, @default_options[:server]
end
def test_port_defaults_to_80_if_use_ssl_is_false
assert_equal 80, @default_options[:port]
end
def test_port_is_set_to_443_if_use_ssl_is_true
options = generate_options(:use_ssl => true)
assert_equal 443, options[:port]
end
def test_explicit_port_trumps_use_ssl
options = generate_options(:port => 555, :use_ssl => true)
assert_equal 555, options[:port]
end
def test_invalid_options_raise
assert_raises(InvalidConnectionOption) do
generate_options(:host => 'campfire.s3.amazonaws.com')
end
end
def test_not_specifying_all_required_proxy_settings_raises
assert_raises(ArgumentError) do
generate_options(:proxy => {})
end
end
def test_not_specifying_proxy_option_at_all_does_not_raise
assert_nothing_raised do
generate_options
end
end
def test_specifying_all_required_proxy_settings
assert_nothing_raised do
generate_options(:proxy => sample_proxy_settings)
end
end
def test_only_host_setting_is_required
assert_nothing_raised do
generate_options(:proxy => {:host => 'http://google.com'})
end
end
def test_proxy_settings_are_extracted
options = generate_options(:proxy => sample_proxy_settings)
assert_equal sample_proxy_settings.values.map {|value| value.to_s}.sort, options.proxy_settings.map {|value| value.to_s}.sort
end
def test_recognizing_that_the_settings_want_to_connect_through_a_proxy
options = generate_options(:proxy => sample_proxy_settings)
assert options.connecting_through_proxy?
end
private
def assert_key_transfered(key, value, options)
assert_equal value, options[key]
end
def generate_options(options = {})
Connection::Options.new(options)
end
end