httpclient 2.2.0.2 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/lib/httpclient.rb +5 -5
  2. data/lib/httpclient/cacert.p7s +12 -12
  3. data/lib/httpclient/cacert_sha1.p7s +12 -12
  4. data/lib/httpclient/http.rb +15 -8
  5. data/lib/httpclient/session.rb +6 -1
  6. data/lib/httpclient/ssl_config.rb +18 -14
  7. data/sample/async.rb +8 -0
  8. data/sample/auth.rb +11 -0
  9. data/sample/cookie.rb +18 -0
  10. data/sample/dav.rb +103 -0
  11. data/sample/howto.rb +49 -0
  12. data/sample/oauth_buzz.rb +57 -0
  13. data/sample/oauth_friendfeed.rb +59 -0
  14. data/sample/oauth_twitter.rb +61 -0
  15. data/sample/ssl/0cert.pem +22 -0
  16. data/sample/ssl/0key.pem +30 -0
  17. data/sample/ssl/1000cert.pem +19 -0
  18. data/sample/ssl/1000key.pem +18 -0
  19. data/sample/ssl/htdocs/index.html +10 -0
  20. data/sample/ssl/ssl_client.rb +22 -0
  21. data/sample/ssl/webrick_httpsd.rb +29 -0
  22. data/sample/stream.rb +21 -0
  23. data/sample/thread.rb +27 -0
  24. data/sample/wcat.rb +21 -0
  25. data/test/ca.cert +23 -0
  26. data/test/client.cert +19 -0
  27. data/test/client.key +15 -0
  28. data/test/helper.rb +99 -0
  29. data/test/htdigest +1 -0
  30. data/test/htpasswd +2 -0
  31. data/test/runner.rb +2 -0
  32. data/test/server.cert +19 -0
  33. data/test/server.key +15 -0
  34. data/test/sslsvr.rb +65 -0
  35. data/test/subca.cert +21 -0
  36. data/test/test_auth.rb +196 -0
  37. data/test/test_cookie.rb +398 -0
  38. data/test/test_http-access2.rb +497 -0
  39. data/test/test_httpclient.rb +1544 -0
  40. data/test/test_ssl.rb +215 -0
  41. metadata +53 -38
  42. data/lib/http-access2.rbc +0 -732
  43. data/lib/httpclient.rbc +0 -13559
  44. data/lib/httpclient/auth.rbc +0 -13772
  45. data/lib/httpclient/connection.rbc +0 -767
  46. data/lib/httpclient/cookie.rbc +0 -8442
  47. data/lib/httpclient/http.rbc +0 -14163
  48. data/lib/httpclient/session.rbc +0 -15846
  49. data/lib/httpclient/ssl_config.rbc +0 -5575
  50. data/lib/httpclient/timeout.rbc +0 -2411
  51. data/lib/httpclient/util.rbc +0 -1278
