httpclient 2.7.2 → 2.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/bin/httpclient +2 -0
- data/lib/hexdump.rb +12 -12
- data/lib/httpclient/http.rb +12 -9
- data/lib/httpclient/jruby_ssl_socket.rb +119 -52
- data/lib/httpclient/session.rb +37 -18
- data/lib/httpclient/ssl_config.rb +90 -122
- data/lib/httpclient/ssl_socket.rb +26 -24
- data/lib/httpclient/util.rb +1 -1
- data/lib/httpclient/version.rb +1 -1
- data/lib/httpclient/webagent-cookie.rb +1 -1
- data/lib/httpclient.rb +19 -10
- data/lib/jsonclient.rb +8 -5
- data/lib/oauthclient.rb +1 -0
- data/sample/auth.rb +1 -1
- data/sample/generate_test_keys.rb +99 -0
- data/test/ca-chain.pem +32 -36
- data/test/ca.cert +16 -19
- data/test/ca.key +27 -0
- data/test/ca.srl +1 -0
- data/test/client-pass.key +30 -18
- data/test/client.cert +17 -16
- data/test/client.key +25 -13
- data/test/fixtures/verify.alt.cert +20 -0
- data/test/fixtures/verify.foo.cert +20 -0
- data/test/fixtures/verify.key +27 -0
- data/test/fixtures/verify.localhost.cert +20 -0
- data/test/helper.rb +5 -7
- data/test/jruby_ssl_socket/test_pemutils.rb +32 -0
- data/test/server.cert +16 -15
- data/test/server.key +25 -13
- data/test/subca.cert +16 -17
- data/test/subca.key +27 -0
- data/test/subca.srl +1 -0
- data/test/test_auth.rb +21 -18
- data/test/test_hexdump.rb +1 -2
- data/test/test_http-access2.rb +33 -23
- data/test/test_httpclient.rb +133 -58
- data/test/test_jsonclient.rb +18 -0
- data/test/test_ssl.rb +205 -23
- metadata +32 -9
data/test/test_httpclient.rb
CHANGED
@@ -20,7 +20,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
20
20
|
def test_initialize
|
21
21
|
setup_proxyserver
|
22
22
|
escape_noproxy do
|
23
|
-
@proxyio.
|
23
|
+
@proxyio.truncate(0)
|
24
24
|
@client = HTTPClient.new(proxyurl)
|
25
25
|
assert_equal(urify(proxyurl), @client.proxy)
|
26
26
|
assert_equal(200, @client.head(serverurl).status)
|
@@ -30,7 +30,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
30
30
|
|
31
31
|
def test_agent_name
|
32
32
|
@client = HTTPClient.new(nil, "agent_name_foo")
|
33
|
-
str = ""
|
33
|
+
str = "".dup
|
34
34
|
@client.debug_dev = str
|
35
35
|
@client.get(serverurl)
|
36
36
|
lines = str.split(/(?:\r?\n)+/)
|
@@ -40,7 +40,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
40
40
|
|
41
41
|
def test_from
|
42
42
|
@client = HTTPClient.new(nil, nil, "from_bar")
|
43
|
-
str = ""
|
43
|
+
str = "".dup
|
44
44
|
@client.debug_dev = str
|
45
45
|
@client.get(serverurl)
|
46
46
|
lines = str.split(/(?:\r?\n)+/)
|
@@ -49,7 +49,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def test_debug_dev
|
52
|
-
str = ""
|
52
|
+
str = "".dup
|
53
53
|
@client.debug_dev = str
|
54
54
|
assert_equal(str.object_id, @client.debug_dev.object_id)
|
55
55
|
assert(str.empty?)
|
@@ -58,7 +58,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def test_debug_dev_stream
|
61
|
-
str = ""
|
61
|
+
str = "".dup
|
62
62
|
@client.debug_dev = str
|
63
63
|
conn = @client.get_async(serverurl)
|
64
64
|
Thread.pass while !conn.finished?
|
@@ -67,7 +67,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
67
67
|
|
68
68
|
def test_protocol_version_http09
|
69
69
|
@client.protocol_version = 'HTTP/0.9'
|
70
|
-
@client.debug_dev = str = ''
|
70
|
+
@client.debug_dev = str = ''.dup
|
71
71
|
@client.test_loopback_http_response << "hello\nworld\n"
|
72
72
|
res = @client.get(serverurl + 'hello')
|
73
73
|
assert_equal('0.9', res.http_version)
|
@@ -88,7 +88,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
88
88
|
assert_equal(nil, @client.protocol_version)
|
89
89
|
@client.protocol_version = 'HTTP/1.0'
|
90
90
|
assert_equal('HTTP/1.0', @client.protocol_version)
|
91
|
-
str = ""
|
91
|
+
str = "".dup
|
92
92
|
@client.debug_dev = str
|
93
93
|
@client.get(serverurl + 'hello')
|
94
94
|
lines = str.split(/(?:\r?\n)+/)
|
@@ -100,7 +100,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
100
100
|
end
|
101
101
|
|
102
102
|
def test_header_accept_by_default
|
103
|
-
str = ""
|
103
|
+
str = "".dup
|
104
104
|
@client.debug_dev = str
|
105
105
|
@client.get(serverurl)
|
106
106
|
lines = str.split(/(?:\r?\n)+/)
|
@@ -108,7 +108,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
108
108
|
end
|
109
109
|
|
110
110
|
def test_header_accept
|
111
|
-
str = ""
|
111
|
+
str = "".dup
|
112
112
|
@client.debug_dev = str
|
113
113
|
@client.get(serverurl, :header => {:Accept => 'text/html'})
|
114
114
|
lines = str.split(/(?:\r?\n)+/)
|
@@ -116,7 +116,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
116
116
|
end
|
117
117
|
|
118
118
|
def test_header_symbol
|
119
|
-
str = ""
|
119
|
+
str = "".dup
|
120
120
|
@client.debug_dev = str
|
121
121
|
@client.post(serverurl + 'servlet', :header => {:'Content-Type' => 'application/json'}, :body => 'hello')
|
122
122
|
lines = str.split(/(?:\r?\n)+/).grep(/^Content-Type/)
|
@@ -124,7 +124,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
124
124
|
end
|
125
125
|
|
126
126
|
def test_host_given
|
127
|
-
str = ""
|
127
|
+
str = "".dup
|
128
128
|
@client.debug_dev = str
|
129
129
|
@client.get(serverurl)
|
130
130
|
lines = str.split(/(?:\r?\n)+/)
|
@@ -134,7 +134,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
134
134
|
assert_equal("Host: localhost:#{serverport}", lines[7])
|
135
135
|
#
|
136
136
|
@client.reset_all
|
137
|
-
str = ""
|
137
|
+
str = "".dup
|
138
138
|
@client.debug_dev = str
|
139
139
|
@client.get(serverurl, nil, {'Host' => 'foo'})
|
140
140
|
lines = str.split(/(?:\r?\n)+/)
|
@@ -166,7 +166,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
166
166
|
|
167
167
|
def test_protocol_version_http11
|
168
168
|
assert_equal(nil, @client.protocol_version)
|
169
|
-
str = ""
|
169
|
+
str = "".dup
|
170
170
|
@client.debug_dev = str
|
171
171
|
@client.get(serverurl)
|
172
172
|
lines = str.split(/(?:\r?\n)+/)
|
@@ -176,7 +176,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
176
176
|
assert_equal("Host: localhost:#{serverport}", lines[7])
|
177
177
|
@client.protocol_version = 'HTTP/1.1'
|
178
178
|
assert_equal('HTTP/1.1', @client.protocol_version)
|
179
|
-
str = ""
|
179
|
+
str = "".dup
|
180
180
|
@client.debug_dev = str
|
181
181
|
@client.get(serverurl)
|
182
182
|
lines = str.split(/(?:\r?\n)+/)
|
@@ -184,7 +184,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
184
184
|
assert_equal("! CONNECTION ESTABLISHED", lines[2])
|
185
185
|
assert_equal("GET / HTTP/1.1", lines[3])
|
186
186
|
@client.protocol_version = 'HTTP/1.0'
|
187
|
-
str = ""
|
187
|
+
str = "".dup
|
188
188
|
@client.debug_dev = str
|
189
189
|
@client.get(serverurl)
|
190
190
|
lines = str.split(/(?:\r?\n)+/)
|
@@ -209,14 +209,14 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
209
209
|
@client.proxy = uri
|
210
210
|
assert_equal(uri, @client.proxy)
|
211
211
|
#
|
212
|
-
@proxyio.
|
212
|
+
@proxyio.truncate(0)
|
213
213
|
@client.proxy = nil
|
214
214
|
assert_equal(200, @client.head(serverurl).status)
|
215
215
|
assert(/accept/ !~ @proxyio.string)
|
216
216
|
#
|
217
|
-
@proxyio.
|
217
|
+
@proxyio.truncate(0)
|
218
218
|
@client.proxy = proxyurl
|
219
|
-
@client.debug_dev = str = ""
|
219
|
+
@client.debug_dev = str = "".dup
|
220
220
|
assert_equal(200, @client.head(serverurl).status)
|
221
221
|
assert(/accept/ =~ @proxyio.string)
|
222
222
|
assert(/Host: localhost:#{serverport}/ =~ str)
|
@@ -225,13 +225,13 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
225
225
|
|
226
226
|
def test_host_header
|
227
227
|
@client.proxy = proxyurl
|
228
|
-
@client.debug_dev = str = ""
|
228
|
+
@client.debug_dev = str = "".dup
|
229
229
|
@client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"
|
230
230
|
assert_equal(200, @client.head('http://www.example.com/foo').status)
|
231
231
|
# ensure no ':80' is added. some servers dislike that.
|
232
232
|
assert(/\r\nHost: www\.example\.com\r\n/ =~ str)
|
233
233
|
#
|
234
|
-
@client.debug_dev = str = ""
|
234
|
+
@client.debug_dev = str = "".dup
|
235
235
|
@client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"
|
236
236
|
assert_equal(200, @client.head('http://www.example.com:12345/foo').status)
|
237
237
|
# ensure ':12345' exists.
|
@@ -273,7 +273,7 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
273
273
|
end
|
274
274
|
|
275
275
|
def test_noproxy_for_localhost
|
276
|
-
@proxyio.
|
276
|
+
@proxyio.truncate(0)
|
277
277
|
@client.proxy = proxyurl
|
278
278
|
assert_equal(200, @client.head(serverurl).status)
|
279
279
|
assert(/accept/ !~ @proxyio.string)
|
@@ -286,36 +286,36 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
286
286
|
assert_equal(nil, @client.no_proxy)
|
287
287
|
@client.no_proxy = 'localhost'
|
288
288
|
assert_equal('localhost', @client.no_proxy)
|
289
|
-
@proxyio.
|
289
|
+
@proxyio.truncate(0)
|
290
290
|
@client.proxy = nil
|
291
291
|
assert_equal(200, @client.head(serverurl).status)
|
292
292
|
assert(/accept/ !~ @proxyio.string)
|
293
293
|
#
|
294
|
-
@proxyio.
|
294
|
+
@proxyio.truncate(0)
|
295
295
|
@client.proxy = proxyurl
|
296
296
|
assert_equal(200, @client.head(serverurl).status)
|
297
297
|
assert(/accept/ !~ @proxyio.string)
|
298
298
|
#
|
299
299
|
@client.no_proxy = 'foobar'
|
300
|
-
@proxyio.
|
300
|
+
@proxyio.truncate(0)
|
301
301
|
@client.proxy = proxyurl
|
302
302
|
assert_equal(200, @client.head(serverurl).status)
|
303
303
|
assert(/accept/ =~ @proxyio.string)
|
304
304
|
#
|
305
305
|
@client.no_proxy = 'foobar,localhost:baz'
|
306
|
-
@proxyio.
|
306
|
+
@proxyio.truncate(0)
|
307
307
|
@client.proxy = proxyurl
|
308
308
|
assert_equal(200, @client.head(serverurl).status)
|
309
309
|
assert(/accept/ !~ @proxyio.string)
|
310
310
|
#
|
311
311
|
@client.no_proxy = 'foobar,localhost:443'
|
312
|
-
@proxyio.
|
312
|
+
@proxyio.truncate(0)
|
313
313
|
@client.proxy = proxyurl
|
314
314
|
assert_equal(200, @client.head(serverurl).status)
|
315
315
|
assert(/accept/ =~ @proxyio.string)
|
316
316
|
#
|
317
317
|
@client.no_proxy = "foobar,localhost:443:localhost:#{serverport},baz"
|
318
|
-
@proxyio.
|
318
|
+
@proxyio.truncate(0)
|
319
319
|
@client.proxy = proxyurl
|
320
320
|
assert_equal(200, @client.head(serverurl).status)
|
321
321
|
assert(/accept/ !~ @proxyio.string)
|
@@ -323,28 +323,28 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
323
323
|
end
|
324
324
|
|
325
325
|
def test_no_proxy_with_initial_dot
|
326
|
-
@client.debug_dev = str = ""
|
326
|
+
@client.debug_dev = str = "".dup
|
327
327
|
@client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"
|
328
328
|
@client.no_proxy = ''
|
329
329
|
@client.proxy = proxyurl
|
330
330
|
@client.head('http://www.foo.com')
|
331
331
|
assert(/CONNECT TO localhost/ =~ str, 'via proxy')
|
332
332
|
#
|
333
|
-
@client.debug_dev = str = ""
|
333
|
+
@client.debug_dev = str = "".dup
|
334
334
|
@client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"
|
335
335
|
@client.no_proxy = '.foo.com'
|
336
336
|
@client.proxy = proxyurl
|
337
337
|
@client.head('http://www.foo.com')
|
338
338
|
assert(/CONNECT TO www.foo.com/ =~ str, 'no proxy because .foo.com matches with www.foo.com')
|
339
339
|
#
|
340
|
-
@client.debug_dev = str = ""
|
340
|
+
@client.debug_dev = str = "".dup
|
341
341
|
@client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"
|
342
342
|
@client.no_proxy = '.foo.com'
|
343
343
|
@client.proxy = proxyurl
|
344
344
|
@client.head('http://foo.com')
|
345
345
|
assert(/CONNECT TO localhost/ =~ str, 'via proxy because .foo.com does not matche with foo.com')
|
346
346
|
#
|
347
|
-
@client.debug_dev = str = ""
|
347
|
+
@client.debug_dev = str = "".dup
|
348
348
|
@client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"
|
349
349
|
@client.no_proxy = 'foo.com'
|
350
350
|
@client.proxy = proxyurl
|
@@ -370,7 +370,7 @@ Connection: close\r
|
|
370
370
|
\r
|
371
371
|
hello
|
372
372
|
EOS
|
373
|
-
@client.debug_dev = str = ''
|
373
|
+
@client.debug_dev = str = ''.dup
|
374
374
|
@client.set_auth("http://www.example.org/baz/", 'admin', 'admin')
|
375
375
|
assert_equal('hello', @client.get('http://www.example.org/baz/foo').content)
|
376
376
|
assert_match(/^Cookie: foo=bar/, str)
|
@@ -411,7 +411,7 @@ EOS
|
|
411
411
|
assert_equal('message body 1', @client.get_content('http://somewhere'))
|
412
412
|
assert_equal('message body 2', @client.get_content('http://somewhere'))
|
413
413
|
#
|
414
|
-
@client.debug_dev = str = ''
|
414
|
+
@client.debug_dev = str = ''.dup
|
415
415
|
@client.test_loopback_response << 'message body 3'
|
416
416
|
assert_equal('message body 3', @client.get_content('http://somewhere'))
|
417
417
|
assert_match(/message body 3/, str)
|
@@ -597,22 +597,22 @@ EOS
|
|
597
597
|
assert(called)
|
598
598
|
end
|
599
599
|
|
600
|
-
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"
|
601
|
-
DEFLATE_CONTENT = "\x78\x9c\xcb\x48\xcd\xc9\xc9\x07\x00\x06\x2c\x02\x15"
|
602
|
-
DEFLATE_NOHEADER_CONTENT = "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15"
|
603
|
-
|
604
|
-
content.force_encoding('BINARY') if content.respond_to?(:force_encoding)
|
605
|
-
end
|
600
|
+
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".b
|
601
|
+
DEFLATE_CONTENT = "\x78\x9c\xcb\x48\xcd\xc9\xc9\x07\x00\x06\x2c\x02\x15".b
|
602
|
+
DEFLATE_NOHEADER_CONTENT = "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15".b
|
603
|
+
|
606
604
|
def test_get_gzipped_content
|
607
605
|
@client.transparent_gzip_decompression = false
|
608
606
|
content = @client.get_content(serverurl + 'compressed?enc=gzip')
|
609
607
|
assert_not_equal('hello', content)
|
610
608
|
assert_equal(GZIP_CONTENT, content)
|
611
609
|
@client.transparent_gzip_decompression = true
|
610
|
+
@client.reset_all
|
612
611
|
assert_equal('hello', @client.get_content(serverurl + 'compressed?enc=gzip'))
|
613
612
|
assert_equal('hello', @client.get_content(serverurl + 'compressed?enc=deflate'))
|
614
613
|
assert_equal('hello', @client.get_content(serverurl + 'compressed?enc=deflate_noheader'))
|
615
614
|
@client.transparent_gzip_decompression = false
|
615
|
+
@client.reset_all
|
616
616
|
end
|
617
617
|
|
618
618
|
def test_get_content_with_block
|
@@ -765,6 +765,22 @@ EOS
|
|
765
765
|
assert_equal(1000*1000, res.content.read.length)
|
766
766
|
end
|
767
767
|
|
768
|
+
if RUBY_VERSION > "1.9"
|
769
|
+
def test_post_async_with_default_internal
|
770
|
+
original_encoding = Encoding.default_internal
|
771
|
+
Encoding.default_internal = Encoding::UTF_8
|
772
|
+
begin
|
773
|
+
post_body = StringIO.new("こんにちは")
|
774
|
+
conn = @client.post_async(serverurl + 'servlet', post_body)
|
775
|
+
Thread.pass while !conn.finished?
|
776
|
+
res = conn.pop
|
777
|
+
assert_equal 'post,こんにちは', res.content.read
|
778
|
+
ensure
|
779
|
+
Encoding.default_internal = original_encoding
|
780
|
+
end
|
781
|
+
end
|
782
|
+
end
|
783
|
+
|
768
784
|
def test_get_with_block
|
769
785
|
called = false
|
770
786
|
res = @client.get(serverurl + 'servlet') { |str|
|
@@ -788,6 +804,29 @@ EOS
|
|
788
804
|
assert_nil(res.content)
|
789
805
|
end
|
790
806
|
|
807
|
+
def test_get_with_block_and_redirects
|
808
|
+
called = false
|
809
|
+
res = @client.get(serverurl + 'servlet', :follow_redirect => true) { |str|
|
810
|
+
assert_equal('get', str)
|
811
|
+
called = true
|
812
|
+
}
|
813
|
+
assert(called)
|
814
|
+
# res does not have a content
|
815
|
+
assert_nil(res.content)
|
816
|
+
end
|
817
|
+
|
818
|
+
def test_get_with_block_arity_2_and_redirects
|
819
|
+
called = false
|
820
|
+
res = @client.get(serverurl + 'servlet', :follow_redirect => true) { |blk_res, str|
|
821
|
+
assert_equal(200, blk_res.status)
|
822
|
+
assert_equal('get', str)
|
823
|
+
called = true
|
824
|
+
}
|
825
|
+
assert(called)
|
826
|
+
# res does not have a content
|
827
|
+
assert_nil(res.content)
|
828
|
+
end
|
829
|
+
|
791
830
|
def test_get_with_block_string_recycle
|
792
831
|
@client.read_block_size = 2
|
793
832
|
body = []
|
@@ -826,7 +865,7 @@ EOS
|
|
826
865
|
end
|
827
866
|
|
828
867
|
def test_post_empty
|
829
|
-
@client.debug_dev = str = ''
|
868
|
+
@client.debug_dev = str = ''.dup
|
830
869
|
# nil body means 'no content' that is allowed but WEBrick cannot handle it.
|
831
870
|
@client.post(serverurl + 'servlet', :body => nil)
|
832
871
|
# request does not have 'Content-Type'
|
@@ -1032,6 +1071,10 @@ EOS
|
|
1032
1071
|
def test_post_with_custom_multipart_and_file
|
1033
1072
|
STDOUT.sync = true
|
1034
1073
|
File.open(__FILE__) do |file|
|
1074
|
+
def file.original_filename
|
1075
|
+
'file.txt'
|
1076
|
+
end
|
1077
|
+
|
1035
1078
|
ext = { 'Content-Type' => 'multipart/alternative' }
|
1036
1079
|
body = [{ 'Content-Type' => 'text/plain', :content => "this is only a test" },
|
1037
1080
|
{ 'Content-Type' => 'application/x-ruby', :content => file }]
|
@@ -1039,6 +1082,7 @@ EOS
|
|
1039
1082
|
assert_match(/^Content-Type: text\/plain\r\n/m, res.content)
|
1040
1083
|
assert_match(/^this is only a test\r\n/m, res.content)
|
1041
1084
|
assert_match(/^Content-Type: application\/x-ruby\r\n/m, res.content)
|
1085
|
+
assert_match(/Content-Disposition: form-data; name="3"; filename="file.txt"/, res.content)
|
1042
1086
|
assert_match(/FIND_TAG_IN_THIS_FILE/, res.content)
|
1043
1087
|
end
|
1044
1088
|
end
|
@@ -1046,7 +1090,7 @@ EOS
|
|
1046
1090
|
def test_patch
|
1047
1091
|
assert_equal("patch", @client.patch(serverurl + 'servlet', '').content)
|
1048
1092
|
param = {'1'=>'2', '3'=>'4'}
|
1049
|
-
@client.debug_dev = str = ''
|
1093
|
+
@client.debug_dev = str = ''.dup
|
1050
1094
|
res = @client.patch(serverurl + 'servlet', param)
|
1051
1095
|
assert_equal(param, params(res.header["x-query"][0]))
|
1052
1096
|
assert_equal('Content-Type: application/x-www-form-urlencoded', str.split(/\r?\n/)[5])
|
@@ -1076,7 +1120,7 @@ EOS
|
|
1076
1120
|
def test_put
|
1077
1121
|
assert_equal("put", @client.put(serverurl + 'servlet', '').content)
|
1078
1122
|
param = {'1'=>'2', '3'=>'4'}
|
1079
|
-
@client.debug_dev = str = ''
|
1123
|
+
@client.debug_dev = str = ''.dup
|
1080
1124
|
res = @client.put(serverurl + 'servlet', param)
|
1081
1125
|
assert_equal(param, params(res.header["x-query"][0]))
|
1082
1126
|
assert_equal('Content-Type: application/x-www-form-urlencoded', str.split(/\r?\n/)[5])
|
@@ -1123,7 +1167,7 @@ EOS
|
|
1123
1167
|
# Not prohibited by spec, but normally it's ignored
|
1124
1168
|
def test_delete_with_body
|
1125
1169
|
param = {'1'=>'2', '3'=>'4'}
|
1126
|
-
@client.debug_dev = str = ''
|
1170
|
+
@client.debug_dev = str = ''.dup
|
1127
1171
|
assert_equal("delete", @client.delete(serverurl + 'servlet', param).content)
|
1128
1172
|
assert_equal({'1' => ['2'], '3' => ['4']}, HTTP::Message.parse(str.split(/\r?\n\r?\n/)[2]))
|
1129
1173
|
end
|
@@ -1263,14 +1307,14 @@ EOS
|
|
1263
1307
|
end
|
1264
1308
|
|
1265
1309
|
def test_extra_headers
|
1266
|
-
str = ""
|
1310
|
+
str = "".dup
|
1267
1311
|
@client.debug_dev = str
|
1268
1312
|
@client.head(serverurl, nil, {"ABC" => "DEF"})
|
1269
1313
|
lines = str.split(/(?:\r?\n)+/)
|
1270
1314
|
assert_equal("= Request", lines[0])
|
1271
1315
|
assert_match("ABC: DEF", lines[4])
|
1272
1316
|
#
|
1273
|
-
str = ""
|
1317
|
+
str = "".dup
|
1274
1318
|
@client.debug_dev = str
|
1275
1319
|
@client.get(serverurl, nil, [["ABC", "DEF"], ["ABC", "DEF"]])
|
1276
1320
|
lines = str.split(/(?:\r?\n)+/)
|
@@ -1280,23 +1324,24 @@ EOS
|
|
1280
1324
|
end
|
1281
1325
|
|
1282
1326
|
def test_http_custom_date_header
|
1283
|
-
@client.debug_dev = (str = "")
|
1327
|
+
@client.debug_dev = (str = "".dup)
|
1284
1328
|
_res = @client.get(serverurl + 'hello', :header => {'Date' => 'foo'})
|
1285
1329
|
lines = str.split(/(?:\r?\n)+/)
|
1286
1330
|
assert_equal('Date: foo', lines[4])
|
1287
1331
|
end
|
1288
1332
|
|
1289
1333
|
def test_timeout
|
1290
|
-
|
1291
|
-
assert_equal(
|
1292
|
-
assert_equal(
|
1334
|
+
client = HTTPClient.new
|
1335
|
+
assert_equal(60, client.connect_timeout)
|
1336
|
+
assert_equal(120, client.send_timeout)
|
1337
|
+
assert_equal(60, client.receive_timeout)
|
1293
1338
|
#
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
assert_equal(1,
|
1298
|
-
assert_equal(2,
|
1299
|
-
assert_equal(3,
|
1339
|
+
client.connect_timeout = 1
|
1340
|
+
client.send_timeout = 2
|
1341
|
+
client.receive_timeout = 3
|
1342
|
+
assert_equal(1, client.connect_timeout)
|
1343
|
+
assert_equal(2, client.send_timeout)
|
1344
|
+
assert_equal(3, client.receive_timeout)
|
1300
1345
|
end
|
1301
1346
|
|
1302
1347
|
def test_connect_timeout
|
@@ -1311,11 +1356,13 @@ EOS
|
|
1311
1356
|
# this test takes 2 sec
|
1312
1357
|
assert_equal('hello?sec=2', @client.get_content(serverurl + 'sleep?sec=2'))
|
1313
1358
|
@client.receive_timeout = 1
|
1359
|
+
@client.reset_all
|
1314
1360
|
assert_equal('hello?sec=0', @client.get_content(serverurl + 'sleep?sec=0'))
|
1315
1361
|
assert_raise(HTTPClient::ReceiveTimeoutError) do
|
1316
1362
|
@client.get_content(serverurl + 'sleep?sec=2')
|
1317
1363
|
end
|
1318
1364
|
@client.receive_timeout = 3
|
1365
|
+
@client.reset_all
|
1319
1366
|
assert_equal('hello?sec=2', @client.get_content(serverurl + 'sleep?sec=2'))
|
1320
1367
|
end
|
1321
1368
|
|
@@ -1323,11 +1370,13 @@ EOS
|
|
1323
1370
|
# this test takes 2 sec
|
1324
1371
|
assert_equal('hello', @client.post(serverurl + 'sleep', :sec => 2).content)
|
1325
1372
|
@client.receive_timeout = 1
|
1373
|
+
@client.reset_all
|
1326
1374
|
assert_equal('hello', @client.post(serverurl + 'sleep', :sec => 0).content)
|
1327
1375
|
assert_raise(HTTPClient::ReceiveTimeoutError) do
|
1328
1376
|
@client.post(serverurl + 'sleep', :sec => 2)
|
1329
1377
|
end
|
1330
1378
|
@client.receive_timeout = 3
|
1379
|
+
@client.reset_all
|
1331
1380
|
assert_equal('hello', @client.post(serverurl + 'sleep', :sec => 2).content)
|
1332
1381
|
end
|
1333
1382
|
|
@@ -1368,7 +1417,7 @@ EOS
|
|
1368
1417
|
def test_eof_error_length
|
1369
1418
|
io = StringIO.new('')
|
1370
1419
|
def io.gets(*arg)
|
1371
|
-
@buf ||= ["HTTP/1.0 200 OK\n", "content-length: 123\n", "\n"]
|
1420
|
+
@buf ||= ["HTTP/1.0 200 OK\n".dup, "content-length: 123\n".dup, "\n".dup]
|
1372
1421
|
@buf.shift
|
1373
1422
|
end
|
1374
1423
|
def io.readpartial(size, buf)
|
@@ -1396,7 +1445,7 @@ EOS
|
|
1396
1445
|
def test_eof_error_rest
|
1397
1446
|
io = StringIO.new('')
|
1398
1447
|
def io.gets(*arg)
|
1399
|
-
@buf ||= ["HTTP/1.0 200 OK\n", "\n"]
|
1448
|
+
@buf ||= ["HTTP/1.0 200 OK\n".dup, "\n".dup]
|
1400
1449
|
@buf.shift
|
1401
1450
|
end
|
1402
1451
|
def io.readpartial(size, buf)
|
@@ -1486,6 +1535,7 @@ EOS
|
|
1486
1535
|
assert_equal('text/plain', HTTP::Message.mime_type('foo.txt'))
|
1487
1536
|
assert_equal('text/html', HTTP::Message.mime_type('foo.html'))
|
1488
1537
|
assert_equal('text/html', HTTP::Message.mime_type('foo.htm'))
|
1538
|
+
assert_equal('text/xml', HTTP::Message.mime_type('foo.xml'))
|
1489
1539
|
assert_equal('application/msword', HTTP::Message.mime_type('foo.doc'))
|
1490
1540
|
assert_equal('image/png', HTTP::Message.mime_type('foo.png'))
|
1491
1541
|
assert_equal('image/gif', HTTP::Message.mime_type('foo.gif'))
|
@@ -1779,6 +1829,19 @@ EOS
|
|
1779
1829
|
end
|
1780
1830
|
end
|
1781
1831
|
|
1832
|
+
def test_strict_response_size_check
|
1833
|
+
@client.strict_response_size_check = false
|
1834
|
+
@client.test_loopback_http_response << "HTTP/1.0 200 OK\r\nContent-Length: 12345\r\n\r\nhello world"
|
1835
|
+
assert_equal('hello world', @client.get_content('http://dummy'))
|
1836
|
+
|
1837
|
+
@client.reset_all
|
1838
|
+
@client.strict_response_size_check = true
|
1839
|
+
@client.test_loopback_http_response << "HTTP/1.0 200 OK\r\nContent-Length: 12345\r\n\r\nhello world"
|
1840
|
+
assert_raise(HTTPClient::BadResponseError) do
|
1841
|
+
@client.get_content('http://dummy')
|
1842
|
+
end
|
1843
|
+
end
|
1844
|
+
|
1782
1845
|
def test_socket_local
|
1783
1846
|
@client.socket_local.host = '127.0.0.1'
|
1784
1847
|
assert_equal('hello', @client.get_content(serverurl + 'hello'))
|
@@ -1806,7 +1869,7 @@ EOS
|
|
1806
1869
|
|
1807
1870
|
if RUBY_VERSION >= "1.9.3"
|
1808
1871
|
def test_continue
|
1809
|
-
@client.debug_dev = str = ''
|
1872
|
+
@client.debug_dev = str = ''.dup
|
1810
1873
|
res = @client.get(serverurl + 'continue', :header => {:Expect => '100-continue'})
|
1811
1874
|
assert_equal(200, res.status)
|
1812
1875
|
assert_equal('done!', res.body)
|
@@ -1844,6 +1907,18 @@ EOS
|
|
1844
1907
|
end
|
1845
1908
|
end
|
1846
1909
|
|
1910
|
+
def test_tcp_keepalive
|
1911
|
+
@client.tcp_keepalive = true
|
1912
|
+
@client.get(serverurl)
|
1913
|
+
|
1914
|
+
# expecting HTTP keepalive caches the socket
|
1915
|
+
session = @client.instance_variable_get(:@session_manager).send(:get_cached_session, HTTPClient::Site.new(URI.parse(serverurl)))
|
1916
|
+
socket = session.instance_variable_get(:@socket)
|
1917
|
+
|
1918
|
+
assert_true(session.tcp_keepalive)
|
1919
|
+
assert_equal(Socket::SO_KEEPALIVE, socket.getsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE).optname)
|
1920
|
+
end
|
1921
|
+
|
1847
1922
|
private
|
1848
1923
|
|
1849
1924
|
def check_query_get(query)
|
data/test/test_jsonclient.rb
CHANGED
@@ -24,6 +24,12 @@ class TestJSONClient < Test::Unit::TestCase
|
|
24
24
|
assert_equal(1, JSON.parse(res.previous.content)['a'])
|
25
25
|
end
|
26
26
|
|
27
|
+
def test_post_with_array
|
28
|
+
res = @client.post(serverurl + 'json', [{'a' => 1, 'b' => {'c' => 2}}])
|
29
|
+
assert_equal(2, res.content[0]['b']['c'])
|
30
|
+
assert_equal('application/json; charset=utf-8', res.content_type)
|
31
|
+
end
|
32
|
+
|
27
33
|
def test_post_with_header
|
28
34
|
res = @client.post(serverurl + 'json', :header => {'X-foo' => 'bar'}, :body => {'a' => 1, 'b' => {'c' => 2}})
|
29
35
|
assert_equal(2, res.content['b']['c'])
|
@@ -54,6 +60,18 @@ class TestJSONClient < Test::Unit::TestCase
|
|
54
60
|
assert_equal('', res.content_type)
|
55
61
|
end
|
56
62
|
|
63
|
+
def test_hash_header_not_modified
|
64
|
+
header = {'X-foo' => 'bar'}
|
65
|
+
_res = @client.post(serverurl, :header => header, :body => {'a' => 1, 'b' => {'c' => 2}})
|
66
|
+
assert_equal({'X-foo' => 'bar'}, header)
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_array_header_not_modified
|
70
|
+
header = [['X-foo', 'bar']]
|
71
|
+
_res = @client.post(serverurl, :header => header, :body => {'a' => 1, 'b' => {'c' => 2}})
|
72
|
+
assert_equal([['X-foo', 'bar']], header)
|
73
|
+
end
|
74
|
+
|
57
75
|
class JSONServlet < WEBrick::HTTPServlet::AbstractServlet
|
58
76
|
def get_instance(*arg)
|
59
77
|
self
|