mislav-fakeweb 1.1.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +80 -0
- data/LICENSE.txt +281 -0
- data/README.rdoc +160 -0
- data/Rakefile +57 -0
- data/lib/fake_web.rb +152 -0
- data/lib/fake_web/ext/net_http.rb +58 -0
- data/lib/fake_web/registry.rb +78 -0
- data/lib/fake_web/responder.rb +88 -0
- data/lib/fake_web/response.rb +10 -0
- data/lib/fake_web/socket_delegator.rb +24 -0
- data/test/fixtures/test_example.txt +1 -0
- data/test/fixtures/test_request +21 -0
- data/test/test_allow_net_connect.rb +41 -0
- data/test/test_fake_web.rb +453 -0
- data/test/test_fake_web_open_uri.rb +62 -0
- data/test/test_helper.rb +52 -0
- data/test/test_query_string.rb +37 -0
- metadata +84 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module FakeWeb
|
|
2
|
+
class SocketDelegator #:nodoc:
|
|
3
|
+
|
|
4
|
+
def initialize(delegate=nil)
|
|
5
|
+
@delegate = nil
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def method_missing(method, *args, &block)
|
|
9
|
+
if @delegate
|
|
10
|
+
@delegate.send(method, *args, &block)
|
|
11
|
+
else
|
|
12
|
+
self.send("my_#{method}", *args, &block)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def my_closed?
|
|
17
|
+
@closed ||= true
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def my_readuntil(*args)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -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> | <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> <a id=1a class=q href="/imghp?hl=en&tab=wi&ie=UTF-8">Images</a> <a id=2a class=q href="http://groups.google.com/grphp?hl=en&tab=wg&ie=UTF-8">Groups</a> <a id=4a class=q href="http://news.google.com/nwshp?hl=en&tab=wn&ie=UTF-8">News</a> <a id=5a class=q href="http://froogle.google.com/frghp?hl=en&tab=wf&ie=UTF-8">Froogle</a> <a id=7a class=q href="/maphp?hl=en&tab=wl&ie=UTF-8">Maps</a> <b><a href="/intl/en/options/" class=q>more »</a></b></font></td></tr></table><table cellspacing=0 cellpadding=0><tr><td width=25%> </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> <a href=/advanced_search?hl=en>Advanced Search</a><br> <a href=/preferences?hl=en>Preferences</a><br> <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 Programs</a> - <a href=/services/>Business Solutions</a> - <a href=/intl/en/about.html>About Google</a></font><p><font size=-2>©2006 Google</font></p></center></body></html>
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
|
+
|
|
3
|
+
class TestFakeWebAllowNetConnect < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def setup
|
|
6
|
+
@original_allow_net_connect = FakeWeb.allow_net_connect?
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def teardown
|
|
10
|
+
FakeWeb.allow_net_connect = @original_allow_net_connect
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_true
|
|
15
|
+
FakeWeb.allow_net_connect = true
|
|
16
|
+
setup_expectations_for_real_apple_hot_news_request
|
|
17
|
+
Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_raises_for_unregistered_requests_when_allow_net_connect_is_false
|
|
21
|
+
FakeWeb.allow_net_connect = false
|
|
22
|
+
exception = assert_raise FakeWeb::NetConnectNotAllowedError do
|
|
23
|
+
Net::HTTP.get(URI.parse('http://example.com/'))
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_question_mark_method_returns_true_after_setting_allow_net_connect_to_true
|
|
28
|
+
FakeWeb.allow_net_connect = true
|
|
29
|
+
assert FakeWeb.allow_net_connect?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_question_mark_method_returns_false_after_setting_allow_net_connect_to_false
|
|
33
|
+
FakeWeb.allow_net_connect = false
|
|
34
|
+
assert !FakeWeb.allow_net_connect?
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_allow_net_connect_is_true_by_default
|
|
38
|
+
assert FakeWeb.allow_net_connect?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
end
|
|
@@ -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
|