fakeweb 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,214 +0,0 @@
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_url_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_content_for_registered_uri
41
- assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
42
- end
43
-
44
- def test_mock_request_with_block
45
- Net::HTTP.start('mock') do |http|
46
- response = http.get('/test_example.txt')
47
- assert 'test example content', response.body
48
- end
49
- end
50
-
51
- def test_mock_post
52
- response = nil
53
- Net::HTTP.start('mock') do |query|
54
- response = query.post('/test_example.txt', '')
55
- end
56
- assert_equal 'test example content', response.body
57
- end
58
-
59
- def test_mock_post_with_string_as_registered_uri
60
- response = nil
61
- FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
62
- Net::HTTP.start('mock') do |query|
63
- response = query.post('/test_string.txt', '')
64
- end
65
- assert_equal 'foo', response.body
66
- end
67
-
68
- def test_mock_get_with_request_as_registered_uri
69
- fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
70
- FakeWeb.register_uri('http://mock/test_response', :response => fake_response)
71
- response = nil
72
- Net::HTTP.start('mock') do |query|
73
- response = query.get('/test_response')
74
- end
75
-
76
- assert_equal fake_response, response
77
- end
78
-
79
- def test_mock_get_with_request_from_file_as_registered_uri
80
- FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/../fixtures/test_request')
81
- response = nil
82
- Net::HTTP.start('www.google.com') do |query|
83
- response = query.get('/')
84
- end
85
- assert_equal '200', response.code
86
- assert response.body.include?('<title>Google</title>')
87
- end
88
-
89
- def test_mock_post_with_request_from_file_as_registered_uri
90
- FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/../fixtures/test_request')
91
- response = nil
92
- Net::HTTP.start('www.google.com') do |query|
93
- response = query.post('/', '')
94
- end
95
- assert_equal "200", response.code
96
- assert response.body.include?('<title>Google</title>')
97
- end
98
-
99
- def test_proxy_request
100
- FakeWeb.register_uri('http://www.example.com/', :string => "hello world")
101
- FakeWeb.register_uri('http://your.proxy.host/', :string => "lala")
102
- proxy_addr = 'your.proxy.host'
103
- proxy_port = 8080
104
-
105
- Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.example.com') do |http|
106
- response = http.get('/')
107
- assert_equal "hello world", response.body
108
- end
109
- end
110
-
111
- def test_https_request
112
- FakeWeb.register_uri('https://www.example.com/', :string => "Hello World")
113
- http = Net::HTTP.new('www.example.com', 443)
114
- http.use_ssl = true
115
- response = http.get('/')
116
- assert_equal "Hello World", response.body
117
- end
118
-
119
- def test_register_unimplemented_response
120
- FakeWeb.register_uri('http://mock/unimplemented', :response => 1)
121
- assert_raises StandardError do
122
- Net::HTTP.start('mock') { |q| q.get('/unimplemented') }
123
- end
124
- end
125
-
126
- def test_real_request
127
- resp = nil
128
- Net::HTTP.start('images.apple.com') do |query|
129
- resp = query.get('/main/rss/hotnews/hotnews.rss')
130
- end
131
- assert resp.body.include?('Apple')
132
- assert resp.body.include?('News')
133
- end
134
-
135
- def test_real_request_on_same_domain_as_mock
136
- FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
137
- resp = nil
138
- Net::HTTP.start('images.apple.com') do |query|
139
- resp = query.get('/main/rss/hotnews/hotnews.rss')
140
- end
141
- assert resp.body.include?('Apple')
142
- assert resp.body.include?('News')
143
- end
144
-
145
- def test_mock_request_on_real_domain
146
- FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
147
- resp = nil
148
- Net::HTTP.start('images.apple.com') do |query|
149
- resp = query.get('/test_string.txt')
150
- end
151
- assert_equal 'foo', resp.body
152
- end
153
-
154
- def test_mock_post_that_raises_exception
155
- FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
156
- assert_raises(StandardError) do
157
- Net::HTTP.start('mock') do |query|
158
- query.post('/raising_exception.txt', 'some data')
159
- end
160
- end
161
- end
162
-
163
- def test_mock_post_that_raises_an_http_error
164
- FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError)
165
- assert_raises(Net::HTTPError) do
166
- Net::HTTP.start('mock') do |query|
167
- query.post('/raising_exception.txt', '')
168
- end
169
- end
170
- end
171
-
172
- def test_mock_instance_syntax
173
- response = nil
174
- uri = URI.parse('http://mock/test_example.txt')
175
- http = Net::HTTP.new(uri.host, uri.port)
176
- response = http.start do
177
- http.get(uri.path)
178
- end
179
-
180
- assert_equal 'test example content', response.body
181
- end
182
-
183
- def test_mock_via_nil_proxy
184
- response = nil
185
- proxy_address = nil
186
- proxy_port = nil
187
-
188
- uri = URI.parse('http://mock/test_example.txt')
189
- http = Net::HTTP::Proxy(proxy_address, proxy_port).new(
190
- uri.host, (uri.port or 80))
191
- response = http.start do
192
- http.get(uri.path)
193
- end
194
- assert_equal 'test example content', response.body
195
-
196
- end
197
-
198
- def test_reponse_type
199
- Net::HTTP.start('mock') do |http|
200
- response = http.get('/test_example.txt', '')
201
- assert_kind_of(Net::HTTPSuccess, response)
202
- end
203
- end
204
-
205
- def test_mock_request_that_raises_an_http_error_with_a_specific_status
206
- FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
207
- exception = assert_raises(Net::HTTPError) do
208
- Net::HTTP.start('mock') { |http| response = http.get('/raising_exception.txt') }
209
- end
210
- assert_equal '404', exception.response.code
211
- assert_equal 'Not Found', exception.response.msg
212
- end
213
-
214
- end