@@ -0,0 +1,398 @@
1
+ require 'test/unit'
2
+ require 'uri'
3
+
4
+ require 'httpclient/cookie'
5
+
6
+ class TestCookie < Test::Unit::TestCase
7
+
8
+ def setup()
9
+ @c = WebAgent::Cookie.new()
10
+ end
11
+
12
+ def test_s_new()
13
+ assert_instance_of(WebAgent::Cookie, @c)
14
+ end
15
+
16
+ def test_discard?
17
+ assert_equal(false, !!(@c.discard?))
18
+ @c.discard = true
19
+ assert_equal(true, !!(@c.discard?))
20
+ end
21
+
22
+ def test_match()
23
+ url = URI.parse('http://www.rubycolor.org/hoge/funi/#919191')
24
+
25
+ @c.domain = 'www.rubycolor.org'
26
+ assert_equal(true, @c.match?(url))
27
+
28
+ @c.domain = '.rubycolor.org'
29
+ assert_equal(true, @c.match?(url))
30
+
31
+ @c.domain = 'aaa.www.rubycolor.org'
32
+ assert_equal(false, @c.match?(url))
33
+
34
+ @c.domain = 'aaa.www.rubycolor.org'
35
+ assert_equal(false, @c.match?(url))
36
+
37
+ @c.domain = 'www.rubycolor.org'
38
+ @c.path = '/'
39
+ assert_equal(true, @c.match?(url))
40
+
41
+ @c.domain = 'www.rubycolor.org'
42
+ @c.path = '/hoge'
43
+ assert_equal(true, @c.match?(url))
44
+
45
+ @c.domain = 'www.rubycolor.org'
46
+ @c.path = '/hoge/hoge'
47
+ assert_equal(false, @c.match?(url))
48
+
49
+ @c.domain = 'www.rubycolor.org'
50
+ @c.path = '/hoge'
51
+ @c.secure = true
52
+ assert_equal(false, @c.match?(url))
53
+
54
+ url2 = URI.parse('https://www.rubycolor.org/hoge/funi/#919191')
55
+ @c.domain = 'www.rubycolor.org'
56
+ @c.path = '/hoge'
57
+ @c.secure = true
58
+ assert_equal(true, @c.match?(url2))
59
+
60
+ @c.domain = 'www.rubycolor.org'
61
+ @c.path = '/hoge'
62
+ @c.secure = nil
63
+ assert_equal(true, @c.match?(url2)) ## not false!
64
+
65
+ url.port = 80
66
+ @c.domain = 'www.rubycolor.org'
67
+ @c.path = '/hoge'
68
+ # @c.port = [80,8080]
69
+ assert_equal(true, @c.match?(url))
70
+
71
+ end
72
+
73
+ def test_head_match?()
74
+ assert_equal(true, @c.head_match?("",""))
75
+ assert_equal(false, @c.head_match?("a",""))
76
+ assert_equal(true, @c.head_match?("","a"))
77
+ assert_equal(true, @c.head_match?("abcde","abcde"))
78
+ assert_equal(true, @c.head_match?("abcde","abcdef"))
79
+ assert_equal(false, @c.head_match?("abcdef","abcde"))
80
+ assert_equal(false, @c.head_match?("abcde","bcde"))
81
+ assert_equal(false, @c.head_match?("bcde","abcde"))
82
+ end
83
+
84
+ def test_tail_match?()
85
+ assert_equal(true, @c.tail_match?("",""))
86
+ assert_equal(false, @c.tail_match?("a",""))
87
+ assert_equal(true, @c.tail_match?("","a"))
88
+ assert_equal(true, @c.tail_match?("abcde","abcde"))
89
+ assert_equal(false, @c.tail_match?("abcde","abcdef"))
90
+ assert_equal(false, @c.tail_match?("abcdef","abcde"))
91
+ assert_equal(false, @c.tail_match?("abcde","bcde"))
92
+ assert_equal(true, @c.tail_match?("bcde","abcde"))
93
+ end
94
+
95
+
96
+ def test_domain_match()
97
+ extend WebAgent::CookieUtils
98
+ assert_equal(true, !!domain_match("hoge.co.jp","."))
99
+ # assert_equal(true, !!domain_match("locahost",".local"))
100
+ assert_equal(true, !!domain_match("192.168.10.1","192.168.10.1"))
101
+ assert_equal(false, !!domain_match("192.168.10.1","192.168.10.2"))
102
+ # assert_equal(false, !!domain_match("hoge.co.jp",".hoge.co.jp"))
103
+ # allows; host == rubyforge.org, domain == .rubyforge.org
104
+ assert_equal(true, !!domain_match("hoge.co.jp",".hoge.co.jp"))
105
+ assert_equal(true, !!domain_match("www.hoge.co.jp", "www.hoge.co.jp"))
106
+ assert_equal(false, !!domain_match("www.hoge.co.jp", "www2.hoge.co.jp"))
107
+ assert_equal(true, !!domain_match("www.hoge.co.jp", ".hoge.co.jp"))
108
+ assert_equal(true, !!domain_match("www.aa.hoge.co.jp", ".hoge.co.jp"))
109
+ assert_equal(false, !!domain_match("www.hoge.co.jp", "hoge.co.jp"))
110
+ end
111
+
112
+ def test_join_quotedstr()
113
+ arr1 = ['hoge=funi', 'hoge2=funi2']
114
+ assert_equal(arr1, @c.instance_eval{join_quotedstr(arr1,';')})
115
+ arr2 = ['hoge="fu', 'ni"', 'funi=funi']
116
+ assert_equal(['hoge="fu;ni"','funi=funi'],
117
+ @c.instance_eval{join_quotedstr(arr2,';')})
118
+ arr3 = ['hoge="funi";hoge2="fu','ni2";hoge3="hoge"', 'funi="funi"']
119
+ assert_equal(['hoge="funi";hoge2="fu,ni2";hoge3="hoge"', 'funi="funi"'],
120
+ @c.instance_eval{join_quotedstr(arr3,',')})
121
+ end
122
+
123
+ end
124
+
125
+ class TestCookieManager < Test::Unit::TestCase
126
+
127
+ def setup()
128
+ @cm = WebAgent::CookieManager.new()
129
+ end
130
+
131
+ def teardown()
132
+ end
133
+
134
+ def test_total_dot_num()
135
+ assert_equal(0, @cm.total_dot_num(""))
136
+ assert_equal(0, @cm.total_dot_num("abcde"))
137
+ assert_equal(1, @cm.total_dot_num("ab.cde"))
138
+ assert_equal(1, @cm.total_dot_num(".abcde"))
139
+ assert_equal(1, @cm.total_dot_num("abcde."))
140
+ assert_equal(2, @cm.total_dot_num("abc.de."))
141
+ assert_equal(2, @cm.total_dot_num("a.bc.de"))
142
+ assert_equal(2, @cm.total_dot_num(".abcde."))
143
+ assert_equal(3, @cm.total_dot_num(".a.bcde."))
144
+ assert_equal(3, @cm.total_dot_num("a.b.cde."))
145
+ assert_equal(3, @cm.total_dot_num("a.b.c.de"))
146
+ end
147
+
148
+ def test_parse()
149
+ str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; expires=Wed, 01-Dec-2010 00:00:00 GMT; path=/"
150
+ @cm.parse(str,URI.parse('http://www.test.jp'))
151
+ cookie = @cm.cookies[0]
152
+ assert_instance_of(WebAgent::Cookie, cookie)
153
+ assert_equal("inkid", cookie.name)
154
+ assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value)
155
+ assert_equal(Time.gm(2010, 12, 1, 0,0,0), cookie.expires)
156
+ assert_equal("/", cookie.path)
157
+ end
158
+
159
+ def test_parse2()
160
+ str = "xmen=off,0,0,1; path=/; domain=.excite.co.jp; expires=Wednesday, 31-Dec-2037 12:00:00 GMT"
161
+ @cm.parse(str,URI.parse('http://www.excite.co.jp'))
162
+ cookie = @cm.cookies[0]
163
+ assert_instance_of(WebAgent::Cookie, cookie)
164
+ assert_equal("xmen", cookie.name)
165
+ assert_equal("off,0,0,1", cookie.value)
166
+ assert_equal("/", cookie.path)
167
+ assert_equal(".excite.co.jp", cookie.domain)
168
+ assert_equal(Time.gm(2037,12,31,12,0,0), cookie.expires)
169
+ end
170
+
171
+ def test_parse3()
172
+ str = "xmen=off,0,0,1; path=/; domain=.excite.co.jp; expires=Wednesday, 31-Dec-2037 12:00:00 GMT;Secure;HTTPOnly"
173
+ @cm.parse(str,URI.parse('http://www.excite.co.jp'))
174
+ cookie = @cm.cookies[0]
175
+ assert_instance_of(WebAgent::Cookie, cookie)
176
+ assert_equal("xmen", cookie.name)
177
+ assert_equal("off,0,0,1", cookie.value)
178
+ assert_equal("/", cookie.path)
179
+ assert_equal(".excite.co.jp", cookie.domain)
180
+ assert_equal(Time.gm(2037,12,31,12,0,0), cookie.expires)
181
+ assert_equal(true, cookie.secure?)
182
+ assert_equal(true, cookie.http_only?)
183
+ end
184
+
185
+ def test_parse_double_semicolon()
186
+ str = "xmen=off,0,0,1;; path=\"/;;\"; domain=.excite.co.jp; expires=Wednesday, 31-Dec-2037 12:00:00 GMT"
187
+ @cm.parse(str,URI.parse('http://www.excite.co.jp'))
188
+ cookie = @cm.cookies[0]
189
+ assert_instance_of(WebAgent::Cookie, cookie)
190
+ assert_equal("xmen", cookie.name)
191
+ assert_equal("off,0,0,1", cookie.value)
192
+ assert_equal("/;;", cookie.path)
193
+ assert_equal(".excite.co.jp", cookie.domain)
194
+ assert_equal(Time.gm(2037,12,31,12,0,0), cookie.expires)
195
+ end
196
+
197
+ # def test_make_portlist()
198
+ # assert_equal([80,8080], @cm.instance_eval{make_portlist("80,8080")})
199
+ # assert_equal([80], @cm.instance_eval{make_portlist("80")})
200
+ # assert_equal([80,8080,10080], @cm.instance_eval{make_portlist(" 80, 8080, 10080 \n")})
201
+ # end
202
+
203
+ def test_check_expired_cookies()
204
+ c1 = WebAgent::Cookie.new()
205
+ c2 = c1.dup
206
+ c3 = c1.dup
207
+ c4 = c1.dup
208
+ c1.expires = Time.now - 100
209
+ c2.expires = Time.now + 100
210
+ c3.expires = Time.now - 10
211
+ c4.expires = nil
212
+ cookies = [c1,c2,c3,c4]
213
+ @cm.cookies = cookies
214
+ @cm.check_expired_cookies()
215
+ # expires == nil cookies (session cookie) exists.
216
+ assert_equal([c2,c4], @cm.cookies)
217
+ end
218
+
219
+ def test_parse_expires
220
+ str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; expires=; path=/"
221
+ @cm.parse(str,URI.parse('http://www.test.jp'))
222
+ cookie = @cm.cookies[0]
223
+ assert_equal("inkid", cookie.name)
224
+ assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value)
225
+ assert_equal(nil, cookie.expires)
226
+ assert_equal("/", cookie.path)
227
+ #
228
+ str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; path=/; expires="
229
+ @cm.parse(str,URI.parse('http://www.test.jp'))
230
+ cookie = @cm.cookies[0]
231
+ assert_equal("inkid", cookie.name)
232
+ assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value)
233
+ assert_equal(nil, cookie.expires)
234
+ assert_equal("/", cookie.path)
235
+ #
236
+ str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; path=/; expires=\"\""
237
+ @cm.parse(str,URI.parse('http://www.test.jp'))
238
+ cookie = @cm.cookies[0]
239
+ assert_equal("inkid", cookie.name)
240
+ assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value)
241
+ assert_equal(nil, cookie.expires)
242
+ assert_equal("/", cookie.path)
243
+ end
244
+
245
+ def test_find_cookie()
246
+ str = "xmen=off,0,0,1; path=/; domain=.excite2.co.jp; expires=Wednesday, 31-Dec-2037 12:00:00 GMT"
247
+ @cm.parse(str, URI.parse("http://www.excite2.co.jp/"))
248
+
249
+ str = "xmen=off,0,0,2; path=/; domain=.excite.co.jp; expires=Wednesday, 31-Dec-2037 12:00:00 GMT"
250
+ @cm.parse(str, URI.parse("http://www.excite.co.jp/"))
251
+
252
+ @cm.cookies[0].use = true
253
+ @cm.cookies[1].use = true
254
+
255
+ url = URI.parse('http://www.excite.co.jp/hoge/funi/')
256
+ cookie_str = @cm.find(url)
257
+ assert_equal("xmen=off,0,0,2", cookie_str)
258
+ end
259
+
260
+ def test_load_cookies()
261
+ begin
262
+ File.open("tmp_test.tmp","w") {|f|
263
+ f.write <<EOF
264
+ http://www.zdnet.co.jp/news/0106/08/e_gibson.html NGUserID d29b8f49-10875-992421294-1 2145801600 www.zdnet.co.jp / 9 0
265
+ http://www.zdnet.co.jp/news/0106/08/e_gibson.html PACK zd3-992421294-7436 1293839999 .zdnet.co.jp / 13 0
266
+ http://example.org/ key value 0 .example.org / 13 0
267
+ http://example.org/ key value .example.org / 13 0
268
+ EOF
269
+ }
270
+
271
+ @cm.cookies_file = 'tmp_test.tmp'
272
+ @cm.load_cookies()
273
+ c0, c1, c2, c3 = @cm.cookies
274
+ assert_equal('http://www.zdnet.co.jp/news/0106/08/e_gibson.html', c0.url.to_s)
275
+ assert_equal('NGUserID', c0.name)
276
+ assert_equal('d29b8f49-10875-992421294-1', c0.value)
277
+ assert_equal(Time.at(2145801600), c0.expires)
278
+ assert_equal('www.zdnet.co.jp', c0.domain)
279
+ assert_equal('/', c0.path)
280
+ assert_equal(9, c0.flag)
281
+ #
282
+ assert_equal('http://www.zdnet.co.jp/news/0106/08/e_gibson.html', c1.url.to_s)
283
+ assert_equal('PACK', c1.name)
284
+ assert_equal('zd3-992421294-7436', c1.value)
285
+ assert_equal(Time.at(1293839999), c1.expires)
286
+ assert_equal('.zdnet.co.jp', c1.domain)
287
+ assert_equal('/', c1.path)
288
+ assert_equal(13, c1.flag)
289
+ #
290
+ assert_equal(nil, c2.expires)
291
+ assert_equal(nil, c3.expires) # allow empty 'expires' (should not happen)
292
+ ensure
293
+ File.unlink("tmp_test.tmp")
294
+ end
295
+ end
296
+
297
+ def test_save_cookie()
298
+ str = <<EOF
299
+ http://www.zdnet.co.jp/news/0106/08/e_gibson.html NGUserID d29b8f49-10875-992421294-1 2145801600 www.zdnet.co.jp / 9
300
+ http://www.zdnet.co.jp/news/0106/08/e_gibson.html PACK zd3-992421294-7436 2145801600 .zdnet.co.jp / 13
301
+ EOF
302
+ begin
303
+ File.open("tmp_test.tmp","w") {|f|
304
+ f.write str
305
+ }
306
+ @cm.cookies_file = 'tmp_test.tmp'
307
+ @cm.load_cookies()
308
+ @cm.instance_eval{@is_saved = false}
309
+ @cm.cookies_file = 'tmp_test2.tmp'
310
+ @cm.save_cookies()
311
+ str2 = ''
312
+ File.open("tmp_test2.tmp","r") {|f|
313
+ str2 = f.read()
314
+ }
315
+ assert_equal(str, str2)
316
+ #
317
+ assert(File.exist?('tmp_test2.tmp'))
318
+ File.unlink("tmp_test2.tmp")
319
+ @cm.save_cookies()
320
+ assert(!File.exist?('tmp_test2.tmp'))
321
+ @cm.save_cookies(true)
322
+ assert(File.exist?('tmp_test2.tmp'))
323
+ ensure
324
+ File.unlink("tmp_test.tmp")
325
+ if FileTest.exist?("tmp_test2.tmp")
326
+ File.unlink("tmp_test2.tmp")
327
+ end
328
+ end
329
+ end
330
+
331
+ def test_not_saved_expired_cookies
332
+ begin
333
+ @cm.cookies_file = 'tmp_test.tmp'
334
+ uri = URI.parse('http://www.example.org')
335
+ @cm.parse("foo=1; path=/", uri)
336
+ @cm.parse("bar=2; path=/; expires=", uri)
337
+ @cm.parse("baz=3; path=/; expires=\"\"", uri)
338
+ @cm.parse("qux=4; path=/; expires=#{(Time.now + 10).asctime}", uri)
339
+ @cm.parse("quxx=5; path=/; expires=#{(Time.now - 10).asctime}", uri)
340
+ @cm.save_cookies()
341
+ @cm.load_cookies
342
+ assert_equal(1, @cm.cookies.size) # +10 cookies only
343
+ ensure
344
+ File.unlink("tmp_test.tmp") if File.exist?("tmp_test.tmp")
345
+ end
346
+ end
347
+
348
+ def test_add()
349
+ c = WebAgent::Cookie.new()
350
+ c.name = "hoge"
351
+ c.value = "funi"
352
+ c.url = URI.parse("http://www.inac.co.jp/hoge")
353
+ @cm.add(c)
354
+ c = @cm.cookies[0]
355
+ assert_equal('hoge', c.name)
356
+ assert_equal('funi', c.value)
357
+ assert_equal(nil, c.expires)
358
+ end
359
+
360
+ def test_add2()
361
+ c = WebAgent::Cookie.new()
362
+ c.name = "hoge"
363
+ c.value = "funi"
364
+ c.path = ''
365
+ c.url = URI.parse("http://www.inac.co.jp/hoge/hoge2/hoge3")
366
+ @cm.add(c)
367
+ #
368
+ c = WebAgent::Cookie.new()
369
+ c.name = "hoge"
370
+ c.value = "funi"
371
+ #c.path = '' NO path given -> same as URL
372
+ c.url = URI.parse("http://www.inac.co.jp/hoge/hoge2/hoge3")
373
+ @cm.add(c)
374
+ #
375
+ c1, c2 = @cm.cookies
376
+ assert_equal('', c1.path)
377
+ assert_equal('/hoge/hoge2', c2.path)
378
+ end
379
+
380
+ def test_check_cookie_accept_domain()
381
+ @cm.accept_domains = [".example1.co.jp", "www1.example.jp"]
382
+ @cm.reject_domains = [".example2.co.jp", "www2.example.jp"]
383
+ check1 = @cm.check_cookie_accept_domain("www.example1.co.jp")
384
+ assert_equal(true, check1)
385
+ check2 = @cm.check_cookie_accept_domain("www.example2.co.jp")
386
+ assert_equal(false, check2)
387
+ check3 = @cm.check_cookie_accept_domain("www1.example.jp")
388
+ assert_equal(true, check3)
389
+ check4 = @cm.check_cookie_accept_domain("www2.example.jp")
390
+ assert_equal(false, check4)
391
+ check5 = @cm.check_cookie_accept_domain("aa.www2.example.jp")
392
+ assert_equal(true, check5)
393
+ check6 = @cm.check_cookie_accept_domain("aa.www2.example.jp")
394
+ assert_equal(true, check6)
395
+ assert_equal(false, @cm.check_cookie_accept_domain(nil))
396
+ end
397
+
398
+ end
@@ -0,0 +1,497 @@
1
+ require 'http-access2'
2
+ require File.expand_path('helper', File.dirname(__FILE__))
3
+
4
+
5
+ module HTTPAccess2
6
+
7
+
8
+ class TestClient < Test::Unit::TestCase
9
+ include Helper
10
+
11
+ def setup
12
+ super
13
+ setup_server
14
+ setup_client
15
+ end
16
+
17
+ def teardown
18
+ super
19
+ end
20
+
21
+ def test_initialize
22
+ setup_proxyserver
23
+ escape_noproxy do
24
+ @proxyio.string = ""
25
+ @client = HTTPAccess2::Client.new(proxyurl)
26
+ assert_equal(URI.parse(proxyurl), @client.proxy)
27
+ assert_equal(200, @client.head(serverurl).status)
28
+ assert(!@proxyio.string.empty?)
29
+ end
30
+ end
31
+
32
+ def test_agent_name
33
+ @client = HTTPAccess2::Client.new(nil, "agent_name_foo")
34
+ str = ""
35
+ @client.debug_dev = str
36
+ @client.get(serverurl)
37
+ lines = str.split(/(?:\r?\n)+/)
38
+ assert_equal("= Request", lines[0])
39
+ assert_match(/^User-Agent: agent_name_foo/, lines[4])
40
+ end
41
+
42
+ def test_from
43
+ @client = HTTPAccess2::Client.new(nil, nil, "from_bar")
44
+ str = ""
45
+ @client.debug_dev = str
46
+ @client.get(serverurl)
47
+ lines = str.split(/(?:\r?\n)+/)
48
+ assert_equal("= Request", lines[0])
49
+ assert_match(/^From: from_bar/, lines[4])
50
+ end
51
+
52
+ def test_debug_dev
53
+ str = ""
54
+ @client.debug_dev = str
55
+ assert(str.empty?)
56
+ @client.get(serverurl)
57
+ assert(!str.empty?)
58
+ end
59
+
60
+ def _test_protocol_version_http09
61
+ @client.protocol_version = 'HTTP/0.9'
62
+ str = ""
63
+ @client.debug_dev = str
64
+ @client.get(serverurl + 'hello')
65
+ lines = str.split(/(?:\r?\n)+/)
66
+ assert_equal("= Request", lines[0])
67
+ assert_equal("! CONNECTION ESTABLISHED", lines[2])
68
+ assert_equal("GET /hello HTTP/0.9", lines[3])
69
+ assert_equal("Connection: close", lines[5])
70
+ assert_equal("= Response", lines[6])
71
+ assert_match(/^hello/, lines[7])
72
+ end
73
+
74
+ def test_protocol_version_http10
75
+ @client.protocol_version = 'HTTP/1.0'
76
+ str = ""
77
+ @client.debug_dev = str
78
+ @client.get(serverurl + 'hello')
79
+ lines = str.split(/(?:\r?\n)+/)
80
+ assert_equal("= Request", lines[0])
81
+ assert_equal("! CONNECTION ESTABLISHED", lines[2])
82
+ assert_equal("GET /hello HTTP/1.0", lines[3])
83
+ assert_equal("Connection: close", lines[5])
84
+ assert_equal("= Response", lines[6])
85
+ end
86
+
87
+ def test_protocol_version_http11
88
+ str = ""
89
+ @client.debug_dev = str
90
+ @client.get(serverurl)
91
+ lines = str.split(/(?:\r?\n)+/)
92
+ assert_equal("= Request", lines[0])
93
+ assert_equal("! CONNECTION ESTABLISHED", lines[2])
94
+ assert_equal("GET / HTTP/1.1", lines[3])
95
+ assert_equal("Host: localhost:#{serverport}", lines[5])
96
+ @client.protocol_version = 'HTTP/1.1'
97
+ str = ""
98
+ @client.debug_dev = str
99
+ @client.get(serverurl)
100
+ lines = str.split(/(?:\r?\n)+/)
101
+ assert_equal("= Request", lines[0])
102
+ assert_equal("! CONNECTION ESTABLISHED", lines[2])
103
+ assert_equal("GET / HTTP/1.1", lines[3])
104
+ @client.protocol_version = 'HTTP/1.0'
105
+ str = ""
106
+ @client.debug_dev = str
107
+ @client.get(serverurl)
108
+ lines = str.split(/(?:\r?\n)+/)
109
+ assert_equal("= Request", lines[0])
110
+ assert_equal("! CONNECTION ESTABLISHED", lines[2])
111
+ assert_equal("GET / HTTP/1.0", lines[3])
112
+ end
113
+
114
+ def test_proxy
115
+ setup_proxyserver
116
+ escape_noproxy do
117
+ assert_raises(URI::InvalidURIError) do
118
+ @client.proxy = "http://"
119
+ end
120
+ @client.proxy = ""
121
+ assert_nil(@client.proxy)
122
+ @client.proxy = "http://foo:1234"
123
+ assert_equal(URI.parse("http://foo:1234"), @client.proxy)
124
+ uri = URI.parse("http://bar:2345")
125
+ @client.proxy = uri
126
+ assert_equal(uri, @client.proxy)
127
+ #
128
+ @proxyio.string = ""
129
+ @client.proxy = nil
130
+ assert_equal(200, @client.head(serverurl).status)
131
+ assert(@proxyio.string.empty?)
132
+ #
133
+ @proxyio.string = ""
134
+ @client.proxy = proxyurl
135
+ assert_equal(200, @client.head(serverurl).status)
136
+ assert(!@proxyio.string.empty?)
137
+ end
138
+ end
139
+
140
+ def test_noproxy_for_localhost
141
+ @proxyio.string = ""
142
+ @client.proxy = proxyurl
143
+ assert_equal(200, @client.head(serverurl).status)
144
+ assert(@proxyio.string.empty?)
145
+ end
146
+
147
+ def test_no_proxy
148
+ setup_proxyserver
149
+ escape_noproxy do
150
+ # proxy is not set.
151
+ @client.no_proxy = 'localhost'
152
+ @proxyio.string = ""
153
+ @client.proxy = nil
154
+ assert_equal(200, @client.head(serverurl).status)
155
+ assert(/accept/ !~ @proxyio.string)
156
+ #
157
+ @proxyio.string = ""
158
+ @client.proxy = proxyurl
159
+ assert_equal(200, @client.head(serverurl).status)
160
+ assert(/accept/ !~ @proxyio.string)
161
+ #
162
+ @client.no_proxy = 'foobar'
163
+ @proxyio.string = ""
164
+ @client.proxy = proxyurl
165
+ assert_equal(200, @client.head(serverurl).status)
166
+ assert(/accept/ =~ @proxyio.string)
167
+ #
168
+ @client.no_proxy = 'foobar,localhost:baz'
169
+ @proxyio.string = ""
170
+ @client.proxy = proxyurl
171
+ assert_equal(200, @client.head(serverurl).status)
172
+ assert(/accept/ !~ @proxyio.string)
173
+ #
174
+ @client.no_proxy = 'foobar,localhost:443'
175
+ @proxyio.string = ""
176
+ @client.proxy = proxyurl
177
+ assert_equal(200, @client.head(serverurl).status)
178
+ assert(/accept/ =~ @proxyio.string)
179
+ #
180
+ @client.no_proxy = "foobar,localhost:443:localhost:#{serverport},baz"
181
+ @proxyio.string = ""
182
+ @client.proxy = proxyurl
183
+ assert_equal(200, @client.head(serverurl).status)
184
+ assert(/accept/ !~ @proxyio.string)
185
+ end
186
+ end
187
+
188
+ def test_get_content
189
+ assert_equal('hello', @client.get_content(serverurl + 'hello'))
190
+ assert_equal('hello', @client.get_content(serverurl + 'redirect1'))
191
+ assert_equal('hello', @client.get_content(serverurl + 'redirect2'))
192
+ assert_raises(HTTPClient::Session::BadResponse) do
193
+ @client.get_content(serverurl + 'notfound')
194
+ end
195
+ assert_raises(HTTPClient::Session::BadResponse) do
196
+ @client.get_content(serverurl + 'redirect_self')
197
+ end
198
+ called = false
199
+ @client.redirect_uri_callback = lambda { |uri, res|
200
+ newuri = res.header['location'][0]
201
+ called = true
202
+ newuri
203
+ }
204
+ assert_equal('hello', @client.get_content(serverurl + 'relative_redirect'))
205
+ assert(called)
206
+ end
207
+
208
+ def test_post_content
209
+ assert_equal('hello', @client.post_content(serverurl + 'hello'))
210
+ assert_equal('hello', @client.post_content(serverurl + 'redirect1'))
211
+ assert_equal('hello', @client.post_content(serverurl + 'redirect2'))
212
+ assert_raises(HTTPClient::Session::BadResponse) do
213
+ @client.post_content(serverurl + 'notfound')
214
+ end
215
+ assert_raises(HTTPClient::Session::BadResponse) do
216
+ @client.post_content(serverurl + 'redirect_self')
217
+ end
218
+ called = false
219
+ @client.redirect_uri_callback = lambda { |uri, res|
220
+ newuri = res.header['location'][0]
221
+ called = true
222
+ newuri
223
+ }
224
+ assert_equal('hello', @client.post_content(serverurl + 'relative_redirect'))
225
+ assert(called)
226
+ end
227
+
228
+ def test_head
229
+ assert_equal("head", @client.head(serverurl + 'servlet').header["x-head"][0])
230
+ res = @client.head(serverurl + 'servlet', {1=>2, 3=>4})
231
+ assert_equal('1=2&3=4', res.header["x-query"][0])
232
+ end
233
+
234
+ def test_get
235
+ assert_equal("get", @client.get(serverurl + 'servlet').content)
236
+ res = @client.get(serverurl + 'servlet', {1=>2, 3=>4})
237
+ assert_equal('1=2&3=4', res.header["x-query"][0])
238
+ end
239
+
240
+ def test_post
241
+ assert_equal("post", @client.post(serverurl + 'servlet').content)
242
+ res = @client.get(serverurl + 'servlet', {1=>2, 3=>4})
243
+ assert_equal('1=2&3=4', res.header["x-query"][0])
244
+ end
245
+
246
+ def test_put
247
+ assert_equal("put", @client.put(serverurl + 'servlet').content)
248
+ res = @client.get(serverurl + 'servlet', {1=>2, 3=>4})
249
+ assert_equal('1=2&3=4', res.header["x-query"][0])
250
+ end
251
+
252
+ def test_delete
253
+ assert_equal("delete", @client.delete(serverurl + 'servlet').content)
254
+ res = @client.get(serverurl + 'servlet', {1=>2, 3=>4})
255
+ assert_equal('1=2&3=4', res.header["x-query"][0])
256
+ end
257
+
258
+ def test_options
259
+ assert_equal("options", @client.options(serverurl + 'servlet').content)
260
+ res = @client.get(serverurl + 'servlet', {1=>2, 3=>4})
261
+ assert_equal('1=2&3=4', res.header["x-query"][0])
262
+ end
263
+
264
+ def test_trace
265
+ assert_equal("trace", @client.trace(serverurl + 'servlet').content)
266
+ res = @client.get(serverurl + 'servlet', {1=>2, 3=>4})
267
+ assert_equal('1=2&3=4', res.header["x-query"][0])
268
+ end
269
+
270
+ def test_get_query
271
+ assert_equal({'1'=>'2'}, check_query_get({1=>2}))
272
+ assert_equal({'a'=>'A', 'B'=>'b'}, check_query_get({"a"=>"A", "B"=>"b"}))
273
+ assert_equal({'&'=>'&'}, check_query_get({"&"=>"&"}))
274
+ assert_equal({'= '=>' =+'}, check_query_get({"= "=>" =+"}))
275
+ assert_equal(
276
+ ['=', '&'].sort,
277
+ check_query_get([["=", "="], ["=", "&"]])['='].to_ary.sort
278
+ )
279
+ assert_equal({'123'=>'45'}, check_query_get('123=45'))
280
+ assert_equal({'12 3'=>'45', ' '=>' '}, check_query_get('12+3=45&+=+'))
281
+ assert_equal({}, check_query_get(''))
282
+ end
283
+
284
+ def test_post_body
285
+ assert_equal({'1'=>'2'}, check_query_post({1=>2}))
286
+ assert_equal({'a'=>'A', 'B'=>'b'}, check_query_post({"a"=>"A", "B"=>"b"}))
287
+ assert_equal({'&'=>'&'}, check_query_post({"&"=>"&"}))
288
+ assert_equal({'= '=>' =+'}, check_query_post({"= "=>" =+"}))
289
+ assert_equal(
290
+ ['=', '&'].sort,
291
+ check_query_post([["=", "="], ["=", "&"]])['='].to_ary.sort
292
+ )
293
+ assert_equal({'123'=>'45'}, check_query_post('123=45'))
294
+ assert_equal({'12 3'=>'45', ' '=>' '}, check_query_post('12+3=45&+=+'))
295
+ assert_equal({}, check_query_post(''))
296
+ #
297
+ post_body = StringIO.new("foo=bar&foo=baz")
298
+ assert_equal(
299
+ ["bar", "baz"],
300
+ check_query_post(post_body)["foo"].to_ary.sort
301
+ )
302
+ end
303
+
304
+ def test_extra_headers
305
+ str = ""
306
+ @client.debug_dev = str
307
+ @client.head(serverurl, nil, {"ABC" => "DEF"})
308
+ lines = str.split(/(?:\r?\n)+/)
309
+ assert_equal("= Request", lines[0])
310
+ assert_match("ABC: DEF", lines[4])
311
+ #
312
+ str = ""
313
+ @client.debug_dev = str
314
+ @client.get(serverurl, nil, [["ABC", "DEF"], ["ABC", "DEF"]])
315
+ lines = str.split(/(?:\r?\n)+/)
316
+ assert_equal("= Request", lines[0])
317
+ assert_match("ABC: DEF", lines[4])
318
+ assert_match("ABC: DEF", lines[5])
319
+ end
320
+
321
+ def test_timeout
322
+ assert_equal(60, @client.connect_timeout)
323
+ assert_equal(120, @client.send_timeout)
324
+ assert_equal(60, @client.receive_timeout)
325
+ end
326
+
327
+ def test_connect_timeout
328
+ # ToDo
329
+ end
330
+
331
+ def test_send_timeout
332
+ # ToDo
333
+ end
334
+
335
+ def test_receive_timeout
336
+ # this test takes 2 sec
337
+ assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=2'))
338
+ @client.receive_timeout = 1
339
+ assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=0'))
340
+ assert_raise(HTTPClient::ReceiveTimeoutError) do
341
+ @client.get_content(serverurl + 'sleep?sec=2')
342
+ end
343
+ @client.receive_timeout = 3
344
+ assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=2'))
345
+ end
346
+
347
+ def test_cookies
348
+ cookiefile = File.join(File.dirname(File.expand_path(__FILE__)),
349
+ 'test_cookies_file')
350
+ # from [ruby-talk:164079]
351
+ File.open(cookiefile, "wb") do |f|
352
+ f << "http://rubyforge.org//account/login.php session_ser LjEwMy45Ni40Ni0q%2A-fa0537de8cc31 1131676286 .rubyforge.org / 13\n"
353
+ end
354
+ cm = WebAgent::CookieManager::new(cookiefile)
355
+ cm.load_cookies
356
+ cookie = cm.cookies.first
357
+ url = cookie.url
358
+ assert(cookie.domain_match(url.host, cookie.domain))
359
+ end
360
+
361
+ private
362
+
363
+ def check_query_get(query)
364
+ WEBrick::HTTPUtils.parse_query(
365
+ @client.get(serverurl + 'servlet', query).header["x-query"][0]
366
+ )
367
+ end
368
+
369
+ def check_query_post(query)
370
+ WEBrick::HTTPUtils.parse_query(
371
+ @client.post(serverurl + 'servlet', query).header["x-query"][0]
372
+ )
373
+ end
374
+
375
+ def setup_server
376
+ @server = WEBrick::HTTPServer.new(
377
+ :BindAddress => "localhost",
378
+ :Logger => @logger,
379
+ :Port => 0,
380
+ :AccessLog => [],
381
+ :DocumentRoot => File.dirname(File.expand_path(__FILE__))
382
+ )
383
+ @serverport = @server.config[:Port]
384
+ [:hello, :sleep, :redirect1, :redirect2, :redirect3, :redirect_self, :relative_redirect].each do |sym|
385
+ @server.mount(
386
+ "/#{sym}",
387
+ WEBrick::HTTPServlet::ProcHandler.new(method("do_#{sym}").to_proc)
388
+ )
389
+ end
390
+ @server.mount('/servlet', TestServlet.new(@server))
391
+ @server_thread = start_server_thread(@server)
392
+ end
393
+
394
+ def escape_noproxy
395
+ backup = HTTPAccess2::Client::NO_PROXY_HOSTS.dup
396
+ HTTPAccess2::Client::NO_PROXY_HOSTS.clear
397
+ yield
398
+ ensure
399
+ HTTPAccess2::Client::NO_PROXY_HOSTS.replace(backup)
400
+ end
401
+
402
+ def do_hello(req, res)
403
+ res['content-type'] = 'text/html'
404
+ res.body = "hello"
405
+ end
406
+
407
+ def do_sleep(req, res)
408
+ sec = req.query['sec'].to_i
409
+ sleep sec
410
+ res['content-type'] = 'text/html'
411
+ res.body = "hello"
412
+ end
413
+
414
+ def do_redirect1(req, res)
415
+ res.set_redirect(WEBrick::HTTPStatus::MovedPermanently, serverurl + "hello")
416
+ end
417
+
418
+ def do_redirect2(req, res)
419
+ res.set_redirect(WEBrick::HTTPStatus::TemporaryRedirect, serverurl + "redirect3")
420
+ end
421
+
422
+ def do_redirect3(req, res)
423
+ res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "hello")
424
+ end
425
+
426
+ def do_redirect_self(req, res)
427
+ res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "redirect_self")
428
+ end
429
+
430
+ def do_relative_redirect(req, res)
431
+ res.set_redirect(WEBrick::HTTPStatus::Found, "hello")
432
+ end
433
+
434
+ class TestServlet < WEBrick::HTTPServlet::AbstractServlet
435
+ def get_instance(*arg)
436
+ self
437
+ end
438
+
439
+ def do_HEAD(req, res)
440
+ res["x-head"] = 'head' # use this for test purpose only.
441
+ res["x-query"] = query_response(req)
442
+ end
443
+
444
+ def do_GET(req, res)
445
+ res.body = 'get'
446
+ res["x-query"] = query_response(req)
447
+ end
448
+
449
+ def do_POST(req, res)
450
+ res.body = 'post'
451
+ res["x-query"] = body_response(req)
452
+ end
453
+
454
+ def do_PUT(req, res)
455
+ res.body = 'put'
456
+ end
457
+
458
+ def do_DELETE(req, res)
459
+ res.body = 'delete'
460
+ end
461
+
462
+ def do_OPTIONS(req, res)
463
+ # check RFC for legal response.
464
+ res.body = 'options'
465
+ end
466
+
467
+ def do_TRACE(req, res)
468
+ # client SHOULD reflect the message received back to the client as the
469
+ # entity-body of a 200 (OK) response. [RFC2616]
470
+ res.body = 'trace'
471
+ res["x-query"] = query_response(req)
472
+ end
473
+
474
+ private
475
+
476
+ def query_response(req)
477
+ query_escape(WEBrick::HTTPUtils.parse_query(req.query_string))
478
+ end
479
+
480
+ def body_response(req)
481
+ query_escape(WEBrick::HTTPUtils.parse_query(req.body))
482
+ end
483
+
484
+ def query_escape(query)
485
+ escaped = []
486
+ query.collect do |k, v|
487
+ v.to_ary.each do |ve|
488
+ escaped << CGI.escape(k) + '=' + CGI.escape(ve)
489
+ end
490
+ end
491
+ escaped.join('&')
492
+ end
493
+ end
494
+ end
495
+
496
+
497
+ end