chrisk-fakeweb 1.1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ test example content
@@ -0,0 +1,21 @@
1
+ HTTP/1.1 200 OK
2
+ Cache-Control: private
3
+ Content-Type: text/html
4
+ Set-Cookie: PREF=ID=c1e3135f3180fabf:TM=1148188144:LM=1148188144:S=x9CJYC71XSKRA32Q; expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com
5
+ Server: GWS/2.1
6
+ Transfer-Encoding: chunked
7
+ Date: Sun, 21 May 2006 05:09:04 GMT
8
+
9
+ <html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"><title>Google</title><style><!--
10
+ body,td,a,p,.h{font-family:arial,sans-serif;}
11
+ .h{font-size: 20px;}
12
+ .q{color:#0000cc;}
13
+ -->
14
+ </style>
15
+ <script>
16
+ <!--
17
+ function sf(){document.f.q.focus();}
18
+ // -->
19
+ </script>
20
+ </head><body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000 onLoad=sf() topmargin=3 marginheight=3><center><table border=0 cellspacing=0 cellpadding=0 width=100%><tr><td align=right nowrap><font size=-1><a href="/url?sa=p&pref=ig&pval=2&q=http://www.google.com/ig%3Fhl%3Den">Personalized Home</a>&nbsp;|&nbsp;<a href="https://www.google.com/accounts/Login?continue=http://www.google.com/&hl=en">Sign in</a></font></td></tr><tr height=4><td><img alt="" width=1 height=1></td></tr></table><img src="/intl/en/images/logo.gif" width=276 height=110 alt="Google"><br><br>
21
+ <form action=/search name=f><table border=0 cellspacing=0 cellpadding=4><tr><td nowrap><font size=-1><b>Web</b>&nbsp;&nbsp;&nbsp;&nbsp;<a id=1a class=q href="/imghp?hl=en&tab=wi&ie=UTF-8">Images</a>&nbsp;&nbsp;&nbsp;&nbsp;<a id=2a class=q href="http://groups.google.com/grphp?hl=en&tab=wg&ie=UTF-8">Groups</a>&nbsp;&nbsp;&nbsp;&nbsp;<a id=4a class=q href="http://news.google.com/nwshp?hl=en&tab=wn&ie=UTF-8">News</a>&nbsp;&nbsp;&nbsp;&nbsp;<a id=5a class=q href="http://froogle.google.com/frghp?hl=en&tab=wf&ie=UTF-8">Froogle</a>&nbsp;&nbsp;&nbsp;&nbsp;<a id=7a class=q href="/maphp?hl=en&tab=wl&ie=UTF-8">Maps</a>&nbsp;&nbsp;&nbsp;&nbsp;<b><a href="/intl/en/options/" class=q>more&nbsp;&raquo;</a></b></font></td></tr></table><table cellspacing=0 cellpadding=0><tr><td width=25%>&nbsp;</td><td align=center><input type=hidden name=hl value=en><input type=hidden name=ie value="ISO-8859-1"><input maxlength=2048 size=55 name=q value="" title="Google Search"><br><input type=submit value="Google Search" name=btnG><input type=submit value="I'm Feeling Lucky" name=btnI></td><td valign=top nowrap width=25%><font size=-2>&nbsp;&nbsp;<a href=/advanced_search?hl=en>Advanced Search</a><br>&nbsp;&nbsp;<a href=/preferences?hl=en>Preferences</a><br>&nbsp;&nbsp;<a href=/language_tools?hl=en>Language Tools</a></font></td></tr></table></form><br><br><font size=-1><a href="/intl/en/ads/">Advertising&nbsp;Programs</a> - <a href=/services/>Business Solutions</a> - <a href=/intl/en/about.html>About Google</a></font><p><font size=-2>&copy;2006 Google</font></p></center></body></html>
@@ -0,0 +1,45 @@
1
+ $:.unshift "#{File.dirname(__FILE__)}/../lib"
2
+
3
+ require 'test/unit'
4
+ require 'fake_web'
5
+ require 'open-uri'
6
+
7
+ class FakeWebExampleTest < Test::Unit::TestCase
8
+ def test_request
9
+ FakeWeb.register_uri('http://example.com/test_me', :string => "Hello World!")
10
+ content = Net::HTTP.get(URI.parse('http://example.com/test_me'))
11
+ assert_equal "Hello World!", content
12
+ end
13
+
14
+ def test_request_with_response
15
+ FakeWeb.register_uri('http://www.google.com/', :response => `curl -is http://www.google.com/`)
16
+ Net::HTTP.start('www.google.com') do |req|
17
+ response = req.get('/')
18
+ if response.code == 200
19
+ assert_equal "OK", response.message
20
+ assert response.body.include?('<title>Google')
21
+ elsif response.code == 302
22
+ # Google redirects foreign sites to ccTLDs.
23
+ assert_equal "Found", response.message
24
+ assert response.body.include?('The document has moved')
25
+ end
26
+ end
27
+ end
28
+
29
+ def test_request_with_custom_status
30
+ FakeWeb.register_uri('http://example.com/', :string => "Nothing to be found 'round here",
31
+ :status => ['404', 'Not Found'])
32
+ Net::HTTP.start('example.com') do |req|
33
+ response = req.get('/')
34
+ assert_equal "404", response.code
35
+ assert_equal "Not Found", response.message
36
+ assert_equal "Nothing to be found 'round here", response.body
37
+ end
38
+ end
39
+
40
+ def test_open_uri
41
+ FakeWeb.register_uri('http://example.com/', :string => "Hello, World!")
42
+ content = open('http://example.com/').string
43
+ assert_equal "Hello, World!", content
44
+ end
45
+ end
@@ -0,0 +1,322 @@
1
+ # FakeWeb - Ruby Helper for Faking Web Requests
2
+ # Copyright 2006 Blaine Cook <romeda@gmail.com>.
3
+ #
4
+ # FakeWeb is free software; you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation; either version 2 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # FakeWeb is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with FakeWeb; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ $:.unshift "#{File.dirname(__FILE__)}/../lib"
19
+
20
+ require 'test/unit'
21
+ require 'fake_web'
22
+
23
+ class TestFakeWeb < Test::Unit::TestCase
24
+
25
+ def setup
26
+ FakeWeb.clean_registry
27
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
28
+ end
29
+
30
+ def test_register_uri
31
+ assert FakeWeb.registered_uri?('http://mock/test_example.txt')
32
+ end
33
+
34
+ def test_register_uri_without_domain_name
35
+ assert_raises URI::InvalidURIError do
36
+ FakeWeb.register_uri('test_example2.txt', File.dirname(__FILE__) + '/fixtures/test_example.txt')
37
+ end
38
+ end
39
+
40
+ def test_register_uri_with_port_and_check_with_port
41
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'foo')
42
+ assert FakeWeb.registered_uri?('http://example.com:3000/')
43
+ end
44
+
45
+ def test_register_uri_with_port_and_check_without_port
46
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'foo')
47
+ assert !FakeWeb.registered_uri?('http://example.com/')
48
+ end
49
+
50
+ def test_register_uri_with_default_port_for_http_and_check_without_port
51
+ FakeWeb.register_uri('http://example.com:80/', :string => 'foo')
52
+ assert FakeWeb.registered_uri?('http://example.com/')
53
+ end
54
+
55
+ def test_register_uri_with_default_port_for_https_and_check_without_port
56
+ FakeWeb.register_uri('https://example.com:443/', :string => 'foo')
57
+ assert FakeWeb.registered_uri?('https://example.com/')
58
+ end
59
+
60
+ def test_register_uri_with_no_port_for_http_and_check_with_default_port
61
+ FakeWeb.register_uri('http://example.com/', :string => 'foo')
62
+ assert FakeWeb.registered_uri?('http://example.com:80/')
63
+ end
64
+
65
+ def test_register_uri_with_no_port_for_https_and_check_with_default_port
66
+ FakeWeb.register_uri('https://example.com/', :string => 'foo')
67
+ assert FakeWeb.registered_uri?('https://example.com:443/')
68
+ end
69
+
70
+ def test_register_uri_with_query_params
71
+ FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
72
+ assert FakeWeb.registered_uri?('http://example.com/?a=1&b=1')
73
+ end
74
+
75
+ def test_register_uri_with_query_params_and_check_in_different_order
76
+ FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
77
+ assert FakeWeb.registered_uri?('http://example.com/?b=1&a=1')
78
+ end
79
+
80
+ def test_content_for_registered_uri
81
+ assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
82
+ end
83
+
84
+ def test_content_for_registered_uri_with_port_and_request_with_port
85
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'test example content')
86
+ Net::HTTP.start('example.com', 3000) do |http|
87
+ response = http.get('/')
88
+ assert_equal 'test example content', response.body
89
+ end
90
+ end
91
+
92
+ def test_content_for_registered_uri_with_default_port_for_http_and_request_without_port
93
+ FakeWeb.register_uri('http://example.com:80/', :string => 'test example content')
94
+ Net::HTTP.start('example.com') do |http|
95
+ response = http.get('/')
96
+ assert_equal 'test example content', response.body
97
+ end
98
+ end
99
+
100
+ def test_content_for_registered_uri_with_no_port_for_http_and_request_with_default_port
101
+ FakeWeb.register_uri('http://example.com/', :string => 'test example content')
102
+ Net::HTTP.start('example.com', 80) do |http|
103
+ response = http.get('/')
104
+ assert_equal 'test example content', response.body
105
+ end
106
+ end
107
+
108
+ def test_content_for_registered_uri_with_default_port_for_https_and_request_with_default_port
109
+ FakeWeb.register_uri('https://example.com:443/', :string => 'test example content')
110
+ http = Net::HTTP.new('example.com', 443)
111
+ http.use_ssl = true
112
+ response = http.get('/')
113
+ assert_equal 'test example content', response.body
114
+ end
115
+
116
+ def test_content_for_registered_uri_with_no_port_for_https_and_request_with_default_port
117
+ FakeWeb.register_uri('https://example.com/', :string => 'test example content')
118
+ http = Net::HTTP.new('example.com', 443)
119
+ http.use_ssl = true
120
+ response = http.get('/')
121
+ assert_equal 'test example content', response.body
122
+ end
123
+
124
+ def test_content_for_registered_uris_with_ports_on_same_domain_and_request_without_port
125
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'port 3000')
126
+ FakeWeb.register_uri('http://example.com/', :string => 'port 80')
127
+ Net::HTTP.start('example.com') do |http|
128
+ response = http.get('/')
129
+ assert_equal 'port 80', response.body
130
+ end
131
+ end
132
+
133
+ def test_content_for_registered_uris_with_ports_on_same_domain_and_request_with_port
134
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'port 3000')
135
+ FakeWeb.register_uri('http://example.com/', :string => 'port 80')
136
+ Net::HTTP.start('example.com', 3000) do |http|
137
+ response = http.get('/')
138
+ assert_equal 'port 3000', response.body
139
+ end
140
+ end
141
+
142
+
143
+ def test_mock_request_with_block
144
+ Net::HTTP.start('mock') do |http|
145
+ response = http.get('/test_example.txt')
146
+ assert_equal 'test example content', response.body
147
+ end
148
+ end
149
+
150
+ def test_mock_post
151
+ response = nil
152
+ Net::HTTP.start('mock') do |query|
153
+ response = query.post('/test_example.txt', '')
154
+ end
155
+ assert_equal 'test example content', response.body
156
+ end
157
+
158
+ def test_mock_post_with_string_as_registered_uri
159
+ response = nil
160
+ FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
161
+ Net::HTTP.start('mock') do |query|
162
+ response = query.post('/test_string.txt', '')
163
+ end
164
+ assert_equal 'foo', response.body
165
+ end
166
+
167
+ def test_mock_get_with_request_as_registered_uri
168
+ fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
169
+ FakeWeb.register_uri('http://mock/test_response', :response => fake_response)
170
+ response = nil
171
+ Net::HTTP.start('mock') do |query|
172
+ response = query.get('/test_response')
173
+ end
174
+
175
+ assert_equal fake_response, response
176
+ end
177
+
178
+ def test_mock_get_with_request_from_file_as_registered_uri
179
+ FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/test_request')
180
+ response = nil
181
+ Net::HTTP.start('www.google.com') do |query|
182
+ response = query.get('/')
183
+ end
184
+ assert_equal '200', response.code
185
+ assert response.body.include?('<title>Google</title>')
186
+ end
187
+
188
+ def test_mock_post_with_request_from_file_as_registered_uri
189
+ FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/test_request')
190
+ response = nil
191
+ Net::HTTP.start('www.google.com') do |query|
192
+ response = query.post('/', '')
193
+ end
194
+ assert_equal "200", response.code
195
+ assert response.body.include?('<title>Google</title>')
196
+ end
197
+
198
+ def test_proxy_request
199
+ FakeWeb.register_uri('http://www.example.com/', :string => "hello world")
200
+ FakeWeb.register_uri('http://your.proxy.host/', :string => "lala")
201
+ proxy_addr = 'your.proxy.host'
202
+ proxy_port = 8080
203
+
204
+ Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.example.com') do |http|
205
+ response = http.get('/')
206
+ assert_equal "hello world", response.body
207
+ end
208
+ end
209
+
210
+ def test_https_request
211
+ FakeWeb.register_uri('https://www.example.com/', :string => "Hello World")
212
+ http = Net::HTTP.new('www.example.com', 443)
213
+ http.use_ssl = true
214
+ response = http.get('/')
215
+ assert_equal "Hello World", response.body
216
+ end
217
+
218
+ def test_register_unimplemented_response
219
+ FakeWeb.register_uri('http://mock/unimplemented', :response => 1)
220
+ assert_raises StandardError do
221
+ Net::HTTP.start('mock') { |q| q.get('/unimplemented') }
222
+ end
223
+ end
224
+
225
+ def test_real_http_request
226
+ resp = nil
227
+ Net::HTTP.start('images.apple.com') do |query|
228
+ resp = query.get('/main/rss/hotnews/hotnews.rss')
229
+ end
230
+ assert resp.body.include?('Apple')
231
+ assert resp.body.include?('News')
232
+ end
233
+
234
+ def test_real_https_request
235
+ http = Net::HTTP.new('images.apple.com', 443)
236
+ http.use_ssl = true
237
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE # silence certificate warning
238
+ response = http.get('/main/rss/hotnews/hotnews.rss')
239
+ assert response.body.include?('Apple')
240
+ assert response.body.include?('News')
241
+ end
242
+
243
+ def test_real_request_on_same_domain_as_mock
244
+ FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
245
+ resp = nil
246
+ Net::HTTP.start('images.apple.com') do |query|
247
+ resp = query.get('/main/rss/hotnews/hotnews.rss')
248
+ end
249
+ assert resp.body.include?('Apple')
250
+ assert resp.body.include?('News')
251
+ end
252
+
253
+ def test_mock_request_on_real_domain
254
+ FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
255
+ resp = nil
256
+ Net::HTTP.start('images.apple.com') do |query|
257
+ resp = query.get('/test_string.txt')
258
+ end
259
+ assert_equal 'foo', resp.body
260
+ end
261
+
262
+ def test_mock_post_that_raises_exception
263
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
264
+ assert_raises(StandardError) do
265
+ Net::HTTP.start('mock') do |query|
266
+ query.post('/raising_exception.txt', 'some data')
267
+ end
268
+ end
269
+ end
270
+
271
+ def test_mock_post_that_raises_an_http_error
272
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError)
273
+ assert_raises(Net::HTTPError) do
274
+ Net::HTTP.start('mock') do |query|
275
+ query.post('/raising_exception.txt', '')
276
+ end
277
+ end
278
+ end
279
+
280
+ def test_mock_instance_syntax
281
+ response = nil
282
+ uri = URI.parse('http://mock/test_example.txt')
283
+ http = Net::HTTP.new(uri.host, uri.port)
284
+ response = http.start do
285
+ http.get(uri.path)
286
+ end
287
+
288
+ assert_equal 'test example content', response.body
289
+ end
290
+
291
+ def test_mock_via_nil_proxy
292
+ response = nil
293
+ proxy_address = nil
294
+ proxy_port = nil
295
+
296
+ uri = URI.parse('http://mock/test_example.txt')
297
+ http = Net::HTTP::Proxy(proxy_address, proxy_port).new(
298
+ uri.host, (uri.port or 80))
299
+ response = http.start do
300
+ http.get(uri.path)
301
+ end
302
+ assert_equal 'test example content', response.body
303
+
304
+ end
305
+
306
+ def test_reponse_type
307
+ Net::HTTP.start('mock') do |http|
308
+ response = http.get('/test_example.txt', '')
309
+ assert_kind_of(Net::HTTPSuccess, response)
310
+ end
311
+ end
312
+
313
+ def test_mock_request_that_raises_an_http_error_with_a_specific_status
314
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
315
+ exception = assert_raises(Net::HTTPError) do
316
+ Net::HTTP.start('mock') { |http| response = http.get('/raising_exception.txt') }
317
+ end
318
+ assert_equal '404', exception.response.code
319
+ assert_equal 'Not Found', exception.response.msg
320
+ end
321
+
322
+ end
@@ -0,0 +1,80 @@
1
+ # FakeWeb - Ruby Helper for Faking Web Requests
2
+ # Copyright 2006 Blaine Cook <romeda@gmail.com>.
3
+ #
4
+ # FakeWeb is free software; you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation; either version 2 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # FakeWeb is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with FakeWeb; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ $:.unshift "#{File.dirname(__FILE__)}/../lib"
19
+
20
+ require 'test/unit'
21
+ require 'open-uri'
22
+ require 'fake_web'
23
+
24
+ class TestFakeWebOpenURI < Test::Unit::TestCase
25
+
26
+ def setup
27
+ FakeWeb.clean_registry
28
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
29
+ end
30
+
31
+ def test_content_for_registered_uri
32
+ assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
33
+ end
34
+
35
+ def test_mock_open
36
+ assert_equal 'test example content', open('http://mock/test_example.txt').read
37
+ end
38
+
39
+ def test_mock_open_with_string_as_registered_uri
40
+ FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
41
+ assert_equal 'foo', open('http://mock/test_string.txt').string
42
+ end
43
+
44
+ def test_real_open
45
+ resp = open('http://images.apple.com/main/rss/hotnews/hotnews.rss')
46
+ assert_equal "200", resp.status.first
47
+ body = resp.read
48
+ assert body.include?('Apple')
49
+ assert body.include?('News')
50
+ end
51
+
52
+ def test_mock_open_that_raises_exception
53
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
54
+ assert_raises(StandardError) do
55
+ open('http://mock/raising_exception.txt')
56
+ end
57
+ end
58
+
59
+ def test_mock_open_that_raises_an_http_error
60
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError)
61
+ assert_raises(OpenURI::HTTPError) do
62
+ open('http://mock/raising_exception.txt')
63
+ end
64
+ end
65
+
66
+ def test_mock_open_that_raises_an_http_error_with_a_specific_status
67
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError, :status => ['123', 'jodel'])
68
+ exception = assert_raises(OpenURI::HTTPError) do
69
+ open('http://mock/raising_exception.txt')
70
+ end
71
+ assert_equal '123', exception.io.code
72
+ assert_equal 'jodel', exception.io.message
73
+ end
74
+
75
+ def test_mock_open_with_block
76
+ open('http://mock/test_example.txt') do |f|
77
+ assert 'test example content', f.readlines
78
+ end
79
+ end
80
+ end