aeden-contacts 0.2.15
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +51 -0
- data/Rakefile +71 -0
- data/VERSION.yml +4 -0
- data/lib/config/contacts.yml +10 -0
- data/lib/contacts/flickr.rb +133 -0
- data/lib/contacts/google.rb +387 -0
- data/lib/contacts/google_oauth.rb +91 -0
- data/lib/contacts/version.rb +9 -0
- data/lib/contacts/windows_live.rb +164 -0
- data/lib/contacts/yahoo.rb +236 -0
- data/lib/contacts.rb +55 -0
- data/spec/contact_spec.rb +61 -0
- data/spec/feeds/contacts.yml +10 -0
- data/spec/feeds/flickr/auth.getFrob.xml +4 -0
- data/spec/feeds/flickr/auth.getToken.xml +5 -0
- data/spec/feeds/google-many.xml +48 -0
- data/spec/feeds/google-single.xml +46 -0
- data/spec/feeds/wl_contacts.xml +29 -0
- data/spec/feeds/yh_contacts.txt +119 -0
- data/spec/feeds/yh_credential.xml +28 -0
- data/spec/flickr/auth_spec.rb +80 -0
- data/spec/gmail/auth_spec.rb +70 -0
- data/spec/gmail/fetching_spec.rb +198 -0
- data/spec/rcov.opts +2 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +84 -0
- data/spec/windows_live/windows_live_spec.rb +34 -0
- data/spec/yahoo/yahoo_spec.rb +83 -0
- data/vendor/fakeweb/CHANGELOG +80 -0
- data/vendor/fakeweb/LICENSE.txt +281 -0
- data/vendor/fakeweb/README.rdoc +160 -0
- data/vendor/fakeweb/Rakefile +57 -0
- data/vendor/fakeweb/fakeweb.gemspec +13 -0
- data/vendor/fakeweb/lib/fake_web/ext/net_http.rb +58 -0
- data/vendor/fakeweb/lib/fake_web/registry.rb +78 -0
- data/vendor/fakeweb/lib/fake_web/responder.rb +88 -0
- data/vendor/fakeweb/lib/fake_web/response.rb +10 -0
- data/vendor/fakeweb/lib/fake_web/socket_delegator.rb +24 -0
- data/vendor/fakeweb/lib/fake_web.rb +152 -0
- data/vendor/fakeweb/test/fixtures/test_example.txt +1 -0
- data/vendor/fakeweb/test/fixtures/test_request +21 -0
- data/vendor/fakeweb/test/test_allow_net_connect.rb +41 -0
- data/vendor/fakeweb/test/test_fake_web.rb +453 -0
- data/vendor/fakeweb/test/test_fake_web_open_uri.rb +62 -0
- data/vendor/fakeweb/test/test_helper.rb +52 -0
- data/vendor/fakeweb/test/test_query_string.rb +37 -0
- data/vendor/windowslivelogin.rb +1151 -0
- metadata +108 -0
@@ -0,0 +1,453 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class TestFakeWeb < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
FakeWeb.allow_net_connect = true
|
7
|
+
FakeWeb.clean_registry
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_register_uri
|
11
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :string => "example")
|
12
|
+
assert FakeWeb.registered_uri?('http://mock/test_example.txt')
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_register_uri_with_a_single_argument
|
16
|
+
assert_nothing_raised do
|
17
|
+
FakeWeb.register_uri("http://example.com")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_register_uri_with_wrong_number_of_arguments
|
22
|
+
assert_raises ArgumentError do
|
23
|
+
FakeWeb.register_uri(:get, "http://example.com", "/example", :string => "example")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_registered_uri_with_wrong_number_of_arguments
|
28
|
+
assert_raises ArgumentError do
|
29
|
+
FakeWeb.registered_uri?
|
30
|
+
end
|
31
|
+
assert_raises ArgumentError do
|
32
|
+
FakeWeb.registered_uri?(:get, "http://example.com", "/example")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_response_for_with_wrong_number_of_arguments
|
37
|
+
assert_raises ArgumentError do
|
38
|
+
FakeWeb.response_for
|
39
|
+
end
|
40
|
+
assert_raises ArgumentError do
|
41
|
+
FakeWeb.response_for(:get, "http://example.com", "/example")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_register_uri_without_domain_name
|
46
|
+
assert_raises URI::InvalidURIError do
|
47
|
+
FakeWeb.register_uri('test_example2.txt', File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_register_uri_with_port_and_check_with_port
|
52
|
+
FakeWeb.register_uri('http://example.com:3000/', :string => 'foo')
|
53
|
+
assert FakeWeb.registered_uri?('http://example.com:3000/')
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_register_uri_with_port_and_check_without_port
|
57
|
+
FakeWeb.register_uri('http://example.com:3000/', :string => 'foo')
|
58
|
+
assert !FakeWeb.registered_uri?('http://example.com/')
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_register_uri_with_default_port_for_http_and_check_without_port
|
62
|
+
FakeWeb.register_uri('http://example.com:80/', :string => 'foo')
|
63
|
+
assert FakeWeb.registered_uri?('http://example.com/')
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_register_uri_with_default_port_for_https_and_check_without_port
|
67
|
+
FakeWeb.register_uri('https://example.com:443/', :string => 'foo')
|
68
|
+
assert FakeWeb.registered_uri?('https://example.com/')
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_register_uri_with_no_port_for_http_and_check_with_default_port
|
72
|
+
FakeWeb.register_uri('http://example.com/', :string => 'foo')
|
73
|
+
assert FakeWeb.registered_uri?('http://example.com:80/')
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_register_uri_with_no_port_for_https_and_check_with_default_port
|
77
|
+
FakeWeb.register_uri('https://example.com/', :string => 'foo')
|
78
|
+
assert FakeWeb.registered_uri?('https://example.com:443/')
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_register_uri_for_any_method_explicitly
|
82
|
+
FakeWeb.register_uri(:any, "http://example.com/rpc_endpoint", :string => "OK")
|
83
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com/rpc_endpoint")
|
84
|
+
assert FakeWeb.registered_uri?(:post, "http://example.com/rpc_endpoint")
|
85
|
+
assert FakeWeb.registered_uri?(:put, "http://example.com/rpc_endpoint")
|
86
|
+
assert FakeWeb.registered_uri?(:delete, "http://example.com/rpc_endpoint")
|
87
|
+
assert FakeWeb.registered_uri?(:any, "http://example.com/rpc_endpoint")
|
88
|
+
assert FakeWeb.registered_uri?("http://example.com/rpc_endpoint")
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_register_uri_for_get_method_only
|
92
|
+
FakeWeb.register_uri(:get, "http://example.com/users", :string => "User list")
|
93
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com/users")
|
94
|
+
assert !FakeWeb.registered_uri?(:post, "http://example.com/users")
|
95
|
+
assert !FakeWeb.registered_uri?(:put, "http://example.com/users")
|
96
|
+
assert !FakeWeb.registered_uri?(:delete, "http://example.com/users")
|
97
|
+
assert !FakeWeb.registered_uri?(:any, "http://example.com/users")
|
98
|
+
assert !FakeWeb.registered_uri?("http://example.com/users")
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_response_for_with_registered_uri
|
102
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
103
|
+
assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_response_for_with_unknown_uri
|
107
|
+
assert_equal nil, FakeWeb.response_for(:get, 'http://example.com/')
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_response_for_with_put_method
|
111
|
+
FakeWeb.register_uri(:put, "http://example.com", :string => "response")
|
112
|
+
assert_equal 'response', FakeWeb.response_for(:put, "http://example.com").body
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_response_for_with_any_method_explicitly
|
116
|
+
FakeWeb.register_uri(:any, "http://example.com", :string => "response")
|
117
|
+
assert_equal 'response', FakeWeb.response_for(:get, "http://example.com").body
|
118
|
+
assert_equal 'response', FakeWeb.response_for(:any, "http://example.com").body
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_content_for_registered_uri_with_port_and_request_with_port
|
122
|
+
FakeWeb.register_uri('http://example.com:3000/', :string => 'test example content')
|
123
|
+
Net::HTTP.start('example.com', 3000) do |http|
|
124
|
+
response = http.get('/')
|
125
|
+
assert_equal 'test example content', response.body
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_content_for_registered_uri_with_default_port_for_http_and_request_without_port
|
130
|
+
FakeWeb.register_uri('http://example.com:80/', :string => 'test example content')
|
131
|
+
Net::HTTP.start('example.com') do |http|
|
132
|
+
response = http.get('/')
|
133
|
+
assert_equal 'test example content', response.body
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_content_for_registered_uri_with_no_port_for_http_and_request_with_default_port
|
138
|
+
FakeWeb.register_uri('http://example.com/', :string => 'test example content')
|
139
|
+
Net::HTTP.start('example.com', 80) do |http|
|
140
|
+
response = http.get('/')
|
141
|
+
assert_equal 'test example content', response.body
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_content_for_registered_uri_with_default_port_for_https_and_request_with_default_port
|
146
|
+
FakeWeb.register_uri('https://example.com:443/', :string => 'test example content')
|
147
|
+
http = Net::HTTP.new('example.com', 443)
|
148
|
+
http.use_ssl = true
|
149
|
+
response = http.get('/')
|
150
|
+
assert_equal 'test example content', response.body
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_content_for_registered_uri_with_no_port_for_https_and_request_with_default_port
|
154
|
+
FakeWeb.register_uri('https://example.com/', :string => 'test example content')
|
155
|
+
http = Net::HTTP.new('example.com', 443)
|
156
|
+
http.use_ssl = true
|
157
|
+
response = http.get('/')
|
158
|
+
assert_equal 'test example content', response.body
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_content_for_registered_uris_with_ports_on_same_domain_and_request_without_port
|
162
|
+
FakeWeb.register_uri('http://example.com:3000/', :string => 'port 3000')
|
163
|
+
FakeWeb.register_uri('http://example.com/', :string => 'port 80')
|
164
|
+
Net::HTTP.start('example.com') do |http|
|
165
|
+
response = http.get('/')
|
166
|
+
assert_equal 'port 80', response.body
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_content_for_registered_uris_with_ports_on_same_domain_and_request_with_port
|
171
|
+
FakeWeb.register_uri('http://example.com:3000/', :string => 'port 3000')
|
172
|
+
FakeWeb.register_uri('http://example.com/', :string => 'port 80')
|
173
|
+
Net::HTTP.start('example.com', 3000) do |http|
|
174
|
+
response = http.get('/')
|
175
|
+
assert_equal 'port 3000', response.body
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_content_for_registered_uri_with_get_method_only
|
180
|
+
FakeWeb.allow_net_connect = false
|
181
|
+
FakeWeb.register_uri(:get, "http://example.com/", :string => "test example content")
|
182
|
+
Net::HTTP.start('example.com') do |http|
|
183
|
+
assert_equal 'test example content', http.get('/').body
|
184
|
+
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.post('/', nil) }
|
185
|
+
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.put('/', nil) }
|
186
|
+
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.delete('/', nil) }
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_content_for_registered_uri_with_any_method_explicitly
|
191
|
+
FakeWeb.allow_net_connect = false
|
192
|
+
FakeWeb.register_uri(:any, "http://example.com/", :string => "test example content")
|
193
|
+
Net::HTTP.start('example.com') do |http|
|
194
|
+
assert_equal 'test example content', http.get('/').body
|
195
|
+
assert_equal 'test example content', http.post('/', nil).body
|
196
|
+
assert_equal 'test example content', http.put('/', nil).body
|
197
|
+
assert_equal 'test example content', http.delete('/').body
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_content_for_registered_uri_with_any_method_implicitly
|
202
|
+
FakeWeb.allow_net_connect = false
|
203
|
+
FakeWeb.register_uri("http://example.com/", :string => "test example content")
|
204
|
+
Net::HTTP.start('example.com') do |http|
|
205
|
+
assert_equal 'test example content', http.get('/').body
|
206
|
+
assert_equal 'test example content', http.post('/', nil).body
|
207
|
+
assert_equal 'test example content', http.put('/', nil).body
|
208
|
+
assert_equal 'test example content', http.delete('/').body
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_mock_request_with_block
|
213
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
214
|
+
Net::HTTP.start('mock') do |http|
|
215
|
+
response = http.get('/test_example.txt')
|
216
|
+
assert_equal 'test example content', response.body
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def test_mock_request_with_undocumented_full_uri_argument_style
|
221
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
222
|
+
Net::HTTP.start('mock') do |query|
|
223
|
+
response = query.get('http://mock/test_example.txt')
|
224
|
+
assert_equal 'test example content', response.body
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_mock_request_with_undocumented_full_uri_argument_style_and_query
|
229
|
+
FakeWeb.register_uri('http://mock/test_example.txt?a=b', :string => 'test query content')
|
230
|
+
Net::HTTP.start('mock') do |query|
|
231
|
+
response = query.get('http://mock/test_example.txt?a=b')
|
232
|
+
assert_equal 'test query content', response.body
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_mock_post
|
237
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
238
|
+
response = nil
|
239
|
+
Net::HTTP.start('mock') do |query|
|
240
|
+
response = query.post('/test_example.txt', '')
|
241
|
+
end
|
242
|
+
assert_equal 'test example content', response.body
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_mock_post_with_string_as_registered_uri
|
246
|
+
response = nil
|
247
|
+
FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
|
248
|
+
Net::HTTP.start('mock') do |query|
|
249
|
+
response = query.post('/test_string.txt', '')
|
250
|
+
end
|
251
|
+
assert_equal 'foo', response.body
|
252
|
+
end
|
253
|
+
|
254
|
+
def test_mock_get_with_request_as_registered_uri
|
255
|
+
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
|
256
|
+
FakeWeb.register_uri('http://mock/test_response', :response => fake_response)
|
257
|
+
response = nil
|
258
|
+
Net::HTTP.start('mock') do |query|
|
259
|
+
response = query.get('/test_response')
|
260
|
+
end
|
261
|
+
|
262
|
+
assert_equal fake_response, response
|
263
|
+
end
|
264
|
+
|
265
|
+
def test_mock_get_with_request_from_file_as_registered_uri
|
266
|
+
FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/test_request')
|
267
|
+
response = nil
|
268
|
+
Net::HTTP.start('www.google.com') do |query|
|
269
|
+
response = query.get('/')
|
270
|
+
end
|
271
|
+
assert_equal '200', response.code
|
272
|
+
assert response.body.include?('<title>Google</title>')
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_mock_post_with_request_from_file_as_registered_uri
|
276
|
+
FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/test_request')
|
277
|
+
response = nil
|
278
|
+
Net::HTTP.start('www.google.com') do |query|
|
279
|
+
response = query.post('/', '')
|
280
|
+
end
|
281
|
+
assert_equal "200", response.code
|
282
|
+
assert response.body.include?('<title>Google</title>')
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_proxy_request
|
286
|
+
FakeWeb.register_uri('http://www.example.com/', :string => "hello world")
|
287
|
+
FakeWeb.register_uri('http://your.proxy.host/', :string => "lala")
|
288
|
+
proxy_addr = 'your.proxy.host'
|
289
|
+
proxy_port = 8080
|
290
|
+
|
291
|
+
Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.example.com') do |http|
|
292
|
+
response = http.get('/')
|
293
|
+
assert_equal "hello world", response.body
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
def test_https_request
|
298
|
+
FakeWeb.register_uri('https://www.example.com/', :string => "Hello World")
|
299
|
+
http = Net::HTTP.new('www.example.com', 443)
|
300
|
+
http.use_ssl = true
|
301
|
+
response = http.get('/')
|
302
|
+
assert_equal "Hello World", response.body
|
303
|
+
end
|
304
|
+
|
305
|
+
def test_register_unimplemented_response
|
306
|
+
FakeWeb.register_uri('http://mock/unimplemented', :response => 1)
|
307
|
+
assert_raises StandardError do
|
308
|
+
Net::HTTP.start('mock') { |q| q.get('/unimplemented') }
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
def test_real_http_request
|
313
|
+
setup_expectations_for_real_apple_hot_news_request
|
314
|
+
|
315
|
+
resp = nil
|
316
|
+
Net::HTTP.start('images.apple.com') do |query|
|
317
|
+
resp = query.get('/main/rss/hotnews/hotnews.rss')
|
318
|
+
end
|
319
|
+
assert resp.body.include?('Apple')
|
320
|
+
assert resp.body.include?('News')
|
321
|
+
end
|
322
|
+
|
323
|
+
def test_real_http_request_with_undocumented_full_uri_argument_style
|
324
|
+
setup_expectations_for_real_apple_hot_news_request(:path => 'http://images.apple.com/main/rss/hotnews/hotnews.rss')
|
325
|
+
|
326
|
+
resp = nil
|
327
|
+
Net::HTTP.start('images.apple.com') do |query|
|
328
|
+
resp = query.get('http://images.apple.com/main/rss/hotnews/hotnews.rss')
|
329
|
+
end
|
330
|
+
assert resp.body.include?('Apple')
|
331
|
+
assert resp.body.include?('News')
|
332
|
+
end
|
333
|
+
|
334
|
+
def test_real_https_request
|
335
|
+
setup_expectations_for_real_apple_hot_news_request(:port => 443)
|
336
|
+
|
337
|
+
http = Net::HTTP.new('images.apple.com', 443)
|
338
|
+
http.use_ssl = true
|
339
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # silence certificate warning
|
340
|
+
response = http.get('/main/rss/hotnews/hotnews.rss')
|
341
|
+
assert response.body.include?('Apple')
|
342
|
+
assert response.body.include?('News')
|
343
|
+
end
|
344
|
+
|
345
|
+
def test_real_request_on_same_domain_as_mock
|
346
|
+
setup_expectations_for_real_apple_hot_news_request
|
347
|
+
|
348
|
+
FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
|
349
|
+
|
350
|
+
resp = nil
|
351
|
+
Net::HTTP.start('images.apple.com') do |query|
|
352
|
+
resp = query.get('/main/rss/hotnews/hotnews.rss')
|
353
|
+
end
|
354
|
+
assert resp.body.include?('Apple')
|
355
|
+
assert resp.body.include?('News')
|
356
|
+
end
|
357
|
+
|
358
|
+
def test_mock_request_on_real_domain
|
359
|
+
FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
|
360
|
+
resp = nil
|
361
|
+
Net::HTTP.start('images.apple.com') do |query|
|
362
|
+
resp = query.get('/test_string.txt')
|
363
|
+
end
|
364
|
+
assert_equal 'foo', resp.body
|
365
|
+
end
|
366
|
+
|
367
|
+
def test_mock_post_that_raises_exception
|
368
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
|
369
|
+
assert_raises(StandardError) do
|
370
|
+
Net::HTTP.start('mock') do |query|
|
371
|
+
query.post('/raising_exception.txt', 'some data')
|
372
|
+
end
|
373
|
+
end
|
374
|
+
end
|
375
|
+
|
376
|
+
def test_mock_post_that_raises_an_http_error
|
377
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError)
|
378
|
+
assert_raises(Net::HTTPError) do
|
379
|
+
Net::HTTP.start('mock') do |query|
|
380
|
+
query.post('/raising_exception.txt', '')
|
381
|
+
end
|
382
|
+
end
|
383
|
+
end
|
384
|
+
|
385
|
+
def test_mock_instance_syntax
|
386
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
387
|
+
response = nil
|
388
|
+
uri = URI.parse('http://mock/test_example.txt')
|
389
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
390
|
+
response = http.start do
|
391
|
+
http.get(uri.path)
|
392
|
+
end
|
393
|
+
|
394
|
+
assert_equal 'test example content', response.body
|
395
|
+
end
|
396
|
+
|
397
|
+
def test_mock_via_nil_proxy
|
398
|
+
response = nil
|
399
|
+
proxy_address = nil
|
400
|
+
proxy_port = nil
|
401
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
402
|
+
uri = URI.parse('http://mock/test_example.txt')
|
403
|
+
http = Net::HTTP::Proxy(proxy_address, proxy_port).new(
|
404
|
+
uri.host, (uri.port or 80))
|
405
|
+
response = http.start do
|
406
|
+
http.get(uri.path)
|
407
|
+
end
|
408
|
+
|
409
|
+
assert_equal 'test example content', response.body
|
410
|
+
end
|
411
|
+
|
412
|
+
def test_response_type
|
413
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :string => "test")
|
414
|
+
Net::HTTP.start('mock') do |http|
|
415
|
+
response = http.get('/test_example.txt', '')
|
416
|
+
assert_kind_of(Net::HTTPSuccess, response)
|
417
|
+
end
|
418
|
+
end
|
419
|
+
|
420
|
+
def test_mock_request_that_raises_an_http_error_with_a_specific_status
|
421
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
|
422
|
+
exception = assert_raises(Net::HTTPError) do
|
423
|
+
Net::HTTP.start('mock') { |http| response = http.get('/raising_exception.txt') }
|
424
|
+
end
|
425
|
+
assert_equal '404', exception.response.code
|
426
|
+
assert_equal 'Not Found', exception.response.msg
|
427
|
+
end
|
428
|
+
|
429
|
+
def test_mock_rotate_responses
|
430
|
+
FakeWeb.register_uri('http://mock/multiple_test_example.txt',
|
431
|
+
[ {:file => File.dirname(__FILE__) + '/fixtures/test_example.txt', :times => 2},
|
432
|
+
{:string => "thrice", :times => 3},
|
433
|
+
{:string => "ever_more"} ])
|
434
|
+
|
435
|
+
uri = URI.parse('http://mock/multiple_test_example.txt')
|
436
|
+
2.times { assert_equal 'test example content', Net::HTTP.get(uri) }
|
437
|
+
3.times { assert_equal 'thrice', Net::HTTP.get(uri) }
|
438
|
+
4.times { assert_equal 'ever_more', Net::HTTP.get(uri) }
|
439
|
+
end
|
440
|
+
|
441
|
+
def test_response_hit_calls_verify
|
442
|
+
verify_called = false
|
443
|
+
FakeWeb.register_uri('http://mock/verify_called',
|
444
|
+
:verify => lambda { |req|
|
445
|
+
assert_kind_of Net::HTTPRequest, req
|
446
|
+
verify_called = true
|
447
|
+
}
|
448
|
+
)
|
449
|
+
assert !verify_called
|
450
|
+
Net::HTTP.start('mock') { |http| response = http.get('/verify_called') }
|
451
|
+
assert verify_called
|
452
|
+
end
|
453
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class TestFakeWebOpenURI < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
FakeWeb.clean_registry
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_content_for_registered_uri
|
10
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
11
|
+
assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_mock_open
|
15
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
16
|
+
assert_equal 'test example content', open('http://mock/test_example.txt').read
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_mock_open_with_string_as_registered_uri
|
20
|
+
FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
|
21
|
+
assert_equal 'foo', open('http://mock/test_string.txt').string
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_real_open
|
25
|
+
setup_expectations_for_real_apple_hot_news_request
|
26
|
+
resp = open('http://images.apple.com/main/rss/hotnews/hotnews.rss')
|
27
|
+
assert_equal "200", resp.status.first
|
28
|
+
body = resp.read
|
29
|
+
assert body.include?('Apple')
|
30
|
+
assert body.include?('News')
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_mock_open_that_raises_exception
|
34
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
|
35
|
+
assert_raises(StandardError) do
|
36
|
+
open('http://mock/raising_exception.txt')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_mock_open_that_raises_an_http_error
|
41
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError)
|
42
|
+
assert_raises(OpenURI::HTTPError) do
|
43
|
+
open('http://mock/raising_exception.txt')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_mock_open_that_raises_an_http_error_with_a_specific_status
|
48
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError, :status => ['123', 'jodel'])
|
49
|
+
exception = assert_raises(OpenURI::HTTPError) do
|
50
|
+
open('http://mock/raising_exception.txt')
|
51
|
+
end
|
52
|
+
assert_equal '123', exception.io.code
|
53
|
+
assert_equal 'jodel', exception.io.message
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_mock_open_with_block
|
57
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
58
|
+
open('http://mock/test_example.txt') do |f|
|
59
|
+
assert 'test example content', f.readlines
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
$:.unshift "#{File.dirname(__FILE__)}/../lib"
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'open-uri'
|
5
|
+
require 'fake_web'
|
6
|
+
require 'rubygems'
|
7
|
+
require 'mocha'
|
8
|
+
|
9
|
+
module FakeWebTestHelper
|
10
|
+
|
11
|
+
# Sets several expectations (using Mocha) that a real HTTP request makes it
|
12
|
+
# past FakeWeb to the socket layer. You can use this when you need to check
|
13
|
+
# that a request isn't handled by FakeWeb.
|
14
|
+
def setup_expectations_for_real_request(options = {})
|
15
|
+
# Socket handling
|
16
|
+
if options[:port] == 443
|
17
|
+
socket = mock("SSLSocket")
|
18
|
+
OpenSSL::SSL::SSLSocket.expects(:===).with(socket).returns(true).at_least_once
|
19
|
+
OpenSSL::SSL::SSLSocket.expects(:new).with(socket, instance_of(OpenSSL::SSL::SSLContext)).returns(socket).at_least_once
|
20
|
+
socket.stubs(:sync_close=).returns(true)
|
21
|
+
socket.expects(:connect).with().at_least_once
|
22
|
+
else
|
23
|
+
socket = mock("TCPSocket")
|
24
|
+
Socket.expects(:===).with(socket).returns(true)
|
25
|
+
end
|
26
|
+
|
27
|
+
TCPSocket.expects(:open).with(options[:host], options[:port]).returns(socket).at_least_once
|
28
|
+
socket.stubs(:closed?).returns(false)
|
29
|
+
socket.stubs(:close).returns(true)
|
30
|
+
|
31
|
+
# Request/response handling
|
32
|
+
request_parts = ["#{options[:method]} #{options[:path]} HTTP/1.1", "Host: #{options[:host]}"]
|
33
|
+
socket.expects(:write).with(all_of(includes(request_parts[0]), includes(request_parts[1]))).returns(100)
|
34
|
+
|
35
|
+
# TODO: handle long response bodies that use more than one #sysread call
|
36
|
+
socket.expects(:sysread).with(1024).at_least_once.returns("HTTP/1.1 #{options[:response_code]} #{options[:response_message]}\nContent-Length: #{options[:response_body].length}\n\n#{options[:response_body]}").then.raises(EOFError)
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
# A helper that calls #setup_expectations_for_real_request for you, using
|
41
|
+
# defaults for our commonly used test request to images.apple.com.
|
42
|
+
def setup_expectations_for_real_apple_hot_news_request(options = {})
|
43
|
+
defaults = { :host => "images.apple.com", :port => 80, :method => "GET",
|
44
|
+
:path => "/main/rss/hotnews/hotnews.rss",
|
45
|
+
:response_code => 200, :response_message => "OK",
|
46
|
+
:response_body => "<title>Apple Hot News</title>" }
|
47
|
+
setup_expectations_for_real_request(defaults.merge(options))
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
Test::Unit::TestCase.send(:include, FakeWebTestHelper)
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class TestFakeWebQueryString < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
FakeWeb.clean_registry
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_register_uri_with_query_params
|
10
|
+
FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
|
11
|
+
assert FakeWeb.registered_uri?('http://example.com/?a=1&b=1')
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_register_uri_with_query_params_and_check_in_different_order
|
15
|
+
FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
|
16
|
+
assert FakeWeb.registered_uri?('http://example.com/?b=1&a=1')
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_registered_uri_gets_recognized_with_empty_query_params
|
20
|
+
FakeWeb.register_uri('http://example.com/', :string => 'foo')
|
21
|
+
assert FakeWeb.registered_uri?('http://example.com/?')
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_register_uri_with_empty_query_params_and_check_with_none
|
25
|
+
FakeWeb.register_uri('http://example.com/?', :string => 'foo')
|
26
|
+
assert FakeWeb.registered_uri?('http://example.com/')
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_registry_sort_query_params
|
30
|
+
assert_equal "a=1&b=2", FakeWeb::Registry.instance.send(:sort_query_params, "b=2&a=1")
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_registry_sort_query_params_sorts_by_value_if_keys_collide
|
34
|
+
assert_equal "a=1&a=2&b=2", FakeWeb::Registry.instance.send(:sort_query_params, "a=2&b=2&a=1")
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|