PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/net/http/test_httpresponse.rb

http://github.com/ruby/ruby
Ruby | 467 lines | 360 code | 104 blank | 3 comment | 6 complexity | f005daaeb85c042af444b4bc44c17e05 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # coding: US-ASCII
  2. # frozen_string_literal: false
  3. require 'net/http'
  4. require 'test/unit'
  5. require 'stringio'
  6. class HTTPResponseTest < Test::Unit::TestCase
  7. def test_singleline_header
  8. io = dummy_io(<<EOS)
  9. HTTP/1.1 200 OK
  10. Content-Length: 5
  11. Connection: close
  12. hello
  13. EOS
  14. res = Net::HTTPResponse.read_new(io)
  15. assert_equal('5', res['content-length'])
  16. assert_equal('close', res['connection'])
  17. end
  18. def test_multiline_header
  19. io = dummy_io(<<EOS)
  20. HTTP/1.1 200 OK
  21. X-Foo: XXX
  22. YYY
  23. X-Bar:
  24. XXX
  25. \tYYY
  26. hello
  27. EOS
  28. res = Net::HTTPResponse.read_new(io)
  29. assert_equal('XXX YYY', res['x-foo'])
  30. assert_equal('XXX YYY', res['x-bar'])
  31. end
  32. def test_read_body
  33. io = dummy_io(<<EOS)
  34. HTTP/1.1 200 OK
  35. Connection: close
  36. Content-Length: 5
  37. hello
  38. EOS
  39. res = Net::HTTPResponse.read_new(io)
  40. body = nil
  41. res.reading_body io, true do
  42. body = res.read_body
  43. end
  44. assert_equal 'hello', body
  45. end
  46. def test_read_body_block
  47. io = dummy_io(<<EOS)
  48. HTTP/1.1 200 OK
  49. Connection: close
  50. Content-Length: 5
  51. hello
  52. EOS
  53. res = Net::HTTPResponse.read_new(io)
  54. body = ''
  55. res.reading_body io, true do
  56. res.read_body do |chunk|
  57. body << chunk
  58. end
  59. end
  60. assert_equal 'hello', body
  61. end
  62. def test_read_body_block_mod
  63. IO.pipe do |r, w|
  64. buf = 'x' * 1024
  65. buf.freeze
  66. n = 1024
  67. len = n * buf.size
  68. th = Thread.new do
  69. w.write("HTTP/1.1 200 OK\r\nContent-Length: #{len}\r\n\r\n")
  70. n.times { w.write(buf) }
  71. :ok
  72. end
  73. io = Net::BufferedIO.new(r)
  74. res = Net::HTTPResponse.read_new(io)
  75. nr = 0
  76. res.reading_body io, true do
  77. # should be allowed to modify the chunk given to them:
  78. res.read_body do |chunk|
  79. nr += chunk.size
  80. chunk.clear
  81. end
  82. end
  83. assert_equal len, nr
  84. assert_equal :ok, th.value
  85. end
  86. end
  87. def test_read_body_content_encoding_deflate
  88. io = dummy_io(<<EOS)
  89. HTTP/1.1 200 OK
  90. Connection: close
  91. Content-Encoding: deflate
  92. Content-Length: 13
  93. x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
  94. EOS
  95. res = Net::HTTPResponse.read_new(io)
  96. res.decode_content = true
  97. body = nil
  98. res.reading_body io, true do
  99. body = res.read_body
  100. end
  101. if Net::HTTP::HAVE_ZLIB
  102. assert_equal nil, res['content-encoding']
  103. assert_equal 'hello', body
  104. else
  105. assert_equal 'deflate', res['content-encoding']
  106. assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
  107. end
  108. end
  109. def test_read_body_content_encoding_deflate_uppercase
  110. io = dummy_io(<<EOS)
  111. HTTP/1.1 200 OK
  112. Connection: close
  113. Content-Encoding: DEFLATE
  114. Content-Length: 13
  115. x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
  116. EOS
  117. res = Net::HTTPResponse.read_new(io)
  118. res.decode_content = true
  119. body = nil
  120. res.reading_body io, true do
  121. body = res.read_body
  122. end
  123. if Net::HTTP::HAVE_ZLIB
  124. assert_equal nil, res['content-encoding']
  125. assert_equal 'hello', body
  126. else
  127. assert_equal 'DEFLATE', res['content-encoding']
  128. assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
  129. end
  130. end
  131. def test_read_body_content_encoding_deflate_chunked
  132. io = dummy_io(<<EOS)
  133. HTTP/1.1 200 OK
  134. Connection: close
  135. Content-Encoding: deflate
  136. Transfer-Encoding: chunked
  137. 6
  138. x\x9C\xCBH\xCD\xC9
  139. 7
  140. \xC9\a\x00\x06,\x02\x15
  141. 0
  142. EOS
  143. res = Net::HTTPResponse.read_new(io)
  144. res.decode_content = true
  145. body = nil
  146. res.reading_body io, true do
  147. body = res.read_body
  148. end
  149. if Net::HTTP::HAVE_ZLIB
  150. assert_equal nil, res['content-encoding']
  151. assert_equal 'hello', body
  152. else
  153. assert_equal 'deflate', res['content-encoding']
  154. assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
  155. end
  156. end
  157. def test_read_body_content_encoding_deflate_disabled
  158. io = dummy_io(<<EOS)
  159. HTTP/1.1 200 OK
  160. Connection: close
  161. Content-Encoding: deflate
  162. Content-Length: 13
  163. x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
  164. EOS
  165. res = Net::HTTPResponse.read_new(io)
  166. res.decode_content = false # user set accept-encoding in request
  167. body = nil
  168. res.reading_body io, true do
  169. body = res.read_body
  170. end
  171. assert_equal 'deflate', res['content-encoding'], 'Bug #7831'
  172. assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body, 'Bug #7381'
  173. end
  174. def test_read_body_content_encoding_deflate_no_length
  175. io = dummy_io(<<EOS)
  176. HTTP/1.1 200 OK
  177. Connection: close
  178. Content-Encoding: deflate
  179. x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
  180. EOS
  181. res = Net::HTTPResponse.read_new(io)
  182. res.decode_content = true
  183. body = nil
  184. res.reading_body io, true do
  185. body = res.read_body
  186. end
  187. if Net::HTTP::HAVE_ZLIB
  188. assert_equal nil, res['content-encoding']
  189. assert_equal 'hello', body
  190. else
  191. assert_equal 'deflate', res['content-encoding']
  192. assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15\r\n", body
  193. end
  194. end
  195. def test_read_body_content_encoding_deflate_content_range
  196. io = dummy_io(<<EOS)
  197. HTTP/1.1 200 OK
  198. Accept-Ranges: bytes
  199. Connection: close
  200. Content-Encoding: gzip
  201. Content-Length: 10
  202. Content-Range: bytes 0-9/55
  203. \x1F\x8B\b\x00\x00\x00\x00\x00\x00\x03
  204. EOS
  205. res = Net::HTTPResponse.read_new(io)
  206. body = nil
  207. res.reading_body io, true do
  208. body = res.read_body
  209. end
  210. assert_equal "\x1F\x8B\b\x00\x00\x00\x00\x00\x00\x03", body
  211. end
  212. def test_read_body_content_encoding_deflate_empty_body
  213. io = dummy_io(<<EOS)
  214. HTTP/1.1 200 OK
  215. Connection: close
  216. Content-Encoding: deflate
  217. Content-Length: 0
  218. EOS
  219. res = Net::HTTPResponse.read_new(io)
  220. res.decode_content = true
  221. body = nil
  222. res.reading_body io, true do
  223. body = res.read_body
  224. end
  225. if Net::HTTP::HAVE_ZLIB
  226. assert_equal nil, res['content-encoding']
  227. assert_equal '', body
  228. else
  229. assert_equal 'deflate', res['content-encoding']
  230. assert_equal '', body
  231. end
  232. end
  233. def test_read_body_content_encoding_deflate_empty_body_no_length
  234. io = dummy_io(<<EOS)
  235. HTTP/1.1 200 OK
  236. Connection: close
  237. Content-Encoding: deflate
  238. EOS
  239. res = Net::HTTPResponse.read_new(io)
  240. res.decode_content = true
  241. body = nil
  242. res.reading_body io, true do
  243. body = res.read_body
  244. end
  245. if Net::HTTP::HAVE_ZLIB
  246. assert_equal nil, res['content-encoding']
  247. assert_equal '', body
  248. else
  249. assert_equal 'deflate', res['content-encoding']
  250. assert_equal '', body
  251. end
  252. end
  253. def test_read_body_string
  254. io = dummy_io(<<EOS)
  255. HTTP/1.1 200 OK
  256. Connection: close
  257. Content-Length: 5
  258. hello
  259. EOS
  260. res = Net::HTTPResponse.read_new(io)
  261. body = ''
  262. res.reading_body io, true do
  263. res.read_body body
  264. end
  265. assert_equal 'hello', body
  266. end
  267. def test_uri_equals
  268. uri = URI 'http://example'
  269. response = Net::HTTPResponse.new '1.1', 200, 'OK'
  270. response.uri = nil
  271. assert_nil response.uri
  272. response.uri = uri
  273. assert_equal uri, response.uri
  274. assert_not_same uri, response.uri
  275. end
  276. def test_ensure_zero_space_does_not_regress
  277. io = dummy_io(<<EOS)
  278. HTTP/1.1 200OK
  279. Content-Length: 5
  280. Connection: close
  281. hello
  282. EOS
  283. assert_raise Net::HTTPBadResponse do
  284. Net::HTTPResponse.read_new(io)
  285. end
  286. end
  287. def test_allow_trailing_space_after_status
  288. io = dummy_io(<<EOS)
  289. HTTP/1.1 200\s
  290. Content-Length: 5
  291. Connection: close
  292. hello
  293. EOS
  294. res = Net::HTTPResponse.read_new(io)
  295. assert_equal('1.1', res.http_version)
  296. assert_equal('200', res.code)
  297. assert_equal('', res.message)
  298. end
  299. def test_normal_status_line
  300. io = dummy_io(<<EOS)
  301. HTTP/1.1 200 OK
  302. Content-Length: 5
  303. Connection: close
  304. hello
  305. EOS
  306. res = Net::HTTPResponse.read_new(io)
  307. assert_equal('1.1', res.http_version)
  308. assert_equal('200', res.code)
  309. assert_equal('OK', res.message)
  310. end
  311. def test_allow_empty_reason_code
  312. io = dummy_io(<<EOS)
  313. HTTP/1.1 200
  314. Content-Length: 5
  315. Connection: close
  316. hello
  317. EOS
  318. res = Net::HTTPResponse.read_new(io)
  319. assert_equal('1.1', res.http_version)
  320. assert_equal('200', res.code)
  321. assert_equal(nil, res.message)
  322. end
  323. def test_raises_exception_with_missing_reason
  324. io = dummy_io(<<EOS)
  325. HTTP/1.1 404
  326. Content-Length: 5
  327. Connection: close
  328. hello
  329. EOS
  330. res = Net::HTTPResponse.read_new(io)
  331. assert_equal(nil, res.message)
  332. assert_raise Net::HTTPClientException do
  333. res.error!
  334. end
  335. end
  336. def test_read_code_type
  337. res = Net::HTTPUnknownResponse.new('1.0', '???', 'test response')
  338. assert_equal Net::HTTPUnknownResponse, res.code_type
  339. res = Net::HTTPInformation.new('1.0', '1xx', 'test response')
  340. assert_equal Net::HTTPInformation, res.code_type
  341. res = Net::HTTPSuccess.new('1.0', '2xx', 'test response')
  342. assert_equal Net::HTTPSuccess, res.code_type
  343. res = Net::HTTPRedirection.new('1.0', '3xx', 'test response')
  344. assert_equal Net::HTTPRedirection, res.code_type
  345. res = Net::HTTPClientError.new('1.0', '4xx', 'test response')
  346. assert_equal Net::HTTPClientError, res.code_type
  347. res = Net::HTTPServerError.new('1.0', '5xx', 'test response')
  348. assert_equal Net::HTTPServerError, res.code_type
  349. end
  350. def test_inspect_response
  351. res = Net::HTTPUnknownResponse.new('1.0', '???', 'test response')
  352. assert_equal '#<Net::HTTPUnknownResponse ??? test response readbody=false>', res.inspect
  353. res = Net::HTTPUnknownResponse.new('1.0', '???', 'test response')
  354. socket = Net::BufferedIO.new(StringIO.new('test body'))
  355. res.reading_body(socket, true) {}
  356. assert_equal '#<Net::HTTPUnknownResponse ??? test response readbody=true>', res.inspect
  357. end
  358. private
  359. def dummy_io(str)
  360. str = str.gsub(/\n/, "\r\n")
  361. Net::BufferedIO.new(StringIO.new(str))
  362. end
  363. end