PageRenderTime 71ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/test/externals/ruby1.9/net/http/test_http.rb

https://bitbucket.org/nicksieger/jruby
Ruby | 374 lines | 304 code | 41 blank | 29 comment | 0 complexity | 2c33cf0a222828d5c8ae7d7e902baf83 MD5 | raw file
Possible License(s): GPL-3.0, JSON
  1. # $Id$
  2. require 'test/unit'
  3. require 'net/http'
  4. require 'stringio'
  5. require File.expand_path("utils", File.dirname(__FILE__))
  6. module TestNetHTTP_version_1_1_methods
  7. def test_s_get
  8. assert_equal $test_net_http_data,
  9. Net::HTTP.get(config('host'), '/', config('port'))
  10. end
  11. def test_head
  12. start {|http|
  13. res = http.head('/')
  14. assert_kind_of Net::HTTPResponse, res
  15. assert_equal $test_net_http_data_type, res['Content-Type']
  16. unless self.is_a?(TestNetHTTP_v1_2_chunked)
  17. assert_equal $test_net_http_data.size, res['Content-Length'].to_i
  18. end
  19. }
  20. end
  21. def test_get
  22. start {|http|
  23. _test_get__get http
  24. _test_get__iter http
  25. _test_get__chunked http
  26. }
  27. end
  28. def _test_get__get(http)
  29. res, body = http.get('/')
  30. assert_kind_of Net::HTTPResponse, res
  31. assert_kind_of String, res.body
  32. assert_kind_of String, body
  33. unless self.is_a?(TestNetHTTP_v1_2_chunked)
  34. assert_not_nil res['content-length']
  35. assert_equal $test_net_http_data.size, res['content-length'].to_i
  36. end
  37. assert_equal $test_net_http_data_type, res['Content-Type']
  38. assert_equal $test_net_http_data.size, body.size
  39. assert_equal $test_net_http_data, body
  40. assert_equal $test_net_http_data.size, res.body.size
  41. assert_equal $test_net_http_data, res.body
  42. end
  43. def _test_get__iter(http)
  44. buf = ''
  45. res, body = http.get('/') {|s| buf << s }
  46. assert_kind_of Net::HTTPResponse, res
  47. # assert_kind_of String, res.body
  48. # assert_kind_of String, body
  49. unless self.is_a?(TestNetHTTP_v1_2_chunked)
  50. assert_not_nil res['content-length']
  51. assert_equal $test_net_http_data.size, res['content-length'].to_i
  52. end
  53. assert_equal $test_net_http_data_type, res['Content-Type']
  54. assert_equal $test_net_http_data.size, buf.size
  55. assert_equal $test_net_http_data, buf
  56. # assert_equal $test_net_http_data.size, res.body.size
  57. # assert_equal $test_net_http_data, res.body
  58. end
  59. def _test_get__chunked(http)
  60. buf = ''
  61. res, body = http.get('/') {|s| buf << s }
  62. assert_kind_of Net::HTTPResponse, res
  63. # assert_kind_of String, res.body
  64. # assert_kind_of String, body
  65. unless self.is_a?(TestNetHTTP_v1_2_chunked)
  66. assert_not_nil res['content-length']
  67. assert_equal $test_net_http_data.size, res['content-length'].to_i
  68. end
  69. assert_equal $test_net_http_data_type, res['Content-Type']
  70. assert_equal $test_net_http_data.size, buf.size
  71. assert_equal $test_net_http_data, buf
  72. # assert_equal $test_net_http_data.size, res.body.size
  73. # assert_equal $test_net_http_data, res.body
  74. end
  75. def test_get__break
  76. i = 0
  77. start {|http|
  78. http.get('/') do |str|
  79. i += 1
  80. break
  81. end
  82. }
  83. assert_equal 1, i
  84. end
  85. def test_get__implicit_start
  86. res, body = new().get('/')
  87. assert_kind_of Net::HTTPResponse, res
  88. assert_kind_of String, body
  89. assert_kind_of String, res.body
  90. unless self.is_a?(TestNetHTTP_v1_2_chunked)
  91. assert_not_nil res['content-length']
  92. end
  93. assert_equal $test_net_http_data_type, res['Content-Type']
  94. assert_equal $test_net_http_data.size, res.body.size
  95. assert_equal $test_net_http_data, res.body
  96. end
  97. def test_get2
  98. start {|http|
  99. http.get2('/') {|res|
  100. assert_kind_of Net::HTTPResponse, res
  101. assert_kind_of Net::HTTPResponse, res.header
  102. unless self.is_a?(TestNetHTTP_v1_2_chunked)
  103. assert_not_nil res['content-length']
  104. end
  105. assert_equal $test_net_http_data_type, res['Content-Type']
  106. assert_kind_of String, res.body
  107. assert_kind_of String, res.entity
  108. assert_equal $test_net_http_data.size, res.body.size
  109. assert_equal $test_net_http_data, res.body
  110. assert_equal $test_net_http_data, res.entity
  111. }
  112. }
  113. end
  114. def test_post
  115. start {|http|
  116. _test_post__base http
  117. _test_post__file http
  118. }
  119. end
  120. def _test_post__base(http)
  121. uheader = {}
  122. uheader['Accept'] = 'application/octet-stream'
  123. data = 'post data'
  124. res, body = http.post('/', data)
  125. assert_kind_of Net::HTTPResponse, res
  126. assert_kind_of String, body
  127. assert_kind_of String, res.body
  128. assert_equal data, body
  129. assert_equal data, res.body
  130. assert_equal data, res.entity
  131. end
  132. def _test_post__file(http)
  133. data = 'post data'
  134. f = StringIO.new
  135. http.post('/', data, nil, f)
  136. assert_equal data, f.string
  137. end
  138. def test_s_post_form
  139. res = Net::HTTP.post_form(
  140. URI.parse("http://#{config('host')}:#{config('port')}/"),
  141. "a" => "x")
  142. assert_equal ["a=x"], res.body.split(/[;&]/).sort
  143. res = Net::HTTP.post_form(
  144. URI.parse("http://#{config('host')}:#{config('port')}/"),
  145. "a" => "x",
  146. "b" => "y")
  147. assert_equal ["a=x", "b=y"], res.body.split(/[;&]/).sort
  148. res = Net::HTTP.post_form(
  149. URI.parse("http://#{config('host')}:#{config('port')}/"),
  150. "a" => ["x1", "x2"],
  151. "b" => "y")
  152. assert_equal ["a=x1", "a=x2", "b=y"], res.body.split(/[;&]/).sort
  153. end
  154. end
  155. module TestNetHTTP_version_1_2_methods
  156. def test_request
  157. start {|http|
  158. _test_request__GET http
  159. _test_request__file http
  160. # _test_request__range http # WEBrick does not support Range: header.
  161. _test_request__HEAD http
  162. _test_request__POST http
  163. _test_request__stream_body http
  164. }
  165. end
  166. def _test_request__GET(http)
  167. req = Net::HTTP::Get.new('/')
  168. http.request(req) {|res|
  169. assert_kind_of Net::HTTPResponse, res
  170. assert_kind_of String, res.body
  171. unless self.is_a?(TestNetHTTP_v1_2_chunked)
  172. assert_not_nil res['content-length']
  173. assert_equal $test_net_http_data.size, res['content-length'].to_i
  174. end
  175. assert_equal $test_net_http_data.size, res.body.size
  176. assert_equal $test_net_http_data, res.body
  177. }
  178. end
  179. def _test_request__file(http)
  180. req = Net::HTTP::Get.new('/')
  181. http.request(req) {|res|
  182. assert_kind_of Net::HTTPResponse, res
  183. unless self.is_a?(TestNetHTTP_v1_2_chunked)
  184. assert_not_nil res['content-length']
  185. assert_equal $test_net_http_data.size, res['content-length'].to_i
  186. end
  187. f = StringIO.new("".force_encoding("ASCII-8BIT"))
  188. res.read_body f
  189. assert_equal $test_net_http_data.bytesize, f.string.bytesize
  190. assert_equal $test_net_http_data.encoding, f.string.encoding
  191. assert_equal $test_net_http_data, f.string
  192. }
  193. end
  194. def _test_request__range(http)
  195. req = Net::HTTP::Get.new('/')
  196. req['range'] = 'bytes=0-5'
  197. assert_equal $test_net_http_data[0,6], http.request(req).body
  198. end
  199. def _test_request__HEAD(http)
  200. req = Net::HTTP::Head.new('/')
  201. http.request(req) {|res|
  202. assert_kind_of Net::HTTPResponse, res
  203. unless self.is_a?(TestNetHTTP_v1_2_chunked)
  204. assert_not_nil res['content-length']
  205. assert_equal $test_net_http_data.size, res['content-length'].to_i
  206. end
  207. assert_nil res.body
  208. }
  209. end
  210. def _test_request__POST(http)
  211. data = 'post data'
  212. req = Net::HTTP::Post.new('/')
  213. req['Accept'] = $test_net_http_data_type
  214. http.request(req, data) {|res|
  215. assert_kind_of Net::HTTPResponse, res
  216. unless self.is_a?(TestNetHTTP_v1_2_chunked)
  217. assert_equal data.size, res['content-length'].to_i
  218. end
  219. assert_kind_of String, res.body
  220. assert_equal data, res.body
  221. }
  222. end
  223. def _test_request__stream_body(http)
  224. req = Net::HTTP::Post.new('/')
  225. data = $test_net_http_data
  226. req.content_length = data.size
  227. req.body_stream = StringIO.new(data)
  228. res = http.request(req)
  229. assert_kind_of Net::HTTPResponse, res
  230. assert_kind_of String, res.body
  231. assert_equal data.size, res.body.size
  232. assert_equal data, res.body
  233. end
  234. def test_send_request
  235. start {|http|
  236. _test_send_request__GET http
  237. _test_send_request__POST http
  238. }
  239. end
  240. def _test_send_request__GET(http)
  241. res = http.send_request('GET', '/')
  242. assert_kind_of Net::HTTPResponse, res
  243. unless self.is_a?(TestNetHTTP_v1_2_chunked)
  244. assert_equal $test_net_http_data.size, res['content-length'].to_i
  245. end
  246. assert_kind_of String, res.body
  247. assert_equal $test_net_http_data, res.body
  248. end
  249. def _test_send_request__POST(http)
  250. data = 'aaabbb cc ddddddddddd lkjoiu4j3qlkuoa'
  251. res = http.send_request('POST', '/', data)
  252. assert_kind_of Net::HTTPResponse, res
  253. assert_kind_of String, res.body
  254. assert_equal data.size, res.body.size
  255. assert_equal data, res.body
  256. end
  257. end
  258. class TestNetHTTP_version_1_1 < Test::Unit::TestCase
  259. CONFIG = {
  260. 'host' => '127.0.0.1',
  261. 'port' => 10081,
  262. 'proxy_host' => nil,
  263. 'proxy_port' => nil,
  264. }
  265. include TestNetHTTPUtils
  266. include TestNetHTTP_version_1_1_methods
  267. def new
  268. Net::HTTP.version_1_1
  269. super
  270. end
  271. end
  272. class TestNetHTTP_v1_2 < Test::Unit::TestCase
  273. CONFIG = {
  274. 'host' => '127.0.0.1',
  275. 'port' => 10081,
  276. 'proxy_host' => nil,
  277. 'proxy_port' => nil,
  278. }
  279. include TestNetHTTPUtils
  280. include TestNetHTTP_version_1_1_methods
  281. include TestNetHTTP_version_1_2_methods
  282. def new
  283. Net::HTTP.version_1_2
  284. super
  285. end
  286. end
  287. class TestNetHTTP_v1_2_chunked < Test::Unit::TestCase
  288. CONFIG = {
  289. 'host' => '127.0.0.1',
  290. 'port' => 10081,
  291. 'proxy_host' => nil,
  292. 'proxy_port' => nil,
  293. 'chunked' => true,
  294. }
  295. include TestNetHTTPUtils
  296. include TestNetHTTP_version_1_1_methods
  297. include TestNetHTTP_version_1_2_methods
  298. def new
  299. Net::HTTP.version_1_2
  300. super
  301. end
  302. def test_chunked_break
  303. i = 0
  304. assert_nothing_raised("[ruby-core:29229]") {
  305. start {|http|
  306. http.request_get('/') {|res|
  307. res.read_body {|chunk|
  308. break
  309. }
  310. }
  311. }
  312. }
  313. end
  314. end
  315. =begin
  316. class TestNetHTTP_proxy < Test::Unit::TestCase
  317. CONFIG = {
  318. 'host' => '127.0.0.1',
  319. 'port' => 10081,
  320. 'proxy_host' => '127.0.0.1',
  321. 'proxy_port' => 10082,
  322. }
  323. include TestNetHTTPUtils
  324. include TestNetHTTP_version_1_1_methods
  325. include TestNetHTTP_version_1_2_methods
  326. def new
  327. Net::HTTP.version_1_2
  328. super
  329. end
  330. end
  331. =end