1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
# $Id$
require 'test/unit'
require 'net/http'
require 'stringio'
require File.expand_path("utils", File.dirname(__FILE__))
module TestNetHTTP_version_1_1_methods
def test_s_get
assert_equal $test_net_http_data,
Net::HTTP.get(config('host'), '/', config('port'))
end
def test_head
start {|http|
res = http.head('/')
assert_kind_of Net::HTTPResponse, res
assert_equal $test_net_http_data_type, res['Content-Type']
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_equal $test_net_http_data.size, res['Content-Length'].to_i
end
}
end
def test_get
start {|http|
_test_get__get http
_test_get__iter http
_test_get__chunked http
}
end
def _test_get__get(http)
res = http.get('/')
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_equal $test_net_http_data_type, res['Content-Type']
assert_equal $test_net_http_data.size, res.body.size
assert_equal $test_net_http_data, res.body
end
def _test_get__iter(http)
buf = ''
res = http.get('/') {|s| buf << s }
assert_kind_of Net::HTTPResponse, res
# assert_kind_of String, res.body
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_equal $test_net_http_data_type, res['Content-Type']
assert_equal $test_net_http_data.size, buf.size
assert_equal $test_net_http_data, buf
# assert_equal $test_net_http_data.size, res.body.size
# assert_equal $test_net_http_data, res.body
end
def _test_get__chunked(http)
buf = ''
res = http.get('/') {|s| buf << s }
assert_kind_of Net::HTTPResponse, res
# assert_kind_of String, res.body
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_equal $test_net_http_data_type, res['Content-Type']
assert_equal $test_net_http_data.size, buf.size
assert_equal $test_net_http_data, buf
# assert_equal $test_net_http_data.size, res.body.size
# assert_equal $test_net_http_data, res.body
end
def test_get__break
i = 0
start {|http|
http.get('/') do |str|
i += 1
break
end
}
assert_equal 1, i
end
def test_get__implicit_start
res = new().get('/')
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
end
assert_equal $test_net_http_data_type, res['Content-Type']
assert_equal $test_net_http_data.size, res.body.size
assert_equal $test_net_http_data, res.body
end
def test_get2
start {|http|
http.get2('/') {|res|
assert_kind_of Net::HTTPResponse, res
assert_kind_of Net::HTTPResponse, res.header
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
end
assert_equal $test_net_http_data_type, res['Content-Type']
assert_kind_of String, res.body
assert_kind_of String, res.entity
assert_equal $test_net_http_data.size, res.body.size
assert_equal $test_net_http_data, res.body
assert_equal $test_net_http_data, res.entity
}
}
end
def test_post
start {|http|
_test_post__base http
_test_post__file http
}
end
def _test_post__base(http)
uheader = {}
uheader['Accept'] = 'application/octet-stream'
data = 'post data'
res = http.post('/', data)
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
assert_equal data, res.body
assert_equal data, res.entity
end
def _test_post__file(http)
data = 'post data'
f = StringIO.new
http.post('/', data, nil, f)
assert_equal data, f.string
end
def test_s_post_form
res = Net::HTTP.post_form(
URI.parse("http://#{config('host')}:#{config('port')}/"),
"a" => "x")
assert_equal ["a=x"], res.body.split(/[;&]/).sort
res = Net::HTTP.post_form(
URI.parse("http://#{config('host')}:#{config('port')}/"),
"a" => "x",
"b" => "y")
assert_equal ["a=x", "b=y"], res.body.split(/[;&]/).sort
res = Net::HTTP.post_form(
URI.parse("http://#{config('host')}:#{config('port')}/"),
"a" => ["x1", "x2"],
"b" => "y")
assert_equal ["a=x1", "a=x2", "b=y"], res.body.split(/[;&]/).sort
end
def test_patch
start {|http|
_test_patch__base http
}
end
def _test_patch__base(http)
uheader = {}
uheader['Accept'] = 'application/octet-stream'
data = 'patch data'
res = http.patch('/', data)
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
assert_equal data, res.body
assert_equal data, res.entity
end
end
module TestNetHTTP_version_1_2_methods
def test_request
start {|http|
_test_request__GET http
_test_request__file http
# _test_request__range http # WEBrick does not support Range: header.
_test_request__HEAD http
_test_request__POST http
_test_request__stream_body http
}
end
def _test_request__GET(http)
req = Net::HTTP::Get.new('/')
http.request(req) {|res|
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_equal $test_net_http_data.size, res.body.size
assert_equal $test_net_http_data, res.body
}
end
def _test_request__file(http)
req = Net::HTTP::Get.new('/')
http.request(req) {|res|
assert_kind_of Net::HTTPResponse, res
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
f = StringIO.new("".force_encoding("ASCII-8BIT"))
res.read_body f
assert_equal $test_net_http_data.bytesize, f.string.bytesize
assert_equal $test_net_http_data.encoding, f.string.encoding
assert_equal $test_net_http_data, f.string
}
end
def _test_request__range(http)
req = Net::HTTP::Get.new('/')
req['range'] = 'bytes=0-5'
assert_equal $test_net_http_data[0,6], http.request(req).body
end
def _test_request__HEAD(http)
req = Net::HTTP::Head.new('/')
http.request(req) {|res|
assert_kind_of Net::HTTPResponse, res
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_nil res.body
}
end
def _test_request__POST(http)
data = 'post data'
req = Net::HTTP::Post.new('/')
req['Accept'] = $test_net_http_data_type
http.request(req, data) {|res|
assert_kind_of Net::HTTPResponse, res
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_equal data.size, res['content-length'].to_i
end
assert_kind_of String, res.body
assert_equal data, res.body
}
end
def _test_request__stream_body(http)
req = Net::HTTP::Post.new('/')
data = $test_net_http_data
req.content_length = data.size
req.body_stream = StringIO.new(data)
res = http.request(req)
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
assert_equal data.size, res.body.size
assert_equal data, res.body
end
def test_send_request
start {|http|
_test_send_request__GET http
_test_send_request__POST http
}
end
def _test_send_request__GET(http)
res = http.send_request('GET', '/')
assert_kind_of Net::HTTPResponse, res
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_kind_of String, res.body
assert_equal $test_net_http_data, res.body
end
def _test_send_request__POST(http)
data = 'aaabbb cc ddddddddddd lkjoiu4j3qlkuoa'
res = http.send_request('POST', '/', data)
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
assert_equal data.size, res.body.size
assert_equal data, res.body
end
def test_set_form
require 'tempfile'
file = Tempfile.new('ruby-test')
file << "\u{30c7}\u{30fc}\u{30bf}"
data = [
['name', 'Gonbei Nanashi'],
['name', "\u{540d}\u{7121}\u{3057}\u{306e}\u{6a29}\u{5175}\u{885b}"],
['s"i\o', StringIO.new("\u{3042 3044 4e9c 925b}")],
["file", file, filename: "ruby-test"]
]
expected = <<"__EOM__".gsub(/\n/, "\r\n")
--<boundary>
Content-Disposition: form-data; name="name"
Gonbei Nanashi
--<boundary>
Content-Disposition: form-data; name="name"
\xE5\x90\x8D\xE7\x84\xA1\xE3\x81\x97\xE3\x81\xAE\xE6\xA8\xA9\xE5\x85\xB5\xE8\xA1\x9B
--<boundary>
Content-Disposition: form-data; name="s\\"i\\\\o"
\xE3\x81\x82\xE3\x81\x84\xE4\xBA\x9C\xE9\x89\x9B
--<boundary>
Content-Disposition: form-data; name="file"; filename="ruby-test"
Content-Type: application/octet-stream
\xE3\x83\x87\xE3\x83\xBC\xE3\x82\xBF
--<boundary>--
__EOM__
start {|http|
_test_set_form_urlencoded(http, data.reject{|k,v|!v.is_a?(String)})
_test_set_form_multipart(http, false, data, expected)
_test_set_form_multipart(http, true, data, expected)
}
end
def _test_set_form_urlencoded(http, data)
req = Net::HTTP::Post.new('/')
req.set_form(data)
res = http.request req
assert_equal "name=Gonbei+Nanashi&name=%E5%90%8D%E7%84%A1%E3%81%97%E3%81%AE%E6%A8%A9%E5%85%B5%E8%A1%9B", res.body
end
def _test_set_form_multipart(http, chunked_p, data, expected)
data.each{|k,v|v.rewind rescue nil}
req = Net::HTTP::Post.new('/')
req.set_form(data, 'multipart/form-data')
req['Transfer-Encoding'] = 'chunked' if chunked_p
res = http.request req
body = res.body
assert_match(/\A--(?<boundary>\S+)/, body)
/\A--(?<boundary>\S+)/ =~ body
expected = expected.gsub(/<boundary>/, boundary)
assert_equal(expected, body)
end
def test_set_form_with_file
require 'tempfile'
file = Tempfile.new('ruby-test')
file << $test_net_http_data
filename = File.basename(file.to_path)
data = [['file', file]]
expected = <<"__EOM__".gsub(/\n/, "\r\n")
--<boundary>
Content-Disposition: form-data; name="file"; filename="<filename>"
Content-Type: application/octet-stream
<data>
--<boundary>--
__EOM__
expected.sub!(/<filename>/, filename)
expected.sub!(/<data>/, $test_net_http_data)
start {|http|
data.each{|k,v|v.rewind rescue nil}
req = Net::HTTP::Post.new('/')
req.set_form(data, 'multipart/form-data')
res = http.request req
body = res.body
header, _ = body.split(/\r\n\r\n/, 2)
assert_match(/\A--(?<boundary>\S+)/, body)
/\A--(?<boundary>\S+)/ =~ body
expected = expected.gsub(/<boundary>/, boundary)
assert_match(/^--(?<boundary>\S+)\r\n/, header)
assert_match(
/^Content-Disposition: form-data; name="file"; filename="#{filename}"\r\n/,
header)
assert_equal(expected, body)
data.each{|k,v|v.rewind rescue nil}
req['Transfer-Encoding'] = 'chunked'
res = http.request req
#assert_equal(expected, res.body)
}
end
end
class TestNetHTTP_v1_2 < Test::Unit::TestCase
CONFIG = {
'host' => '127.0.0.1',
'port' => 10081,
'proxy_host' => nil,
'proxy_port' => nil,
}
include TestNetHTTPUtils
include TestNetHTTP_version_1_1_methods
include TestNetHTTP_version_1_2_methods
def new
Net::HTTP.version_1_2
super
end
end
class TestNetHTTP_v1_2_chunked < Test::Unit::TestCase
CONFIG = {
'host' => '127.0.0.1',
'port' => 10081,
'proxy_host' => nil,
'proxy_port' => nil,
'chunked' => true,
}
include TestNetHTTPUtils
include TestNetHTTP_version_1_1_methods
include TestNetHTTP_version_1_2_methods
def new
Net::HTTP.version_1_2
super
end
def test_chunked_break
i = 0
assert_nothing_raised("[ruby-core:29229]") {
start {|http|
http.request_get('/') {|res|
res.read_body {|chunk|
break
}
}
}
}
end
end
|