Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 1545 lines (1414 sloc) 49.978 kB
6a381cd @thodg Added tests for custom multipart
thodg authored
1 # -*- encoding: utf-8 -*-
93e62e6 @nahi Extract test helper
authored
2 require File.expand_path('helper', File.dirname(__FILE__))
beaf135 * forgot to add new files in the previous commit.
nahi authored
3
4
5 class TestHTTPClient < Test::Unit::TestCase
93e62e6 @nahi Extract test helper
authored
6 include Helper
beaf135 * forgot to add new files in the previous commit.
nahi authored
7
8 def setup
93e62e6 @nahi Extract test helper
authored
9 super
beaf135 * forgot to add new files in the previous commit.
nahi authored
10 setup_server
11 setup_client
12 end
13
14 def teardown
93e62e6 @nahi Extract test helper
authored
15 super
beaf135 * forgot to add new files in the previous commit.
nahi authored
16 end
17
18 def test_initialize
19 setup_proxyserver
20 escape_noproxy do
21 @proxyio.string = ""
d7a8fe2 @nahi Use free port number for test server
authored
22 @client = HTTPClient.new(proxyurl)
23 assert_equal(URI.parse(proxyurl), @client.proxy)
24 assert_equal(200, @client.head(serverurl).status)
7008822 * test cleanups for 1.9
nahi authored
25 assert(/accept/ =~ @proxyio.string)
beaf135 * forgot to add new files in the previous commit.
nahi authored
26 end
27 end
28
29 def test_agent_name
30 @client = HTTPClient.new(nil, "agent_name_foo")
31 str = ""
32 @client.debug_dev = str
d7a8fe2 @nahi Use free port number for test server
authored
33 @client.get(serverurl)
beaf135 * forgot to add new files in the previous commit.
nahi authored
34 lines = str.split(/(?:\r?\n)+/)
35 assert_equal("= Request", lines[0])
36 assert_match(/^User-Agent: agent_name_foo/, lines[4])
37 end
38
39 def test_from
40 @client = HTTPClient.new(nil, nil, "from_bar")
41 str = ""
42 @client.debug_dev = str
d7a8fe2 @nahi Use free port number for test server
authored
43 @client.get(serverurl)
beaf135 * forgot to add new files in the previous commit.
nahi authored
44 lines = str.split(/(?:\r?\n)+/)
45 assert_equal("= Request", lines[0])
46 assert_match(/^From: from_bar/, lines[4])
47 end
48
49 def test_debug_dev
50 str = ""
51 @client.debug_dev = str
4fc7a2b * added tests for coverage. closes #189.
nahi authored
52 assert_equal(str.object_id, @client.debug_dev.object_id)
beaf135 * forgot to add new files in the previous commit.
nahi authored
53 assert(str.empty?)
d7a8fe2 @nahi Use free port number for test server
authored
54 @client.get(serverurl)
beaf135 * forgot to add new files in the previous commit.
nahi authored
55 assert(!str.empty?)
56 end
57
4fc7a2b * added tests for coverage. closes #189.
nahi authored
58 def test_debug_dev_stream
beaf135 * forgot to add new files in the previous commit.
nahi authored
59 str = ""
60 @client.debug_dev = str
d7a8fe2 @nahi Use free port number for test server
authored
61 conn = @client.get_async(serverurl)
4fc7a2b * added tests for coverage. closes #189.
nahi authored
62 Thread.pass while !conn.finished?
63 assert(!str.empty?)
64 end
65
66 def test_protocol_version_http09
67 @client.protocol_version = 'HTTP/0.9'
68 @client.debug_dev = str = ''
a176e7b * check test coverage on lib/httpclient/session.rb. see #189.
nahi authored
69 @client.test_loopback_http_response << "hello\nworld\n"
d7a8fe2 @nahi Use free port number for test server
authored
70 res = @client.get(serverurl + 'hello')
1949938 @nahi Do not depend on Float#to_s behavior. closes #36.
authored
71 assert_equal('0.9', res.http_version)
a176e7b * check test coverage on lib/httpclient/session.rb. see #189.
nahi authored
72 assert_equal(nil, res.status)
73 assert_equal(nil, res.reason)
74 assert_equal("hello\nworld\n", res.content)
beaf135 * forgot to add new files in the previous commit.
nahi authored
75 lines = str.split(/(?:\r?\n)+/)
76 assert_equal("= Request", lines[0])
77 assert_equal("! CONNECTION ESTABLISHED", lines[2])
78 assert_equal("GET /hello HTTP/0.9", lines[3])
79 assert_equal("Connection: close", lines[5])
80 assert_equal("= Response", lines[6])
a176e7b * check test coverage on lib/httpclient/session.rb. see #189.
nahi authored
81 assert_match(/^hello$/, lines[7])
82 assert_match(/^world$/, lines[8])
beaf135 * forgot to add new files in the previous commit.
nahi authored
83 end
84
85 def test_protocol_version_http10
4fc7a2b * added tests for coverage. closes #189.
nahi authored
86 assert_equal(nil, @client.protocol_version)
beaf135 * forgot to add new files in the previous commit.
nahi authored
87 @client.protocol_version = 'HTTP/1.0'
4fc7a2b * added tests for coverage. closes #189.
nahi authored
88 assert_equal('HTTP/1.0', @client.protocol_version)
beaf135 * forgot to add new files in the previous commit.
nahi authored
89 str = ""
90 @client.debug_dev = str
d7a8fe2 @nahi Use free port number for test server
authored
91 @client.get(serverurl + 'hello')
beaf135 * forgot to add new files in the previous commit.
nahi authored
92 lines = str.split(/(?:\r?\n)+/)
93 assert_equal("= Request", lines[0])
94 assert_equal("! CONNECTION ESTABLISHED", lines[2])
95 assert_equal("GET /hello HTTP/1.0", lines[3])
96 assert_equal("Connection: close", lines[5])
97 assert_equal("= Response", lines[6])
98 end
99
58bde3a @nahi Do not override given Host header. closes #19.
authored
100 def test_host_given
101 str = ""
102 @client.debug_dev = str
d7a8fe2 @nahi Use free port number for test server
authored
103 @client.get(serverurl)
58bde3a @nahi Do not override given Host header. closes #19.
authored
104 lines = str.split(/(?:\r?\n)+/)
105 assert_equal("= Request", lines[0])
106 assert_equal("! CONNECTION ESTABLISHED", lines[2])
107 assert_equal("GET / HTTP/1.1", lines[3])
d7a8fe2 @nahi Use free port number for test server
authored
108 assert_equal("Host: localhost:#{serverport}", lines[5])
58bde3a @nahi Do not override given Host header. closes #19.
authored
109 #
110 @client.reset_all
111 str = ""
112 @client.debug_dev = str
d7a8fe2 @nahi Use free port number for test server
authored
113 @client.get(serverurl, nil, {'Host' => 'foo'})
58bde3a @nahi Do not override given Host header. closes #19.
authored
114 lines = str.split(/(?:\r?\n)+/)
115 assert_equal("= Request", lines[0])
116 assert_equal("! CONNECTION ESTABLISHED", lines[2])
117 assert_equal("GET / HTTP/1.1", lines[3])
118 assert_equal("Host: foo", lines[4]) # use given param
119 end
120
beaf135 * forgot to add new files in the previous commit.
nahi authored
121 def test_protocol_version_http11
4fc7a2b * added tests for coverage. closes #189.
nahi authored
122 assert_equal(nil, @client.protocol_version)
beaf135 * forgot to add new files in the previous commit.
nahi authored
123 str = ""
124 @client.debug_dev = str
d7a8fe2 @nahi Use free port number for test server
authored
125 @client.get(serverurl)
beaf135 * forgot to add new files in the previous commit.
nahi authored
126 lines = str.split(/(?:\r?\n)+/)
127 assert_equal("= Request", lines[0])
128 assert_equal("! CONNECTION ESTABLISHED", lines[2])
129 assert_equal("GET / HTTP/1.1", lines[3])
d7a8fe2 @nahi Use free port number for test server
authored
130 assert_equal("Host: localhost:#{serverport}", lines[5])
beaf135 * forgot to add new files in the previous commit.
nahi authored
131 @client.protocol_version = 'HTTP/1.1'
4fc7a2b * added tests for coverage. closes #189.
nahi authored
132 assert_equal('HTTP/1.1', @client.protocol_version)
beaf135 * forgot to add new files in the previous commit.
nahi authored
133 str = ""
134 @client.debug_dev = str
d7a8fe2 @nahi Use free port number for test server
authored
135 @client.get(serverurl)
beaf135 * forgot to add new files in the previous commit.
nahi authored
136 lines = str.split(/(?:\r?\n)+/)
137 assert_equal("= Request", lines[0])
138 assert_equal("! CONNECTION ESTABLISHED", lines[2])
139 assert_equal("GET / HTTP/1.1", lines[3])
140 @client.protocol_version = 'HTTP/1.0'
141 str = ""
142 @client.debug_dev = str
d7a8fe2 @nahi Use free port number for test server
authored
143 @client.get(serverurl)
beaf135 * forgot to add new files in the previous commit.
nahi authored
144 lines = str.split(/(?:\r?\n)+/)
145 assert_equal("= Request", lines[0])
146 assert_equal("! CONNECTION ESTABLISHED", lines[2])
147 assert_equal("GET / HTTP/1.0", lines[3])
148 end
149
150 def test_proxy
151 setup_proxyserver
152 escape_noproxy do
153 assert_raises(URI::InvalidURIError) do
154 @client.proxy = "http://"
155 end
796a731 @nahi Regression: setting proxy with URI
authored
156 @client.proxy = ""
157 assert_nil(@client.proxy)
4fc7a2b * added tests for coverage. closes #189.
nahi authored
158 @client.proxy = "http://admin:admin@foo:1234"
159 assert_equal(URI.parse("http://admin:admin@foo:1234"), @client.proxy)
beaf135 * forgot to add new files in the previous commit.
nahi authored
160 uri = URI.parse("http://bar:2345")
161 @client.proxy = uri
162 assert_equal(uri, @client.proxy)
163 #
164 @proxyio.string = ""
165 @client.proxy = nil
d7a8fe2 @nahi Use free port number for test server
authored
166 assert_equal(200, @client.head(serverurl).status)
7008822 * test cleanups for 1.9
nahi authored
167 assert(/accept/ !~ @proxyio.string)
beaf135 * forgot to add new files in the previous commit.
nahi authored
168 #
169 @proxyio.string = ""
d7a8fe2 @nahi Use free port number for test server
authored
170 @client.proxy = proxyurl
d202412 * added test for #195.
nahi authored
171 @client.debug_dev = str = ""
d7a8fe2 @nahi Use free port number for test server
authored
172 assert_equal(200, @client.head(serverurl).status)
7008822 * test cleanups for 1.9
nahi authored
173 assert(/accept/ =~ @proxyio.string)
d7a8fe2 @nahi Use free port number for test server
authored
174 assert(/Host: localhost:#{serverport}/ =~ str)
beaf135 * forgot to add new files in the previous commit.
nahi authored
175 end
176 end
177
d202412 * added test for #195.
nahi authored
178 def test_host_header
d7a8fe2 @nahi Use free port number for test server
authored
179 @client.proxy = proxyurl
d202412 * added test for #195.
nahi authored
180 @client.debug_dev = str = ""
181 @client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"
182 assert_equal(200, @client.head('http://www.example.com/foo').status)
183 # ensure no ':80' is added. some servers dislike that.
184 assert(/\r\nHost: www\.example\.com\r\n/ =~ str)
185 #
186 @client.debug_dev = str = ""
187 @client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"
188 assert_equal(200, @client.head('http://www.example.com:12345/foo').status)
189 # ensure ':12345' exists.
190 assert(/\r\nHost: www\.example\.com:12345\r\n/ =~ str)
191 end
192
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
193 def test_proxy_env
194 setup_proxyserver
195 escape_env do
196 ENV['http_proxy'] = "http://admin:admin@foo:1234"
197 ENV['NO_PROXY'] = "foobar"
198 client = HTTPClient.new
199 assert_equal(URI.parse("http://admin:admin@foo:1234"), client.proxy)
200 assert_equal('foobar', client.no_proxy)
201 end
202 end
203
204 def test_proxy_env_cgi
205 setup_proxyserver
206 escape_env do
207 ENV['REQUEST_METHOD'] = 'GET' # CGI environment emulation
208 ENV['http_proxy'] = "http://admin:admin@foo:1234"
209 ENV['no_proxy'] = "foobar"
210 client = HTTPClient.new
211 assert_equal(nil, client.proxy)
212 ENV['CGI_HTTP_PROXY'] = "http://admin:admin@foo:1234"
213 client = HTTPClient.new
214 assert_equal(URI.parse("http://admin:admin@foo:1234"), client.proxy)
215 end
216 end
217
f29310d @ashb Support empty (but set) http_proxy ENV variable
ashb authored
218 def test_empty_proxy_env
219 setup_proxyserver
220 escape_env do
221 ENV['http_proxy'] = ""
222 client = HTTPClient.new
223 assert_equal(nil, client.proxy)
224 end
225 end
226
beaf135 * forgot to add new files in the previous commit.
nahi authored
227 def test_noproxy_for_localhost
228 @proxyio.string = ""
d7a8fe2 @nahi Use free port number for test server
authored
229 @client.proxy = proxyurl
230 assert_equal(200, @client.head(serverurl).status)
7008822 * test cleanups for 1.9
nahi authored
231 assert(/accept/ !~ @proxyio.string)
beaf135 * forgot to add new files in the previous commit.
nahi authored
232 end
233
234 def test_no_proxy
235 setup_proxyserver
236 escape_noproxy do
237 # proxy is not set.
4fc7a2b * added tests for coverage. closes #189.
nahi authored
238 assert_equal(nil, @client.no_proxy)
beaf135 * forgot to add new files in the previous commit.
nahi authored
239 @client.no_proxy = 'localhost'
4fc7a2b * added tests for coverage. closes #189.
nahi authored
240 assert_equal('localhost', @client.no_proxy)
beaf135 * forgot to add new files in the previous commit.
nahi authored
241 @proxyio.string = ""
242 @client.proxy = nil
d7a8fe2 @nahi Use free port number for test server
authored
243 assert_equal(200, @client.head(serverurl).status)
7008822 * test cleanups for 1.9
nahi authored
244 assert(/accept/ !~ @proxyio.string)
beaf135 * forgot to add new files in the previous commit.
nahi authored
245 #
246 @proxyio.string = ""
d7a8fe2 @nahi Use free port number for test server
authored
247 @client.proxy = proxyurl
248 assert_equal(200, @client.head(serverurl).status)
7008822 * test cleanups for 1.9
nahi authored
249 assert(/accept/ !~ @proxyio.string)
beaf135 * forgot to add new files in the previous commit.
nahi authored
250 #
251 @client.no_proxy = 'foobar'
252 @proxyio.string = ""
d7a8fe2 @nahi Use free port number for test server
authored
253 @client.proxy = proxyurl
254 assert_equal(200, @client.head(serverurl).status)
7008822 * test cleanups for 1.9
nahi authored
255 assert(/accept/ =~ @proxyio.string)
beaf135 * forgot to add new files in the previous commit.
nahi authored
256 #
257 @client.no_proxy = 'foobar,localhost:baz'
258 @proxyio.string = ""
d7a8fe2 @nahi Use free port number for test server
authored
259 @client.proxy = proxyurl
260 assert_equal(200, @client.head(serverurl).status)
7008822 * test cleanups for 1.9
nahi authored
261 assert(/accept/ !~ @proxyio.string)
beaf135 * forgot to add new files in the previous commit.
nahi authored
262 #
263 @client.no_proxy = 'foobar,localhost:443'
264 @proxyio.string = ""
d7a8fe2 @nahi Use free port number for test server
authored
265 @client.proxy = proxyurl
266 assert_equal(200, @client.head(serverurl).status)
7008822 * test cleanups for 1.9
nahi authored
267 assert(/accept/ =~ @proxyio.string)
beaf135 * forgot to add new files in the previous commit.
nahi authored
268 #
d7a8fe2 @nahi Use free port number for test server
authored
269 @client.no_proxy = "foobar,localhost:443:localhost:#{serverport},baz"
beaf135 * forgot to add new files in the previous commit.
nahi authored
270 @proxyio.string = ""
d7a8fe2 @nahi Use free port number for test server
authored
271 @client.proxy = proxyurl
272 assert_equal(200, @client.head(serverurl).status)
7008822 * test cleanups for 1.9
nahi authored
273 assert(/accept/ !~ @proxyio.string)
beaf135 * forgot to add new files in the previous commit.
nahi authored
274 end
275 end
276
cf55b01 * Cookie header is not set in authentication negotiation. do not reu…
nahi authored
277 def test_cookie_update_while_authentication
278 escape_noproxy do
279 @client.test_loopback_http_response << <<EOS
280 HTTP/1.0 401\r
281 Date: Fri, 19 Dec 2008 11:57:29 GMT\r
282 Content-Type: text/plain\r
283 Content-Length: 0\r
284 WWW-Authenticate: Basic realm="hello"\r
c156a0f @nahi Make test TZ-free
authored
285 Set-Cookie: foo=bar; path=/; domain=.example.org; expires=#{Time.at(1924873200).httpdate}\r
cf55b01 * Cookie header is not set in authentication negotiation. do not reu…
nahi authored
286 \r
287 EOS
288 @client.test_loopback_http_response << <<EOS
289 HTTP/1.1 200 OK\r
290 Content-Length: 5\r
291 Connection: close\r
292 \r
293 hello
294 EOS
295 @client.debug_dev = str = ''
296 @client.set_auth("http://www.example.org/baz/", 'admin', 'admin')
297 assert_equal('hello', @client.get('http://www.example.org/baz/foo').content)
298 assert_match(/^Cookie: foo=bar/, str)
299 assert_match(/^Authorization: Basic YWRtaW46YWRtaW4=/, str)
300 end
301 end
302
303
ac0fa20 * added a test for CONNECT proxy auth.
nahi authored
304 def test_proxy_ssl
305 escape_noproxy do
306 @client.proxy = 'http://admin:admin@localhost:8080/'
307 # disconnected at initial 'CONNECT' so there're 2 loopback responses
308 @client.test_loopback_http_response << <<EOS
309 HTTP/1.0 407 Proxy Authentication Required\r
310 Date: Fri, 19 Dec 2008 11:57:29 GMT\r
311 Content-Type: text/plain\r
312 Content-Length: 0\r
313 Proxy-Authenticate: Basic realm="hello"\r
314 Proxy-Connection: close\r
315 \r
316 EOS
317 @client.test_loopback_http_response << <<EOS
318 HTTP/1.0 200 Connection established\r
319 \r
320 HTTP/1.1 200 OK\r
321 Content-Length: 5\r
322 Connection: close\r
323 \r
324 hello
325 EOS
326 assert_equal('hello', @client.get('https://localhost:17171/baz').content)
327 end
328 end
329
4fc7a2b * added tests for coverage. closes #189.
nahi authored
330 def test_loopback_response
331 @client.test_loopback_response << 'message body 1'
332 @client.test_loopback_response << 'message body 2'
333 assert_equal('message body 1', @client.get_content('http://somewhere'))
334 assert_equal('message body 2', @client.get_content('http://somewhere'))
335 #
336 @client.debug_dev = str = ''
337 @client.test_loopback_response << 'message body 3'
338 assert_equal('message body 3', @client.get_content('http://somewhere'))
339 assert_match(/message body 3/, str)
340 end
341
342 def test_loopback_response_stream
343 @client.test_loopback_response << 'message body 1'
344 @client.test_loopback_response << 'message body 2'
345 conn = @client.get_async('http://somewhere')
346 Thread.pass while !conn.finished?
347 assert_equal('message body 1', conn.pop.content.read)
348 conn = @client.get_async('http://somewhere')
349 Thread.pass while !conn.finished?
350 assert_equal('message body 2', conn.pop.content.read)
351 end
352
353 def test_loopback_http_response
354 @client.test_loopback_http_response << "HTTP/1.0 200 OK\ncontent-length: 100\n\nmessage body 1"
355 @client.test_loopback_http_response << "HTTP/1.0 200 OK\ncontent-length: 100\n\nmessage body 2"
356 assert_equal('message body 1', @client.get_content('http://somewhere'))
357 assert_equal('message body 2', @client.get_content('http://somewhere'))
358 end
359
7968719 @nahi multiline response message header support.
authored
360 def test_multiline_header
361 @client.test_loopback_http_response << "HTTP/1.0 200 OK\nX-Foo: XXX\n YYY\nX-Bar: \n XXX\n\tYYY\ncontent-length: 100\n\nmessage body 1"
362 res = @client.get('http://somewhere')
363 assert_equal('message body 1', res.content)
364 assert_equal(['XXX YYY'], res.header['x-foo'])
365 assert_equal(['XXX YYY'], res.header['x-bar'])
366 end
367
4fc7a2b * added tests for coverage. closes #189.
nahi authored
368 def test_broken_header
369 @client.test_loopback_http_response << "HTTP/1.0 200 OK\nXXXXX\ncontent-length: 100\n\nmessage body 1"
7968719 @nahi multiline response message header support.
authored
370 res = @client.get('http://somewhere')
371 assert_equal('message body 1', res.content)
4fc7a2b * added tests for coverage. closes #189.
nahi authored
372 end
373
5fb4436 * Security fix introduced at 2.1.3.
nahi authored
374 def test_redirect_non_https
d7a8fe2 @nahi Use free port number for test server
authored
375 url = serverurl + 'redirect1'
5fb4436 * Security fix introduced at 2.1.3.
nahi authored
376 https_url = URI.parse(url)
377 https_url.scheme = 'https'
378 #
379 redirect_to_http = "HTTP/1.0 302 OK\nLocation: #{url}\n\n"
380 redirect_to_https = "HTTP/1.0 302 OK\nLocation: #{https_url}\n\n"
381 #
382 # https -> http is denied
383 @client.test_loopback_http_response << redirect_to_http
384 assert_raises(HTTPClient::BadResponseError) do
385 @client.get_content(https_url)
386 end
387 #
388 # http -> http is OK
389 @client.reset_all
390 @client.test_loopback_http_response << redirect_to_http
391 assert_equal('hello', @client.get_content(url))
392 #
393 # http -> https is OK
394 @client.reset_all
395 @client.test_loopback_http_response << redirect_to_https
396 assert_raises(OpenSSL::SSL::SSLError) do
397 # trying to normal endpoint with SSL -> SSL negotiation failure
398 @client.get_content(url)
399 end
400 #
401 # https -> https is OK
402 @client.reset_all
403 @client.test_loopback_http_response << redirect_to_https
404 assert_raises(OpenSSL::SSL::SSLError) do
405 # trying to normal endpoint with SSL -> SSL negotiation failure
406 @client.get_content(https_url)
407 end
a620b5f @nahi Invalidate pooled sessions for the same destination when we got a Kee…
authored
408 #
409 # https -> http with strict_redirect_uri_callback
410 @client.redirect_uri_callback = @client.method(:strict_redirect_uri_callback)
411 @client.test_loopback_http_response << redirect_to_http
412 assert_raises(HTTPClient::BadResponseError) do
413 @client.get_content(https_url)
414 end
5fb4436 * Security fix introduced at 2.1.3.
nahi authored
415 end
416
4fc7a2b * added tests for coverage. closes #189.
nahi authored
417 def test_redirect_relative
418 @client.test_loopback_http_response << "HTTP/1.0 302 OK\nLocation: hello\n\n"
d7a8fe2 @nahi Use free port number for test server
authored
419 assert_equal('hello', @client.get_content(serverurl + 'redirect1'))
4fc7a2b * added tests for coverage. closes #189.
nahi authored
420 #
421 @client.reset_all
422 @client.redirect_uri_callback = @client.method(:strict_redirect_uri_callback)
d7a8fe2 @nahi Use free port number for test server
authored
423 assert_equal('hello', @client.get_content(serverurl + 'redirect1'))
4fc7a2b * added tests for coverage. closes #189.
nahi authored
424 @client.reset_all
425 @client.test_loopback_http_response << "HTTP/1.0 302 OK\nLocation: hello\n\n"
5ddb7c5 * added 'res' attribute to HTTPClient::BadResponse. you can get res…
nahi authored
426 begin
d7a8fe2 @nahi Use free port number for test server
authored
427 @client.get_content(serverurl + 'redirect1')
5ddb7c5 * added 'res' attribute to HTTPClient::BadResponse. you can get res…
nahi authored
428 assert(false)
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
429 rescue HTTPClient::BadResponseError => e
5ddb7c5 * added 'res' attribute to HTTPClient::BadResponse. you can get res…
nahi authored
430 assert_equal(302, e.res.status)
4fc7a2b * added tests for coverage. closes #189.
nahi authored
431 end
432 end
433
8a94b21 @nahi Fixed NME for non absolute-URI redirect on https. closes #20.
authored
434 def test_redirect_https_relative
d7a8fe2 @nahi Use free port number for test server
authored
435 url = serverurl + 'redirect1'
8a94b21 @nahi Fixed NME for non absolute-URI redirect on https. closes #20.
authored
436 https_url = URI.parse(url)
437 https_url.scheme = 'https'
438 @client.test_loopback_http_response << "HTTP/1.0 302 OK\nLocation: /foo\n\n"
439 @client.test_loopback_http_response << "HTTP/1.0 200 OK\n\nhello"
440 assert_equal('hello', @client.get_content(https_url))
441 end
442
d24ab1d @nahi do not try to read when a response code is 1XX, 204 or 304. closes #10.
authored
443 def test_no_content
444 assert_nothing_raised do
445 timeout(2) do
d7a8fe2 @nahi Use free port number for test server
authored
446 @client.get(serverurl + 'status', :status => 101)
447 @client.get(serverurl + 'status', :status => 204)
448 @client.get(serverurl + 'status', :status => 304)
d24ab1d @nahi do not try to read when a response code is 1XX, 204 or 304. closes #10.
authored
449 end
450 end
451 end
452
beaf135 * forgot to add new files in the previous commit.
nahi authored
453 def test_get_content
d7a8fe2 @nahi Use free port number for test server
authored
454 assert_equal('hello', @client.get_content(serverurl + 'hello'))
455 assert_equal('hello', @client.get_content(serverurl + 'redirect1'))
456 assert_equal('hello', @client.get_content(serverurl + 'redirect2'))
457 url = serverurl.sub(/localhost/, '127.0.0.1')
4fc7a2b * added tests for coverage. closes #189.
nahi authored
458 assert_equal('hello', @client.get_content(url + 'hello'))
459 assert_equal('hello', @client.get_content(url + 'redirect1'))
460 assert_equal('hello', @client.get_content(url + 'redirect2'))
d7a8fe2 @nahi Use free port number for test server
authored
461 @client.reset(serverurl)
4fc7a2b * added tests for coverage. closes #189.
nahi authored
462 @client.reset(url)
d7a8fe2 @nahi Use free port number for test server
authored
463 @client.reset(serverurl)
4fc7a2b * added tests for coverage. closes #189.
nahi authored
464 @client.reset(url)
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
465 assert_raises(HTTPClient::BadResponseError) do
d7a8fe2 @nahi Use free port number for test server
authored
466 @client.get_content(serverurl + 'notfound')
beaf135 * forgot to add new files in the previous commit.
nahi authored
467 end
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
468 assert_raises(HTTPClient::BadResponseError) do
d7a8fe2 @nahi Use free port number for test server
authored
469 @client.get_content(serverurl + 'redirect_self')
beaf135 * forgot to add new files in the previous commit.
nahi authored
470 end
471 called = false
472 @client.redirect_uri_callback = lambda { |uri, res|
473 newuri = res.header['location'][0]
474 called = true
475 newuri
476 }
d7a8fe2 @nahi Use free port number for test server
authored
477 assert_equal('hello', @client.get_content(serverurl + 'relative_redirect'))
beaf135 * forgot to add new files in the previous commit.
nahi authored
478 assert(called)
479 end
480
95964f1 @nahi Added transparent_gzip_decompression property. closes #42.
authored
481 GZIP_CONTENT = "\x1f\x8b\x08\x00\x1a\x96\xe0\x4c\x00\x03\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36\x05\x00\x00\x00"
482 DEFLATE_CONTENT = "\x78\x9c\xcb\x48\xcd\xc9\xc9\x07\x00\x06\x2c\x02\x15"
483 GZIP_CONTENT.force_encoding('BINARY') if GZIP_CONTENT.respond_to?(:force_encoding)
484 DEFLATE_CONTENT.force_encoding('BINARY') if DEFLATE_CONTENT.respond_to?(:force_encoding)
485 def test_get_gzipped_content
486 @client.transparent_gzip_decompression = false
d7a8fe2 @nahi Use free port number for test server
authored
487 content = @client.get_content(serverurl + 'compressed?enc=gzip')
95964f1 @nahi Added transparent_gzip_decompression property. closes #42.
authored
488 assert_not_equal('hello', content)
489 assert_equal(GZIP_CONTENT, content)
490 @client.transparent_gzip_decompression = true
d7a8fe2 @nahi Use free port number for test server
authored
491 assert_equal('hello', @client.get_content(serverurl + 'compressed?enc=gzip'))
492 assert_equal('hello', @client.get_content(serverurl + 'compressed?enc=deflate'))
95964f1 @nahi Added transparent_gzip_decompression property. closes #42.
authored
493 @client.transparent_gzip_decompression = false
494 end
495
c9d999f * get_content with block yielded unexpected message body. closes #173.
nahi authored
496 def test_get_content_with_block
d7a8fe2 @nahi Use free port number for test server
authored
497 @client.get_content(serverurl + 'hello') do |str|
c9d999f * get_content with block yielded unexpected message body. closes #173.
nahi authored
498 assert_equal('hello', str)
499 end
d7a8fe2 @nahi Use free port number for test server
authored
500 @client.get_content(serverurl + 'redirect1') do |str|
c9d999f * get_content with block yielded unexpected message body. closes #173.
nahi authored
501 assert_equal('hello', str)
502 end
d7a8fe2 @nahi Use free port number for test server
authored
503 @client.get_content(serverurl + 'redirect2') do |str|
c9d999f * get_content with block yielded unexpected message body. closes #173.
nahi authored
504 assert_equal('hello', str)
505 end
506 end
507
beaf135 * forgot to add new files in the previous commit.
nahi authored
508 def test_post_content
d7a8fe2 @nahi Use free port number for test server
authored
509 assert_equal('hello', @client.post_content(serverurl + 'hello'))
510 assert_equal('hello', @client.post_content(serverurl + 'redirect1'))
511 assert_equal('hello', @client.post_content(serverurl + 'redirect2'))
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
512 assert_raises(HTTPClient::BadResponseError) do
d7a8fe2 @nahi Use free port number for test server
authored
513 @client.post_content(serverurl + 'notfound')
beaf135 * forgot to add new files in the previous commit.
nahi authored
514 end
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
515 assert_raises(HTTPClient::BadResponseError) do
d7a8fe2 @nahi Use free port number for test server
authored
516 @client.post_content(serverurl + 'redirect_self')
beaf135 * forgot to add new files in the previous commit.
nahi authored
517 end
518 called = false
519 @client.redirect_uri_callback = lambda { |uri, res|
520 newuri = res.header['location'][0]
521 called = true
522 newuri
523 }
d7a8fe2 @nahi Use free port number for test server
authored
524 assert_equal('hello', @client.post_content(serverurl + 'relative_redirect'))
beaf135 * forgot to add new files in the previous commit.
nahi authored
525 assert(called)
526 end
527
fad6e92 * post an IO body on the second request as well as the first request…
nahi authored
528 def test_post_content_io
529 post_body = StringIO.new("1234567890")
d7a8fe2 @nahi Use free port number for test server
authored
530 assert_equal('post,1234567890', @client.post_content(serverurl + 'servlet', post_body))
fad6e92 * post an IO body on the second request as well as the first request…
nahi authored
531 post_body = StringIO.new("1234567890")
d7a8fe2 @nahi Use free port number for test server
authored
532 assert_equal('post,1234567890', @client.post_content(serverurl + 'servlet_redirect', post_body))
fad6e92 * post an IO body on the second request as well as the first request…
nahi authored
533 #
534 post_body = StringIO.new("1234567890")
535 post_body.read(5)
d7a8fe2 @nahi Use free port number for test server
authored
536 assert_equal('post,67890', @client.post_content(serverurl + 'servlet_redirect', post_body))
fad6e92 * post an IO body on the second request as well as the first request…
nahi authored
537 end
538
beaf135 * forgot to add new files in the previous commit.
nahi authored
539 def test_head
d7a8fe2 @nahi Use free port number for test server
authored
540 assert_equal("head", @client.head(serverurl + 'servlet').header["x-head"][0])
541 res = @client.head(serverurl + 'servlet', {1=>2, 3=>4})
beaf135 * forgot to add new files in the previous commit.
nahi authored
542 assert_equal('1=2&3=4', res.header["x-query"][0])
543 end
544
4fc7a2b * added tests for coverage. closes #189.
nahi authored
545 def test_head_async
d7a8fe2 @nahi Use free port number for test server
authored
546 conn = @client.head_async(serverurl + 'servlet', {1=>2, 3=>4})
4fc7a2b * added tests for coverage. closes #189.
nahi authored
547 Thread.pass while !conn.finished?
548 res = conn.pop
549 assert_equal('1=2&3=4', res.header["x-query"][0])
550 end
551
beaf135 * forgot to add new files in the previous commit.
nahi authored
552 def test_get
d7a8fe2 @nahi Use free port number for test server
authored
553 assert_equal("get", @client.get(serverurl + 'servlet').content)
554 res = @client.get(serverurl + 'servlet', {1=>2, 3=>4})
beaf135 * forgot to add new files in the previous commit.
nahi authored
555 assert_equal('1=2&3=4', res.header["x-query"][0])
2253f95 * test coverage checked. see #189.
nahi authored
556 assert_nil(res.contenttype)
557 #
d7a8fe2 @nahi Use free port number for test server
authored
558 url = serverurl.to_s + 'servlet?5=6&7=8'
2253f95 * test coverage checked. see #189.
nahi authored
559 res = @client.get(url, {1=>2, 3=>4})
560 assert_equal('1=2&3=4&5=6&7=8', res.header["x-query"][0])
561 assert_nil(res.contenttype)
beaf135 * forgot to add new files in the previous commit.
nahi authored
562 end
563
0623a18 @nahi Add keyword argument :follow_redirect: bool to get/post
authored
564 def test_get_follow_redirect
d7a8fe2 @nahi Use free port number for test server
authored
565 assert_equal('hello', @client.get(serverurl + 'hello', :follow_redirect => true).body)
566 assert_equal('hello', @client.get(serverurl + 'redirect1', :follow_redirect => true).body)
567 assert_equal('hello', @client.get(serverurl + 'redirect2', :follow_redirect => true).body)
0623a18 @nahi Add keyword argument :follow_redirect: bool to get/post
authored
568 end
569
4fc7a2b * added tests for coverage. closes #189.
nahi authored
570 def test_get_async
d7a8fe2 @nahi Use free port number for test server
authored
571 conn = @client.get_async(serverurl + 'servlet', {1=>2, 3=>4})
4fc7a2b * added tests for coverage. closes #189.
nahi authored
572 Thread.pass while !conn.finished?
573 res = conn.pop
574 assert_equal('1=2&3=4', res.header["x-query"][0])
575 end
576
0b7e801 @nahi avoid data corruption bug in async method.
authored
577 def test_get_async_for_largebody
d7a8fe2 @nahi Use free port number for test server
authored
578 conn = @client.get_async(serverurl + 'largebody')
0b7e801 @nahi avoid data corruption bug in async method.
authored
579 res = conn.pop
580 assert_equal(1000*1000, res.content.read.length)
581 end
582
47ba7a6 * [212] broke get/post with block. fixed. closes #173.
nahi authored
583 def test_get_with_block
584 called = false
d7a8fe2 @nahi Use free port number for test server
authored
585 res = @client.get(serverurl + 'servlet') { |str|
47ba7a6 * [212] broke get/post with block. fixed. closes #173.
nahi authored
586 assert_equal('get', str)
587 called = true
588 }
589 assert(called)
590 # res does not have a content
591 assert_nil(res.content)
592 end
593
beaf135 * forgot to add new files in the previous commit.
nahi authored
594 def test_post
d7a8fe2 @nahi Use free port number for test server
authored
595 assert_equal("post", @client.post(serverurl + 'servlet').content[0, 4])
596 res = @client.post(serverurl + 'servlet', {1=>2, 3=>4})
beaf135 * forgot to add new files in the previous commit.
nahi authored
597 assert_equal('1=2&3=4', res.header["x-query"][0])
598 end
599
0623a18 @nahi Add keyword argument :follow_redirect: bool to get/post
authored
600 def test_post_follow_redirect
d7a8fe2 @nahi Use free port number for test server
authored
601 assert_equal('hello', @client.post(serverurl + 'hello', :follow_redirect => true).body)
602 assert_equal('hello', @client.post(serverurl + 'redirect1', :follow_redirect => true).body)
603 assert_equal('hello', @client.post(serverurl + 'redirect2', :follow_redirect => true).body)
0623a18 @nahi Add keyword argument :follow_redirect: bool to get/post
authored
604 end
605
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
606 def test_post_with_content_type
607 ext = {'content-type' => 'application/x-www-form-urlencoded', 'hello' => 'world'}
d7a8fe2 @nahi Use free port number for test server
authored
608 assert_equal("post", @client.post(serverurl + 'servlet').content[0, 4], ext)
609 res = @client.post(serverurl + 'servlet', {1=>2, 3=>4}, ext)
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
610 assert_equal('1=2&3=4', res.header["x-query"][0])
611 #
612 ext = [['content-type', 'multipart/form-data'], ['hello', 'world']]
d7a8fe2 @nahi Use free port number for test server
authored
613 assert_equal("post", @client.post(serverurl + 'servlet').content[0, 4], ext)
614 res = @client.post(serverurl + 'servlet', {1=>2, 3=>4}, ext)
2253f95 * test coverage checked. see #189.
nahi authored
615 assert_match(/Content-Disposition: form-data; name="1"/, res.content)
616 assert_match(/Content-Disposition: form-data; name="3"/, res.content)
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
617 #
618 ext = {'content-type' => 'multipart/form-data; boundary=hello'}
d7a8fe2 @nahi Use free port number for test server
authored
619 assert_equal("post", @client.post(serverurl + 'servlet').content[0, 4], ext)
620 res = @client.post(serverurl + 'servlet', {1=>2, 3=>4}, ext)
2253f95 * test coverage checked. see #189.
nahi authored
621 assert_match(/Content-Disposition: form-data; name="1"/, res.content)
622 assert_match(/Content-Disposition: form-data; name="3"/, res.content)
623 assert_equal("post,--hello\r\nContent-Disposition: form-data; name=\"1\"\r\n\r\n2\r\n--hello\r\nContent-Disposition: form-data; name=\"3\"\r\n\r\n4\r\n--hello--\r\n\r\n", res.content)
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
624 end
625
626 def test_post_with_file
627 STDOUT.sync = true
628 File.open(__FILE__) do |file|
d7a8fe2 @nahi Use free port number for test server
authored
629 res = @client.post(serverurl + 'servlet', {1=>2, 3=>file})
f4f6d9a @nahi test update for 1.9 support. (not yet completed)
authored
630 assert_match(/^Content-Disposition: form-data; name="1"\r\n/nm, res.content)
a176e7b * check test coverage on lib/httpclient/session.rb. see #189.
nahi authored
631 assert_match(/^Content-Disposition: form-data; name="3";/, res.content)
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
632 assert_match(/FIND_TAG_IN_THIS_FILE/, res.content)
633 end
634 end
635
3df5871 @nahi Add test for streaming + non-chunked post
authored
636 def test_post_with_io # streaming, but not chunked
637 myio = StringIO.new("X" * (HTTP::Message::Body::DEFAULT_CHUNK_SIZE + 1))
638 def myio.read(*args)
639 @called ||= 0
640 @called += 1
641 super
642 end
643 def myio.called
644 @called
645 end
646 @client.debug_dev = str = StringIO.new
647 res = @client.post(serverurl + 'servlet', {1=>2, 3=>myio})
648 assert_match(/\r\nContent-Disposition: form-data; name="1"\r\n/m, res.content)
649 assert_match(/\r\n2\r\n/m, res.content)
650 assert_match(/\r\nContent-Disposition: form-data; name="3"; filename=""\r\n/m, res.content)
651 assert_match(/\r\nContent-Length:/m, str.string)
652 assert_equal(3, myio.called)
653 end
654
655 def test_post_with_io_nosize # streaming + chunked post
2253f95 * test coverage checked. see #189.
nahi authored
656 myio = StringIO.new("4")
657 def myio.size
658 nil
659 end
3df5871 @nahi Add test for streaming + non-chunked post
authored
660 @client.debug_dev = str = StringIO.new
d7a8fe2 @nahi Use free port number for test server
authored
661 res = @client.post(serverurl + 'servlet', {1=>2, 3=>myio})
2253f95 * test coverage checked. see #189.
nahi authored
662 assert_match(/\r\nContent-Disposition: form-data; name="1"\r\n/m, res.content)
663 assert_match(/\r\n2\r\n/m, res.content)
664 assert_match(/\r\nContent-Disposition: form-data; name="3"; filename=""\r\n/m, res.content)
665 assert_match(/\r\n4\r\n/m, res.content)
3df5871 @nahi Add test for streaming + non-chunked post
authored
666 assert_match(/\r\nTransfer-Encoding: chunked\r\n/m, str.string)
2253f95 * test coverage checked. see #189.
nahi authored
667 end
668
4fc7a2b * added tests for coverage. closes #189.
nahi authored
669 def test_post_async
d7a8fe2 @nahi Use free port number for test server
authored
670 conn = @client.post_async(serverurl + 'servlet', {1=>2, 3=>4})
4fc7a2b * added tests for coverage. closes #189.
nahi authored
671 Thread.pass while !conn.finished?
672 res = conn.pop
673 assert_equal('1=2&3=4', res.header["x-query"][0])
674 end
675
47ba7a6 * [212] broke get/post with block. fixed. closes #173.
nahi authored
676 def test_post_with_block
677 called = false
d7a8fe2 @nahi Use free port number for test server
authored
678 res = @client.post(serverurl + 'servlet') { |str|
47ba7a6 * [212] broke get/post with block. fixed. closes #173.
nahi authored
679 assert_equal('post,', str)
680 called = true
681 }
682 assert(called)
683 assert_nil(res.content)
684 #
685 called = false
d7a8fe2 @nahi Use free port number for test server
authored
686 res = @client.post(serverurl + 'servlet', {1=>2, 3=>4}) { |str|
47ba7a6 * [212] broke get/post with block. fixed. closes #173.
nahi authored
687 assert_equal('post,1=2&3=4', str)
688 called = true
689 }
690 assert(called)
691 assert_equal('1=2&3=4', res.header["x-query"][0])
692 assert_nil(res.content)
693 end
694
6a381cd @thodg Added tests for custom multipart
thodg authored
695 def test_post_with_custom_multipart
696 ext = {'content-type' => 'multipart/form-data'}
d7a8fe2 @nahi Use free port number for test server
authored
697 assert_equal("post", @client.post(serverurl + 'servlet').content[0, 4], ext)
6a381cd @thodg Added tests for custom multipart
thodg authored
698 body = [{ 'Content-Disposition' => 'form-data; name="1"', :content => "2"},
699 { 'Content-Disposition' => 'form-data; name="3"', :content => "4"}]
d7a8fe2 @nahi Use free port number for test server
authored
700 res = @client.post(serverurl + 'servlet', body, ext)
6a381cd @thodg Added tests for custom multipart
thodg authored
701 assert_match(/Content-Disposition: form-data; name="1"/, res.content)
702 assert_match(/Content-Disposition: form-data; name="3"/, res.content)
703 #
704 ext = {'content-type' => 'multipart/form-data; boundary=hello'}
d7a8fe2 @nahi Use free port number for test server
authored
705 assert_equal("post", @client.post(serverurl + 'servlet').content[0, 4], ext)
706 res = @client.post(serverurl + 'servlet', body, ext)
6a381cd @thodg Added tests for custom multipart
thodg authored
707 assert_match(/Content-Disposition: form-data; name="1"/, res.content)
708 assert_match(/Content-Disposition: form-data; name="3"/, res.content)
709 assert_equal("post,--hello\r\nContent-Disposition: form-data; name=\"1\"\r\n\r\n2\r\n--hello\r\nContent-Disposition: form-data; name=\"3\"\r\n\r\n4\r\n--hello--\r\n\r\n", res.content)
710 end
711
712 def test_post_with_custom_multipart_and_file
713 STDOUT.sync = true
714 File.open(__FILE__) do |file|
715 ext = { 'Content-Type' => 'multipart/alternative' }
716 body = [{ 'Content-Type' => 'text/plain', :content => "this is only a test" },
717 { 'Content-Type' => 'application/x-ruby', :content => file }]
d7a8fe2 @nahi Use free port number for test server
authored
718 res = @client.post(serverurl + 'servlet', body, ext)
6a381cd @thodg Added tests for custom multipart
thodg authored
719 assert_match(/^Content-Type: text\/plain\r\n/m, res.content)
720 assert_match(/^this is only a test\r\n/m, res.content)
721 assert_match(/^Content-Type: application\/x-ruby\r\n/m, res.content)
722 assert_match(/FIND_TAG_IN_THIS_FILE/, res.content)
723 end
724 end
725
beaf135 * forgot to add new files in the previous commit.
nahi authored
726 def test_put
d7a8fe2 @nahi Use free port number for test server
authored
727 assert_equal("put", @client.put(serverurl + 'servlet').content)
728 res = @client.put(serverurl + 'servlet', {1=>2, 3=>4})
4fc7a2b * added tests for coverage. closes #189.
nahi authored
729 assert_equal('1=2&3=4', res.header["x-query"][0])
730 end
731
58bde3a @nahi Do not override given Host header. closes #19.
authored
732 def test_put_bytesize
d7a8fe2 @nahi Use free port number for test server
authored
733 res = @client.put(serverurl + 'servlet', 'txt' => 'あいうえお')
58bde3a @nahi Do not override given Host header. closes #19.
authored
734 assert_equal('txt=%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A', res.header["x-query"][0])
735 assert_equal('15', res.header["x-size"][0])
736 end
737
4fc7a2b * added tests for coverage. closes #189.
nahi authored
738 def test_put_async
d7a8fe2 @nahi Use free port number for test server
authored
739 conn = @client.put_async(serverurl + 'servlet', {1=>2, 3=>4})
4fc7a2b * added tests for coverage. closes #189.
nahi authored
740 Thread.pass while !conn.finished?
741 res = conn.pop
beaf135 * forgot to add new files in the previous commit.
nahi authored
742 assert_equal('1=2&3=4', res.header["x-query"][0])
743 end
744
745 def test_delete
d7a8fe2 @nahi Use free port number for test server
authored
746 assert_equal("delete", @client.delete(serverurl + 'servlet').content)
4fc7a2b * added tests for coverage. closes #189.
nahi authored
747 end
748
749 def test_delete_async
d7a8fe2 @nahi Use free port number for test server
authored
750 conn = @client.delete_async(serverurl + 'servlet')
4fc7a2b * added tests for coverage. closes #189.
nahi authored
751 Thread.pass while !conn.finished?
752 res = conn.pop
753 assert_equal('delete', res.content.read)
beaf135 * forgot to add new files in the previous commit.
nahi authored
754 end
755
756 def test_options
d7a8fe2 @nahi Use free port number for test server
authored
757 assert_equal("options", @client.options(serverurl + 'servlet').content)
4fc7a2b * added tests for coverage. closes #189.
nahi authored
758 end
759
760 def test_options_async
d7a8fe2 @nahi Use free port number for test server
authored
761 conn = @client.options_async(serverurl + 'servlet')
4fc7a2b * added tests for coverage. closes #189.
nahi authored
762 Thread.pass while !conn.finished?
763 res = conn.pop
764 assert_equal('options', res.content.read)
765 end
766
767 def test_propfind
d7a8fe2 @nahi Use free port number for test server
authored
768 assert_equal("propfind", @client.propfind(serverurl + 'servlet').content)
4fc7a2b * added tests for coverage. closes #189.
nahi authored
769 end
770
771 def test_propfind_async
d7a8fe2 @nahi Use free port number for test server
authored
772 conn = @client.propfind_async(serverurl + 'servlet')
4fc7a2b * added tests for coverage. closes #189.
nahi authored
773 Thread.pass while !conn.finished?
774 res = conn.pop
775 assert_equal('propfind', res.content.read)
776 end
777
778 def test_proppatch
d7a8fe2 @nahi Use free port number for test server
authored
779 assert_equal("proppatch", @client.proppatch(serverurl + 'servlet').content)
780 res = @client.proppatch(serverurl + 'servlet', {1=>2, 3=>4})
4fc7a2b * added tests for coverage. closes #189.
nahi authored
781 assert_equal('proppatch', res.content)
782 assert_equal('1=2&3=4', res.header["x-query"][0])
783 end
784
785 def test_proppatch_async
d7a8fe2 @nahi Use free port number for test server
authored
786 conn = @client.proppatch_async(serverurl + 'servlet', {1=>2, 3=>4})
4fc7a2b * added tests for coverage. closes #189.
nahi authored
787 Thread.pass while !conn.finished?
788 res = conn.pop
789 assert_equal('proppatch', res.content.read)
beaf135 * forgot to add new files in the previous commit.
nahi authored
790 assert_equal('1=2&3=4', res.header["x-query"][0])
791 end
792
793 def test_trace
d7a8fe2 @nahi Use free port number for test server
authored
794 assert_equal("trace", @client.trace(serverurl + 'servlet').content)
795 res = @client.trace(serverurl + 'servlet', {1=>2, 3=>4})
beaf135 * forgot to add new files in the previous commit.
nahi authored
796 assert_equal('1=2&3=4', res.header["x-query"][0])
797 end
798
4fc7a2b * added tests for coverage. closes #189.
nahi authored
799 def test_trace_async
d7a8fe2 @nahi Use free port number for test server
authored
800 conn = @client.trace_async(serverurl + 'servlet', {1=>2, 3=>4})
4fc7a2b * added tests for coverage. closes #189.
nahi authored
801 Thread.pass while !conn.finished?
802 res = conn.pop
803 assert_equal('1=2&3=4', res.header["x-query"][0])
804 end
805
806 def test_chunked
d7a8fe2 @nahi Use free port number for test server
authored
807 assert_equal('chunked', @client.get_content(serverurl + 'chunked', { 'msg' => 'chunked' }))
4fc7a2b * added tests for coverage. closes #189.
nahi authored
808 end
809
810 def test_chunked_empty
d7a8fe2 @nahi Use free port number for test server
authored
811 assert_equal('', @client.get_content(serverurl + 'chunked', { 'msg' => '' }))
4fc7a2b * added tests for coverage. closes #189.
nahi authored
812 end
813
beaf135 * forgot to add new files in the previous commit.
nahi authored
814 def test_get_query
815 assert_equal({'1'=>'2'}, check_query_get({1=>2}))
816 assert_equal({'a'=>'A', 'B'=>'b'}, check_query_get({"a"=>"A", "B"=>"b"}))
817 assert_equal({'&'=>'&'}, check_query_get({"&"=>"&"}))
818 assert_equal({'= '=>' =+'}, check_query_get({"= "=>" =+"}))
819 assert_equal(
820 ['=', '&'].sort,
821 check_query_get([["=", "="], ["=", "&"]])['='].to_ary.sort
822 )
823 assert_equal({'123'=>'45'}, check_query_get('123=45'))
824 assert_equal({'12 3'=>'45', ' '=>' '}, check_query_get('12+3=45&+=+'))
825 assert_equal({}, check_query_get(''))
2253f95 * test coverage checked. see #189.
nahi authored
826 assert_equal({'1'=>'2'}, check_query_get({1=>StringIO.new('2')}))
827 assert_equal({'1'=>'2', '3'=>'4'}, check_query_get(StringIO.new('3=4&1=2')))
beaf135 * forgot to add new files in the previous commit.
nahi authored
828 end
829
830 def test_post_body
831 assert_equal({'1'=>'2'}, check_query_post({1=>2}))
832 assert_equal({'a'=>'A', 'B'=>'b'}, check_query_post({"a"=>"A", "B"=>"b"}))
833 assert_equal({'&'=>'&'}, check_query_post({"&"=>"&"}))
834 assert_equal({'= '=>' =+'}, check_query_post({"= "=>" =+"}))
835 assert_equal(
836 ['=', '&'].sort,
837 check_query_post([["=", "="], ["=", "&"]])['='].to_ary.sort
838 )
839 assert_equal({'123'=>'45'}, check_query_post('123=45'))
840 assert_equal({'12 3'=>'45', ' '=>' '}, check_query_post('12+3=45&+=+'))
841 assert_equal({}, check_query_post(''))
842 #
843 post_body = StringIO.new("foo=bar&foo=baz")
844 assert_equal(
845 ["bar", "baz"],
846 check_query_post(post_body)["foo"].to_ary.sort
847 )
848 end
849
850 def test_extra_headers
851 str = ""
852 @client.debug_dev = str
d7a8fe2 @nahi Use free port number for test server
authored
853 @client.head(serverurl, nil, {"ABC" => "DEF"})
beaf135 * forgot to add new files in the previous commit.
nahi authored
854 lines = str.split(/(?:\r?\n)+/)
855 assert_equal("= Request", lines[0])
856 assert_match("ABC: DEF", lines[4])
857 #
858 str = ""
859 @client.debug_dev = str
d7a8fe2 @nahi Use free port number for test server
authored
860 @client.get(serverurl, nil, [["ABC", "DEF"], ["ABC", "DEF"]])
beaf135 * forgot to add new files in the previous commit.
nahi authored
861 lines = str.split(/(?:\r?\n)+/)
862 assert_equal("= Request", lines[0])
863 assert_match("ABC: DEF", lines[4])
864 assert_match("ABC: DEF", lines[5])
865 end
866
867 def test_timeout
868 assert_equal(60, @client.connect_timeout)
869 assert_equal(120, @client.send_timeout)
870 assert_equal(60, @client.receive_timeout)
4fc7a2b * added tests for coverage. closes #189.
nahi authored
871 #
872 @client.connect_timeout = 1
873 @client.send_timeout = 2
874 @client.receive_timeout = 3
875 assert_equal(1, @client.connect_timeout)
876 assert_equal(2, @client.send_timeout)
877 assert_equal(3, @client.receive_timeout)
beaf135 * forgot to add new files in the previous commit.
nahi authored
878 end
879
880 def test_connect_timeout
881 # ToDo
882 end
883
884 def test_send_timeout
885 # ToDo
886 end
887
888 def test_receive_timeout
889 # this test takes 2 sec
d7a8fe2 @nahi Use free port number for test server
authored
890 assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=2'))
beaf135 * forgot to add new files in the previous commit.
nahi authored
891 @client.receive_timeout = 1
d7a8fe2 @nahi Use free port number for test server
authored
892 assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=0'))
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
893 assert_raise(HTTPClient::ReceiveTimeoutError) do
d7a8fe2 @nahi Use free port number for test server
authored
894 @client.get_content(serverurl + 'sleep?sec=2')
beaf135 * forgot to add new files in the previous commit.
nahi authored
895 end
896 @client.receive_timeout = 3
d7a8fe2 @nahi Use free port number for test server
authored
897 assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=2'))
beaf135 * forgot to add new files in the previous commit.
nahi authored
898 end
899
59dbc78 @nahi just added POST receive_timeout test for #2.
authored
900 def test_receive_timeout_post
901 # this test takes 2 sec
d7a8fe2 @nahi Use free port number for test server
authored
902 assert_equal('hello', @client.post(serverurl + 'sleep', :sec => 2).content)
59dbc78 @nahi just added POST receive_timeout test for #2.
authored
903 @client.receive_timeout = 1
d7a8fe2 @nahi Use free port number for test server
authored
904 assert_equal('hello', @client.post(serverurl + 'sleep', :sec => 0).content)
59dbc78 @nahi just added POST receive_timeout test for #2.
authored
905 assert_raise(HTTPClient::ReceiveTimeoutError) do
d7a8fe2 @nahi Use free port number for test server
authored
906 @client.post(serverurl + 'sleep', :sec => 2)
59dbc78 @nahi just added POST receive_timeout test for #2.
authored
907 end
908 @client.receive_timeout = 3
d7a8fe2 @nahi Use free port number for test server
authored
909 assert_equal('hello', @client.post(serverurl + 'sleep', :sec => 2).content)
59dbc78 @nahi just added POST receive_timeout test for #2.
authored
910 end
911
1986c59 @nahi Applied the patch in #44: let Connection#pop raise the Exception when…
authored
912 def test_async_error
913 assert_raise( SocketError ) do
914 conn = @client.get_async("http://non-existing-host/")
915 conn.pop
916 end
917 end
918
4fc7a2b * added tests for coverage. closes #189.
nahi authored
919 def test_reset
d7a8fe2 @nahi Use free port number for test server
authored
920 url = serverurl + 'servlet'
4fc7a2b * added tests for coverage. closes #189.
nahi authored
921 assert_nothing_raised do
922 5.times do
923 @client.get(url)
924 @client.reset(url)
925 end
926 end
927 end
928
929 def test_reset_all
930 assert_nothing_raised do
931 5.times do
d7a8fe2 @nahi Use free port number for test server
authored
932 @client.get(serverurl + 'servlet')
4fc7a2b * added tests for coverage. closes #189.
nahi authored
933 @client.reset_all
934 end
935 end
936 end
937
beaf135 * forgot to add new files in the previous commit.
nahi authored
938 def test_cookies
4fc7a2b * added tests for coverage. closes #189.
nahi authored
939 cookiefile = File.join(File.dirname(File.expand_path(__FILE__)), 'test_cookies_file')
beaf135 * forgot to add new files in the previous commit.
nahi authored
940 File.open(cookiefile, "wb") do |f|
4fc7a2b * added tests for coverage. closes #189.
nahi authored
941 f << "http://rubyforge.org/account/login.php session_ser LjEwMy45Ni40Ni0q%2A-fa0537de8cc31 2000000000 .rubyforge.org / 13\n"
beaf135 * forgot to add new files in the previous commit.
nahi authored
942 end
4fc7a2b * added tests for coverage. closes #189.
nahi authored
943 @client.set_cookie_store(cookiefile)
944 cookie = @client.cookie_manager.cookies.first
beaf135 * forgot to add new files in the previous commit.
nahi authored
945 url = cookie.url
946 assert(cookie.domain_match(url.host, cookie.domain))
4fc7a2b * added tests for coverage. closes #189.
nahi authored
947 #
948 @client.reset_all
c156a0f @nahi Make test TZ-free
authored
949 @client.test_loopback_http_response << "HTTP/1.0 200 OK\nSet-Cookie: foo=bar; expires=#{Time.at(1924873200).gmtime.httpdate}\n\nOK"
4fc7a2b * added tests for coverage. closes #189.
nahi authored
950 @client.get_content('http://rubyforge.org/account/login.php')
951 @client.save_cookie_store
952 str = File.read(cookiefile)
0d46b59 @nahi illegal Cookie PATH handling. closes #13.
authored
953 assert_match(%r(http://rubyforge.org/account/login.php foo bar 1924873200 rubyforge.org /account 1), str)
207be5c @nahi Test fixture cleanup after running
authored
954 File.unlink(cookiefile)
4fc7a2b * added tests for coverage. closes #189.
nahi authored
955 end
956
350acc3 * applied a patch from user. IO#readpartial does not clear the secon…
nahi authored
957 def test_eof_error_length
958 io = StringIO.new('')
959 def io.gets(*arg)
960 @buf ||= ["HTTP/1.0 200 OK\n", "content-length: 123\n", "\n"]
961 @buf.shift
962 end
963 def io.readpartial(size, buf)
964 @second ||= false
965 if !@second
966 @second = '1st'
967 buf << "abc"
968 buf
969 elsif @second == '1st'
970 @second = '2nd'
971 raise EOFError.new
972 else
973 raise Exception.new
974 end
975 end
976 def io.eof?
977 true
978 end
979 @client.test_loopback_http_response << io
980 assert_nothing_raised do
981 @client.get('http://foo/bar')
982 end
983 end
984
985 def test_eof_error_rest
986 io = StringIO.new('')
987 def io.gets(*arg)
988 @buf ||= ["HTTP/1.0 200 OK\n", "\n"]
989 @buf.shift
990 end
991 def io.readpartial(size, buf)
992 @second ||= false
993 if !@second
994 @second = '1st'
995 buf << "abc"
996 buf
997 elsif @second == '1st'
998 @second = '2nd'
999 raise EOFError.new
1000 else
1001 raise Exception.new
1002 end
1003 end
1004 def io.eof?
1005 true
1006 end
1007 @client.test_loopback_http_response << io
1008 assert_nothing_raised do
1009 @client.get('http://foo/bar')
1010 end
1011 end
1012
4fc7a2b * added tests for coverage. closes #189.
nahi authored
1013 def test_urify
1014 extend HTTPClient::Util
1015 assert_nil(urify(nil))
1016 uri = 'http://foo'
1017 assert_equal(URI.parse(uri), urify(uri))
1018 assert_equal(URI.parse(uri), urify(URI.parse(uri)))
1019 end
1020
1021 def test_connection
1022 c = HTTPClient::Connection.new
1023 assert(c.finished?)
1024 assert_nil(c.join)
1025 end
1026
1027 def test_site
1028 site = HTTPClient::Site.new
1029 assert_equal('tcp', site.scheme)
1030 assert_equal('0.0.0.0', site.host)
1031 assert_equal(0, site.port)
1032 assert_equal('tcp://0.0.0.0:0', site.addr)
1033 assert_equal('tcp://0.0.0.0:0', site.to_s)
1034 assert_nothing_raised do
1035 site.inspect
1036 end
1037 #
1038 site = HTTPClient::Site.new(URI.parse('http://localhost:12345/foo'))
1039 assert_equal('http', site.scheme)
1040 assert_equal('localhost', site.host)
1041 assert_equal(12345, site.port)
1042 assert_equal('http://localhost:12345', site.addr)
1043 assert_equal('http://localhost:12345', site.to_s)
1044 assert_nothing_raised do
1045 site.inspect
1046 end
1047 #
1048 site1 = HTTPClient::Site.new(URI.parse('http://localhost:12341/'))
1049 site2 = HTTPClient::Site.new(URI.parse('http://localhost:12342/'))
1050 site3 = HTTPClient::Site.new(URI.parse('http://localhost:12342/'))
1051 assert(!(site1 == site2))
1052 h = { site1 => 'site1', site2 => 'site2' }
1053 h[site3] = 'site3'
1054 assert_equal('site1', h[site1])
1055 assert_equal('site3', h[site2])
beaf135 * forgot to add new files in the previous commit.
nahi authored
1056 end
1057
2253f95 * test coverage checked. see #189.
nahi authored
1058 def test_http_header
d7a8fe2 @nahi Use free port number for test server
authored
1059 res = @client.get(serverurl + 'hello')
2253f95 * test coverage checked. see #189.
nahi authored
1060 assert_equal('text/html', res.contenttype)
1061 assert_equal(5, res.header.get(nil).size)
1062 #
1063 res.header.delete('connection')
1064 assert_equal(4, res.header.get(nil).size)
1065 #
1066 res.header['foo'] = 'bar'
1067 assert_equal(['bar'], res.header['foo'])
1068 #
1069 assert_equal([['foo', 'bar']], res.header.get('foo'))
83dc6ec * avoid duplicated request header line while HTTP access negotiation…
nahi authored
1070 res.header['foo'] = ['bar', 'bar2']
2253f95 * test coverage checked. see #189.
nahi authored
1071 assert_equal([['foo', 'bar'], ['foo', 'bar2']], res.header.get('foo'))
1072 end
1073
1074 def test_mime_type
1075 assert_equal('text/plain', HTTP::Message.mime_type('foo.txt'))
1076 assert_equal('text/html', HTTP::Message.mime_type('foo.html'))
1077 assert_equal('text/html', HTTP::Message.mime_type('foo.htm'))
1078 assert_equal('application/msword', HTTP::Message.mime_type('foo.doc'))
1079 assert_equal('image/png', HTTP::Message.mime_type('foo.png'))
1080 assert_equal('image/gif', HTTP::Message.mime_type('foo.gif'))
1081 assert_equal('image/jpeg', HTTP::Message.mime_type('foo.jpg'))
1082 assert_equal('image/jpeg', HTTP::Message.mime_type('foo.jpeg'))
1083 assert_equal('application/octet-stream', HTTP::Message.mime_type('foo.unknown'))
1084 #
7008822 * test cleanups for 1.9
nahi authored
1085 handler = lambda { |path| 'hello/world' }
1086 assert_nil(HTTP::Message.mime_type_handler)
2253f95 * test coverage checked. see #189.
nahi authored
1087 assert_nil(HTTP::Message.get_mime_type_func)
7008822 * test cleanups for 1.9
nahi authored
1088 HTTP::Message.mime_type_handler = handler
1089 assert_not_nil(HTTP::Message.mime_type_handler)
2253f95 * test coverage checked. see #189.
nahi authored
1090 assert_not_nil(HTTP::Message.get_mime_type_func)
1091 assert_equal('hello/world', HTTP::Message.mime_type('foo.txt'))
7008822 * test cleanups for 1.9
nahi authored
1092 HTTP::Message.mime_type_handler = nil
1093 assert_equal('text/plain', HTTP::Message.mime_type('foo.txt'))
2253f95 * test coverage checked. see #189.
nahi authored
1094 HTTP::Message.set_mime_type_func(nil)
1095 assert_equal('text/plain', HTTP::Message.mime_type('foo.txt'))
7008822 * test cleanups for 1.9
nahi authored
1096 #
1097 handler = lambda { |path| nil }
1098 HTTP::Message.mime_type_handler = handler
1099 assert_equal('application/octet-stream', HTTP::Message.mime_type('foo.txt'))
2253f95 * test coverage checked. see #189.
nahi authored
1100 end
1101
1102 def test_connect_request
7008822 * test cleanups for 1.9
nahi authored
1103 req = HTTP::Message.new_connect_request(URI.parse('https://foo/bar'))
2253f95 * test coverage checked. see #189.
nahi authored
1104 assert_equal("CONNECT foo:443 HTTP/1.0\r\n\r\n", req.dump)
7008822 * test cleanups for 1.9
nahi authored
1105 req = HTTP::Message.new_connect_request(URI.parse('https://example.com/'))
1106 assert_equal("CONNECT example.com:443 HTTP/1.0\r\n\r\n", req.dump)
2253f95 * test coverage checked. see #189.
nahi authored
1107 end
1108
1109 def test_response
1110 res = HTTP::Message.new_response('response')
1111 res.contenttype = 'text/plain'
c156a0f @nahi Make test TZ-free
authored
1112 res.header.body_date = Time.at(946652400)
2253f95 * test coverage checked. see #189.
nahi authored
1113 assert_equal(
1114 [
1115 "",
1116 "Content-Length: 8",
1117 "Content-Type: text/plain",
1118 "Last-Modified: Fri, 31 Dec 1999 15:00:00 GMT",
1119 "Status: 200 OK",
1120 "response"
1121 ],
1122 res.dump.split(/\r\n/).sort
1123 )
218a79b @nahi Add res.headers method which returns a Hash of headers.
authored
1124 assert_equal(['8'], res.header['Content-Length'])
1125 assert_equal('8', res.headers['Content-Length'])
2253f95 * test coverage checked. see #189.
nahi authored
1126 res.header.set('foo', 'bar')
1127 assert_equal(
1128 [
1129 "",
1130 "Content-Length: 8",
1131 "Content-Type: text/plain",
1132 "Last-Modified: Fri, 31 Dec 1999 15:00:00 GMT",
1133 "Status: 200 OK",
1134 "foo: bar",
1135 "response"
1136 ],
1137 res.dump.split(/\r\n/).sort
1138 )
1139 # nil body
1140 res = HTTP::Message.new_response(nil)
1141 assert_equal(
1142 [
1143 "Content-Length: 0",
1144 "Content-Type: text/html; charset=us-ascii",
1145 "Status: 200 OK"
1146 ],
1147 res.dump.split(/\r\n/).sort
1148 )
1149 # for mod_ruby env
1150 Object.const_set('Apache', nil)
1151 begin
1152 res = HTTP::Message.new_response('response')
1153 assert(res.dump.split(/\r\n/).any? { |line| /^Date/ =~ line })
1154 #
1155 res = HTTP::Message.new_response('response')
1156 res.contenttype = 'text/plain'
c156a0f @nahi Make test TZ-free
authored
1157 res.header.body_date = Time.at(946652400)
1158 res.header['Date'] = Time.at(946652400).httpdate
2253f95 * test coverage checked. see #189.
nahi authored
1159 assert_equal(
1160 [
1161 "",
1162 "Content-Length: 8",
1163 "Content-Type: text/plain",
c6d461b * added RDoc for httpclient/connection.rb. see #162.
nahi authored
1164 "Date: Fri, 31 Dec 1999 15:00:00 GMT",
2253f95 * test coverage checked. see #189.
nahi authored
1165 "HTTP/1.1 200 OK",
1166 "Last-Modified: Fri, 31 Dec 1999 15:00:00 GMT",
1167 "response"
1168 ],
1169 res.dump.split(/\r\n/).sort
1170 )
1171 ensure
1172 Object.instance_eval { remove_const('Apache') }
1173 end
1174 end
1175
2431a5f @nahi Add res.cookies method.
authored
1176 def test_response_cookies
1177 res = HTTP::Message.new_response('response')
1178 res.contenttype = 'text/plain'
c156a0f @nahi Make test TZ-free
authored
1179 res.header.body_date = Time.at(946652400)
2431a5f @nahi Add res.cookies method.
authored
1180 assert_nil(res.cookies)
1181 #
1182 res.header['Set-Cookie'] = [
1183 'CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT',
1184 'PART_NUMBER=ROCKET_LAUNCHER_0001; path=/'
1185 ]
1186 assert_equal(
1187 [
1188 "",
1189 "Content-Length: 8",
1190 "Content-Type: text/plain",
1191 "Last-Modified: Fri, 31 Dec 1999 15:00:00 GMT",
1192 "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT",
1193 "Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/",
1194 "Status: 200 OK",
1195 "response"
1196 ],
1197 res.dump.split(/\r\n/).sort
1198 )
1199 assert_equal(2, res.cookies.size)
1200 assert_equal('CUSTOMER', res.cookies[0].name)
1201 assert_equal('PART_NUMBER', res.cookies[1].name)
1202 end
1203
95f2457 @nahi Do not invoke scheduler thread when it won't be used.
authored
1204 if !defined?(JRUBY_VERSION) and RUBY_VERSION < '1.9'
1205 def test_timeout_scheduler
d7a8fe2 @nahi Use free port number for test server
authored
1206 assert_equal('hello', @client.get_content(serverurl + 'hello'))
95f2457 @nahi Do not invoke scheduler thread when it won't be used.
authored
1207 status = HTTPClient.timeout_scheduler.instance_eval { @thread.kill; @thread.join; @thread.status }
1208 assert(!status) # dead
d7a8fe2 @nahi Use free port number for test server
authored
1209 assert_equal('hello', @client.get_content(serverurl + 'hello'))
95f2457 @nahi Do not invoke scheduler thread when it won't be used.
authored
1210 end
b6d2b8b * test added for #197. closes #197.
nahi authored
1211 end
1212
33b4eab * let SessionManager inherit proxy and debug_dev setting from client…
nahi authored
1213 def test_session_manager
1214 mgr = HTTPClient::SessionManager.new(@client)
1215 assert_nil(mgr.instance_eval { @proxy })
1216 assert_nil(mgr.debug_dev)
1217 @client.debug_dev = Object.new
1218 @client.proxy = 'http://myproxy:12345'
1219 mgr = HTTPClient::SessionManager.new(@client)
1220 assert_equal('http://myproxy:12345', mgr.instance_eval { @proxy }.to_s)
1221 assert_equal(@client.debug_dev, mgr.debug_dev)
1222 end
1223
43730ac @nahi Reenable keep-alive for chunked response.
authored
1224 def create_keepalive_disconnected_thread(idx, sock)
9ada537 @nahi Make Keep-Alive test use threads properly.
authored
1225 Thread.new {
1226 # return "12345" for the first connection
1227 sock.gets
1228 sock.gets
1229 sock.write("HTTP/1.1 200 OK\r\n")
1230 sock.write("Content-Length: 5\r\n")
1231 sock.write("\r\n")
1232 sock.write("12345")
1233 # for the next connection, close while reading the request for emulating
1234 # KeepAliveDisconnected
1235 sock.gets
1236 sock.close
1237 }
1238 end
1239
a620b5f @nahi Invalidate pooled sessions for the same destination when we got a Kee…
authored
1240 def test_keepalive_disconnected
1241 client = HTTPClient.new
1242 server = TCPServer.open('127.0.0.1', 0)
1243 server.listen(30) # set enough backlogs
9ada537 @nahi Make Keep-Alive test use threads properly.
authored
1244 endpoint = "http://127.0.0.1:#{server.addr[1]}/"
4ac6fee @nahi Avoid unused variable warning of testcase.
authored
1245 Thread.new {
9ada537 @nahi Make Keep-Alive test use threads properly.
authored
1246 Thread.abort_on_exception = true
1247 # emulate 10 keep-alive connections
1248 10.times do |idx|
1249 sock = server.accept
43730ac @nahi Reenable keep-alive for chunked response.
authored
1250 create_keepalive_disconnected_thread(idx, sock)
9ada537 @nahi Make Keep-Alive test use threads properly.
authored
1251 end
1252 # return "23456" for the request which gets KeepAliveDisconnected
a620b5f @nahi Invalidate pooled sessions for the same destination when we got a Kee…
authored
1253 5.times do
1254 sock = server.accept
1255 sock.gets
1256 sock.gets
1257 sock.write("HTTP/1.1 200 OK\r\n")
1258 sock.write("\r\n")
9ada537 @nahi Make Keep-Alive test use threads properly.
authored
1259 sock.write("23456")
1260 sock.close
a620b5f @nahi Invalidate pooled sessions for the same destination when we got a Kee…
authored
1261 end
9ada537 @nahi Make Keep-Alive test use threads properly.
authored
1262 # return "34567" for the rest requests
a620b5f @nahi Invalidate pooled sessions for the same destination when we got a Kee…
authored
1263 while true
1264 sock = server.accept
9ada537 @nahi Make Keep-Alive test use threads properly.
authored
1265 sock.gets
1266 sock.gets
1267 sock.write("HTTP/1.1 200 OK\r\n")
1268 sock.write("Connection: close\r\n")
1269 sock.write("Content-Length: 5\r\n")
1270 sock.write("\r\n")
1271 sock.write("34567")
a620b5f @nahi Invalidate pooled sessions for the same destination when we got a Kee…
authored
1272 sock.close
1273 end
1274 }
9ada537 @nahi Make Keep-Alive test use threads properly.
authored
1275 # allocate 10 keep-alive connections
1276 10.times.to_enum.map {
1277 Thread.new {
1278 assert_equal("12345", client.get(endpoint).content)
1279 }
1280 }.each(&:join)
1281 # send 5 requests, which should get KeepAliveDesconnected.
1282 # doing these requests, rest keep-alive connections are invalidated.
1283 5.times.to_enum.map {
1284 Thread.new {
1285 assert_equal("23456", client.get(endpoint).content)
1286 }
1287 }.each(&:join)
1288 # rest requests won't get KeepAliveDisconnected; how can I check this?
a620b5f @nahi Invalidate pooled sessions for the same destination when we got a Kee…
authored
1289 10.times.to_enum.map {
1290 Thread.new {
9ada537 @nahi Make Keep-Alive test use threads properly.
authored
1291 assert_equal("34567", client.get(endpoint).content)
a620b5f @nahi Invalidate pooled sessions for the same destination when we got a Kee…
authored
1292 }
9ada537 @nahi Make Keep-Alive test use threads properly.
authored
1293 }.each(&:join)
a620b5f @nahi Invalidate pooled sessions for the same destination when we got a Kee…
authored
1294 end
1295
43730ac @nahi Reenable keep-alive for chunked response.
authored
1296 def create_keepalive_thread(count, sock)
1297 Thread.new {
1298 Thread.abort_on_exception = true
1299 count.times do
1300 req = sock.gets
1301 while line = sock.gets
1302 break if line.chomp.empty?
1303 end
1304 case req
1305 when /chunked/
1306 sock.write("HTTP/1.1 200 OK\r\n")
1307 sock.write("Transfer-Encoding: chunked\r\n")
1308 sock.write("\r\n")
1309 sock.write("1a\r\n")
1310 sock.write("abcdefghijklmnopqrstuvwxyz\r\n")
1311 sock.write("10\r\n")
1312 sock.write("1234567890abcdef\r\n")
1313 sock.write("0\r\n")
1314 sock.write("\r\n")
1315 else
1316 sock.write("HTTP/1.1 200 OK\r\n")
1317 sock.write("Content-Length: 5\r\n")
1318 sock.write("\r\n")
1319 sock.write("12345")
1320 end
1321 end
1322 sock.close
1323 }
1324 end
1325
1326 def test_keepalive
1327 server = TCPServer.open('localhost', 0)
1328 server_thread = Thread.new {
1329 Thread.abort_on_exception = true
1330 sock = server.accept
1331 create_keepalive_thread(10, sock)
1332 }
1333 url = "http://localhost:#{server.addr[1]}/"
1334 begin
1335 # content-length
1336 5.times do
1337 assert_equal('12345', @client.get(url).body)
1338 end
1339 # chunked
1340 5.times do
1341 assert_equal('abcdefghijklmnopqrstuvwxyz1234567890abcdef', @client.get(url + 'chunked').body)
1342 end
1343 ensure
1344 server.close
1345 server_thread.join
1346 end
1347 end
1348
c9b5f3e @nahi Rename 'local_bind' to 'socket_local'
authored
1349 def test_socket_local
1350 @client.socket_local.host = 'localhost'
d7a8fe2 @nahi Use free port number for test server
authored
1351 assert_equal('hello', @client.get_content(serverurl + 'hello'))
c9b5f3e @nahi Rename 'local_bind' to 'socket_local'
authored
1352 @client.reset_all
d7a8fe2 @nahi Use free port number for test server
authored
1353 @client.socket_local.port = serverport
c9b5f3e @nahi Rename 'local_bind' to 'socket_local'
authored
1354 assert_raises(Errno::EADDRINUSE) do
d7a8fe2 @nahi Use free port number for test server
authored
1355 assert_equal('hello', @client.get_content(serverurl + 'hello'))
c9b5f3e @nahi Rename 'local_bind' to 'socket_local'
authored
1356 end
1357 end
1358
beaf135 * forgot to add new files in the previous commit.
nahi authored
1359 private
1360
1361 def check_query_get(query)
1362 WEBrick::HTTPUtils.parse_query(
d7a8fe2 @nahi Use free port number for test server
authored
1363 @client.get(serverurl + 'servlet', query).header["x-query"][0]
beaf135 * forgot to add new files in the previous commit.
nahi authored
1364 )
1365 end
1366
1367 def check_query_post(query)
1368 WEBrick::HTTPUtils.parse_query(
d7a8fe2 @nahi Use free port number for test server
authored
1369 @client.post(serverurl + 'servlet', query).header["x-query"][0]
beaf135 * forgot to add new files in the previous commit.
nahi authored
1370 )
1371 end
1372
1373 def setup_server
1374 @server = WEBrick::HTTPServer.new(
a560489 * use 'localhost' instead of '0.0.0.0'
nahi authored
1375 :BindAddress => "localhost",
beaf135 * forgot to add new files in the previous commit.
nahi authored
1376 :Logger => @logger,
d7a8fe2 @nahi Use free port number for test server
authored
1377 :Port => 0,
beaf135 * forgot to add new files in the previous commit.
nahi authored
1378 :AccessLog => [],
1379 :DocumentRoot => File.dirname(File.expand_path(__FILE__))
1380 )
d7a8fe2 @nahi Use free port number for test server
authored
1381 @serverport = @server.config[:Port]
95964f1 @nahi Added transparent_gzip_decompression property. closes #42.
authored
1382 [:hello, :sleep, :servlet_redirect, :redirect1, :redirect2, :redirect3, :redirect_self, :relative_redirect, :chunked, :largebody, :status, :compressed].each do |sym|
beaf135 * forgot to add new files in the previous commit.
nahi authored
1383 @server.mount(
1384 "/#{sym}",
1385 WEBrick::HTTPServlet::ProcHandler.new(method("do_#{sym}").to_proc)
1386 )
1387 end
1388 @server.mount('/servlet', TestServlet.new(@server))
1389 @server_thread = start_server_thread(@server)
1390 end
1391
b041b24 * added tests for proxy environment settings. see #109.
nahi authored
1392 def escape_env
1393 env = {}
1394 env.update(ENV)
1395 yield
1396 ensure
1397 ENV.clear
1398 ENV.update(env)
1399 end
1400
beaf135 * forgot to add new files in the previous commit.
nahi authored
1401 def escape_noproxy
1402 backup = HTTPClient::NO_PROXY_HOSTS.dup
1403 HTTPClient::NO_PROXY_HOSTS.clear
1404 yield
1405 ensure
1406 HTTPClient::NO_PROXY_HOSTS.replace(backup)
1407 end
1408
1409 def do_hello(req, res)
1410 res['content-type'] = 'text/html'
1411 res.body = "hello"
1412 end
1413
1414 def do_sleep(req, res)
1415 sec = req.query['sec'].to_i
1416 sleep sec
1417 res['content-type'] = 'text/html'
1418 res.body = "hello"
1419 end
1420
47ba7a6 * [212] broke get/post with block. fixed. closes #173.
nahi authored
1421 def do_servlet_redirect(req, res)
d7a8fe2 @nahi Use free port number for test server
authored
1422 res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "servlet")
47ba7a6 * [212] broke get/post with block. fixed. closes #173.
nahi authored
1423 end
1424
beaf135 * forgot to add new files in the previous commit.
nahi authored
1425 def do_redirect1(req, res)
d7a8fe2 @nahi Use free port number for test server
authored
1426 res.set_redirect(WEBrick::HTTPStatus::MovedPermanently, serverurl + "hello")
beaf135 * forgot to add new files in the previous commit.
nahi authored
1427 end
1428
1429 def do_redirect2(req, res)
d7a8fe2 @nahi Use free port number for test server
authored
1430 res.set_redirect(WEBrick::HTTPStatus::TemporaryRedirect, serverurl + "redirect3")
beaf135 * forgot to add new files in the previous commit.
nahi authored
1431 end
1432
1433 def do_redirect3(req, res)
d7a8fe2 @nahi Use free port number for test server
authored
1434 res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "hello")
beaf135 * forgot to add new files in the previous commit.
nahi authored
1435 end
1436
1437 def do_redirect_self(req, res)
d7a8fe2 @nahi Use free port number for test server
authored
1438 res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "redirect_self")
beaf135 * forgot to add new files in the previous commit.
nahi authored
1439 end
1440
1441 def do_relative_redirect(req, res)
1442 res.set_redirect(WEBrick::HTTPStatus::Found, "hello")
1443 end
1444
4fc7a2b * added tests for coverage. closes #189.
nahi authored
1445 def do_chunked(req, res)
1446 res.chunked = true
1447 piper, pipew = IO.pipe
1448 res.body = piper
1449 pipew << req.query['msg']
1450 pipew.close
1451 end
1452
0b7e801 @nahi avoid data corruption bug in async method.
authored
1453 def do_largebody(req, res)
1454 res['content-type'] = 'text/html'
1455 res.body = "a" * 1000 * 1000
1456 end
1457
95964f1 @nahi Added transparent_gzip_decompression property. closes #42.
authored
1458 def do_compressed(req, res)
1459 if req.query['enc'] == 'gzip'
1460 res['content-encoding'] = 'gzip'
1461 res.body = GZIP_CONTENT
1462 elsif req.query['enc'] == 'deflate'
1463 res['content-encoding'] = 'deflate'
1464 res.body = DEFLATE_CONTENT
1465 end
1466 end
1467
d24ab1d @nahi do not try to read when a response code is 1XX, 204 or 304. closes #10.
authored
1468 def do_status(req, res)
1469 res.status = req.query['status'].to_i
1470 end
1471
beaf135 * forgot to add new files in the previous commit.
nahi authored
1472 class TestServlet < WEBrick::HTTPServlet::AbstractServlet
1473 def get_instance(*arg)
1474 self
1475 end
1476
1477 def do_HEAD(req, res)
1478 res["x-head"] = 'head' # use this for test purpose only.
1479 res["x-query"] = query_response(req)
1480 end
1481
1482 def do_GET(req, res)
1483 res.body = 'get'
1484 res["x-query"] = query_response(req)
1485 end
1486
1487 def do_POST(req, res)
47ba7a6 * [212] broke get/post with block. fixed. closes #173.
nahi authored
1488 res.body = 'post,' + req.body.to_s
beaf135 * forgot to add new files in the previous commit.
nahi authored
1489 res["x-query"] = body_response(req)
1490 end
1491
1492 def do_PUT(req, res)
4fc7a2b * added tests for coverage. closes #189.
nahi authored
1493 res["x-query"] = body_response(req)
58bde3a @nahi Do not override given Host header. closes #19.
authored
1494 param = WEBrick::HTTPUtils.parse_query(req.body) || {}
1495 res["x-size"] = (param['txt'] || '').size
261b280 @nahi define String#bytesize and use it for 1.9 compatibility. closes #23.
authored
1496 res.body = param['txt'] || 'put'
beaf135 * forgot to add new files in the previous commit.
nahi authored
1497 end
1498
1499 def do_DELETE(req, res)
1500 res.body = 'delete'
1501 end
1502
1503 def do_OPTIONS(req, res)
1504 # check RFC for legal response.
1505 res.body = 'options'
1506 end
1507
4fc7a2b * added tests for coverage. closes #189.
nahi authored
1508 def do_PROPFIND(req, res)
1509 res.body = 'propfind'
1510 end
1511
1512 def do_PROPPATCH(req, res)
1513 res.body = 'proppatch'
1514 res["x-query"] = body_response(req)
1515 end
1516
beaf135 * forgot to add new files in the previous commit.
nahi authored
1517 def do_TRACE(req, res)
1518 # client SHOULD reflect the message received back to the client as the
1519 # entity-body of a 200 (OK) response. [RFC2616]
1520 res.body = 'trace'
1521 res["x-query"] = query_response(req)
1522 end
1523
1524 private
1525
1526 def query_response(req)
1527 query_escape(WEBrick::HTTPUtils.parse_query(req.query_string))
1528 end
1529
1530 def body_response(req)
1531 query_escape(WEBrick::HTTPUtils.parse_query(req.body))
1532 end
1533
1534 def query_escape(query)
1535 escaped = []
2253f95 * test coverage checked. see #189.
nahi authored
1536 query.sort_by { |k, v| k }.collect do |k, v|
beaf135 * forgot to add new files in the previous commit.
nahi authored
1537 v.to_ary.each do |ve|
1538 escaped << CGI.escape(k) + '=' + CGI.escape(ve)
1539 end
1540 end
1541 escaped.join('&')
1542 end
1543 end
1544 end
Something went wrong with that request. Please try again.