httpclient 2.8.2.4 → 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.
@@ -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.string = ""
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.string = ""
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.string = ""
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.string = ""
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.string = ""
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.string = ""
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.string = ""
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.string = ""
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.string = ""
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.string = ""
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,12 +597,10 @@ 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
- [GZIP_CONTENT, DEFLATE_CONTENT, DEFLATE_NOHEADER_CONTENT].each do |content|
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')
@@ -867,7 +865,7 @@ EOS
867
865
  end
868
866
 
869
867
  def test_post_empty
870
- @client.debug_dev = str = ''
868
+ @client.debug_dev = str = ''.dup
871
869
  # nil body means 'no content' that is allowed but WEBrick cannot handle it.
872
870
  @client.post(serverurl + 'servlet', :body => nil)
873
871
  # request does not have 'Content-Type'
@@ -1092,7 +1090,7 @@ EOS
1092
1090
  def test_patch
1093
1091
  assert_equal("patch", @client.patch(serverurl + 'servlet', '').content)
1094
1092
  param = {'1'=>'2', '3'=>'4'}
1095
- @client.debug_dev = str = ''
1093
+ @client.debug_dev = str = ''.dup
1096
1094
  res = @client.patch(serverurl + 'servlet', param)
1097
1095
  assert_equal(param, params(res.header["x-query"][0]))
1098
1096
  assert_equal('Content-Type: application/x-www-form-urlencoded', str.split(/\r?\n/)[5])
@@ -1122,7 +1120,7 @@ EOS
1122
1120
  def test_put
1123
1121
  assert_equal("put", @client.put(serverurl + 'servlet', '').content)
1124
1122
  param = {'1'=>'2', '3'=>'4'}
1125
- @client.debug_dev = str = ''
1123
+ @client.debug_dev = str = ''.dup
1126
1124
  res = @client.put(serverurl + 'servlet', param)
1127
1125
  assert_equal(param, params(res.header["x-query"][0]))
1128
1126
  assert_equal('Content-Type: application/x-www-form-urlencoded', str.split(/\r?\n/)[5])
@@ -1169,7 +1167,7 @@ EOS
1169
1167
  # Not prohibited by spec, but normally it's ignored
1170
1168
  def test_delete_with_body
1171
1169
  param = {'1'=>'2', '3'=>'4'}
1172
- @client.debug_dev = str = ''
1170
+ @client.debug_dev = str = ''.dup
1173
1171
  assert_equal("delete", @client.delete(serverurl + 'servlet', param).content)
1174
1172
  assert_equal({'1' => ['2'], '3' => ['4']}, HTTP::Message.parse(str.split(/\r?\n\r?\n/)[2]))
1175
1173
  end
@@ -1309,14 +1307,14 @@ EOS
1309
1307
  end
1310
1308
 
1311
1309
  def test_extra_headers
1312
- str = ""
1310
+ str = "".dup
1313
1311
  @client.debug_dev = str
1314
1312
  @client.head(serverurl, nil, {"ABC" => "DEF"})
1315
1313
  lines = str.split(/(?:\r?\n)+/)
1316
1314
  assert_equal("= Request", lines[0])
1317
1315
  assert_match("ABC: DEF", lines[4])
1318
1316
  #
1319
- str = ""
1317
+ str = "".dup
1320
1318
  @client.debug_dev = str
1321
1319
  @client.get(serverurl, nil, [["ABC", "DEF"], ["ABC", "DEF"]])
1322
1320
  lines = str.split(/(?:\r?\n)+/)
@@ -1326,23 +1324,24 @@ EOS
1326
1324
  end
1327
1325
 
1328
1326
  def test_http_custom_date_header
1329
- @client.debug_dev = (str = "")
1327
+ @client.debug_dev = (str = "".dup)
1330
1328
  _res = @client.get(serverurl + 'hello', :header => {'Date' => 'foo'})
1331
1329
  lines = str.split(/(?:\r?\n)+/)
1332
1330
  assert_equal('Date: foo', lines[4])
1333
1331
  end
1334
1332
 
1335
1333
  def test_timeout
1336
- assert_equal(60, @client.connect_timeout)
1337
- assert_equal(120, @client.send_timeout)
1338
- assert_equal(60, @client.receive_timeout)
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)
1339
1338
  #
1340
- @client.connect_timeout = 1
1341
- @client.send_timeout = 2
1342
- @client.receive_timeout = 3
1343
- assert_equal(1, @client.connect_timeout)
1344
- assert_equal(2, @client.send_timeout)
1345
- assert_equal(3, @client.receive_timeout)
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)
1346
1345
  end
1347
1346
 
1348
1347
  def test_connect_timeout
@@ -1418,7 +1417,7 @@ EOS
1418
1417
  def test_eof_error_length
1419
1418
  io = StringIO.new('')
1420
1419
  def io.gets(*arg)
1421
- @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]
1422
1421
  @buf.shift
1423
1422
  end
1424
1423
  def io.readpartial(size, buf)
@@ -1446,7 +1445,7 @@ EOS
1446
1445
  def test_eof_error_rest
1447
1446
  io = StringIO.new('')
1448
1447
  def io.gets(*arg)
1449
- @buf ||= ["HTTP/1.0 200 OK\n", "\n"]
1448
+ @buf ||= ["HTTP/1.0 200 OK\n".dup, "\n".dup]
1450
1449
  @buf.shift
1451
1450
  end
1452
1451
  def io.readpartial(size, buf)
@@ -1870,7 +1869,7 @@ EOS
1870
1869
 
1871
1870
  if RUBY_VERSION >= "1.9.3"
1872
1871
  def test_continue
1873
- @client.debug_dev = str = ''
1872
+ @client.debug_dev = str = ''.dup
1874
1873
  res = @client.get(serverurl + 'continue', :header => {:Expect => '100-continue'})
1875
1874
  assert_equal(200, res.status)
1876
1875
  assert_equal('done!', res.body)
@@ -1908,6 +1907,18 @@ EOS
1908
1907
  end
1909
1908
  end
1910
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
+
1911
1922
  private
1912
1923
 
1913
1924
  def check_query_get(query)
@@ -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