PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/test/externals/ruby1.8/webrick/test_httprequest.rb

https://bitbucket.org/nicksieger/jruby
Ruby | 272 lines | 239 code | 31 blank | 2 comment | 1 complexity | 171f4957b87333fbfbde7ffe36ac0d50 MD5 | raw file
Possible License(s): GPL-3.0, JSON
  1. require "webrick"
  2. require "stringio"
  3. require "test/unit"
  4. class TestWEBrickHTTPRequest < Test::Unit::TestCase
  5. def test_parse_09
  6. msg = <<-_end_of_message_
  7. GET /
  8. foobar # HTTP/0.9 request don't have header nor entity body.
  9. _end_of_message_
  10. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  11. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  12. assert_equal("GET", req.request_method)
  13. assert_equal("/", req.unparsed_uri)
  14. assert_equal(WEBrick::HTTPVersion.new("0.9"), req.http_version)
  15. assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
  16. assert_equal(80, req.port)
  17. assert_equal(false, req.keep_alive?)
  18. assert_equal(nil, req.body)
  19. assert(req.query.empty?)
  20. end
  21. def test_parse_10
  22. msg = <<-_end_of_message_
  23. GET / HTTP/1.0
  24. _end_of_message_
  25. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  26. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  27. assert_equal("GET", req.request_method)
  28. assert_equal("/", req.unparsed_uri)
  29. assert_equal(WEBrick::HTTPVersion.new("1.0"), req.http_version)
  30. assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
  31. assert_equal(80, req.port)
  32. assert_equal(false, req.keep_alive?)
  33. assert_equal(nil, req.body)
  34. assert(req.query.empty?)
  35. end
  36. def test_parse_11
  37. msg = <<-_end_of_message_
  38. GET /path HTTP/1.1
  39. _end_of_message_
  40. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  41. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  42. assert_equal("GET", req.request_method)
  43. assert_equal("/path", req.unparsed_uri)
  44. assert_equal("", req.script_name)
  45. assert_equal("/path", req.path_info)
  46. assert_equal(WEBrick::HTTPVersion.new("1.1"), req.http_version)
  47. assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
  48. assert_equal(80, req.port)
  49. assert_equal(true, req.keep_alive?)
  50. assert_equal(nil, req.body)
  51. assert(req.query.empty?)
  52. end
  53. def test_parse_headers
  54. msg = <<-_end_of_message_
  55. GET /path HTTP/1.1
  56. Host: test.ruby-lang.org:8080
  57. Connection: close
  58. Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1,
  59. text/html;level=2;q=0.4, */*;q=0.5
  60. Accept-Encoding: compress;q=0.5
  61. Accept-Encoding: gzip;q=1.0, identity; q=0.4, *;q=0
  62. Accept-Language: en;q=0.5, *; q=0
  63. Accept-Language: ja
  64. Content-Type: text/plain
  65. Content-Length: 7
  66. foobar
  67. _end_of_message_
  68. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  69. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  70. assert_equal(
  71. URI.parse("http://test.ruby-lang.org:8080/path"), req.request_uri)
  72. assert_equal("test.ruby-lang.org", req.host)
  73. assert_equal(8080, req.port)
  74. assert_equal(false, req.keep_alive?)
  75. assert_equal(
  76. %w(text/html;level=1 text/html */* text/html;level=2 text/*),
  77. req.accept)
  78. assert_equal(%w(gzip compress identity *), req.accept_encoding)
  79. assert_equal(%w(ja en *), req.accept_language)
  80. assert_equal(7, req.content_length)
  81. assert_equal("text/plain", req.content_type)
  82. assert_equal("foobar\n", req.body)
  83. assert(req.query.empty?)
  84. end
  85. def test_parse_header2()
  86. msg = <<-_end_of_message_
  87. POST /foo/bar/../baz?q=a HTTP/1.0
  88. Content-Length: 9
  89. User-Agent:
  90. FOO BAR
  91. BAZ
  92. hogehoge
  93. _end_of_message_
  94. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  95. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  96. assert_equal("POST", req.request_method)
  97. assert_equal("/foo/baz", req.path)
  98. assert_equal("", req.script_name)
  99. assert_equal("/foo/baz", req.path_info)
  100. assert_equal("9", req['content-length'])
  101. assert_equal("FOO BAR BAZ", req['user-agent'])
  102. assert_equal("hogehoge\n", req.body)
  103. end
  104. def test_parse_headers3
  105. msg = <<-_end_of_message_
  106. GET /path HTTP/1.1
  107. Host: test.ruby-lang.org
  108. _end_of_message_
  109. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  110. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  111. assert_equal(URI.parse("http://test.ruby-lang.org/path"), req.request_uri)
  112. assert_equal("test.ruby-lang.org", req.host)
  113. assert_equal(80, req.port)
  114. msg = <<-_end_of_message_
  115. GET /path HTTP/1.1
  116. Host: 192.168.1.1
  117. _end_of_message_
  118. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  119. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  120. assert_equal(URI.parse("http://192.168.1.1/path"), req.request_uri)
  121. assert_equal("192.168.1.1", req.host)
  122. assert_equal(80, req.port)
  123. msg = <<-_end_of_message_
  124. GET /path HTTP/1.1
  125. Host: [fe80::208:dff:feef:98c7]
  126. _end_of_message_
  127. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  128. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  129. assert_equal(URI.parse("http://[fe80::208:dff:feef:98c7]/path"),
  130. req.request_uri)
  131. assert_equal("[fe80::208:dff:feef:98c7]", req.host)
  132. assert_equal(80, req.port)
  133. msg = <<-_end_of_message_
  134. GET /path HTTP/1.1
  135. Host: 192.168.1.1:8080
  136. _end_of_message_
  137. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  138. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  139. assert_equal(URI.parse("http://192.168.1.1:8080/path"), req.request_uri)
  140. assert_equal("192.168.1.1", req.host)
  141. assert_equal(8080, req.port)
  142. msg = <<-_end_of_message_
  143. GET /path HTTP/1.1
  144. Host: [fe80::208:dff:feef:98c7]:8080
  145. _end_of_message_
  146. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  147. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  148. assert_equal(URI.parse("http://[fe80::208:dff:feef:98c7]:8080/path"),
  149. req.request_uri)
  150. assert_equal("[fe80::208:dff:feef:98c7]", req.host)
  151. assert_equal(8080, req.port)
  152. end
  153. def test_parse_get_params
  154. param = "foo=1;foo=2;foo=3;bar=x"
  155. msg = <<-_end_of_message_
  156. GET /path?#{param} HTTP/1.1
  157. Host: test.ruby-lang.org:8080
  158. _end_of_message_
  159. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  160. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  161. query = req.query
  162. assert_equal("1", query["foo"])
  163. assert_equal(["1", "2", "3"], query["foo"].to_ary)
  164. assert_equal(["1", "2", "3"], query["foo"].list)
  165. assert_equal("x", query["bar"])
  166. assert_equal(["x"], query["bar"].list)
  167. end
  168. def test_parse_post_params
  169. param = "foo=1;foo=2;foo=3;bar=x"
  170. msg = <<-_end_of_message_
  171. POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
  172. Host: test.ruby-lang.org:8080
  173. Content-Length: #{param.size}
  174. Content-Type: application/x-www-form-urlencoded
  175. #{param}
  176. _end_of_message_
  177. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  178. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  179. query = req.query
  180. assert_equal("1", query["foo"])
  181. assert_equal(["1", "2", "3"], query["foo"].to_ary)
  182. assert_equal(["1", "2", "3"], query["foo"].list)
  183. assert_equal("x", query["bar"])
  184. assert_equal(["x"], query["bar"].list)
  185. end
  186. def test_chunked
  187. crlf = "\x0d\x0a"
  188. msg = <<-_end_of_message_
  189. POST /path HTTP/1.1
  190. Host: test.ruby-lang.org:8080
  191. Transfer-Encoding: chunked
  192. _end_of_message_
  193. msg.gsub!(/^ {6}/, "")
  194. open(__FILE__){|io|
  195. while chunk = io.read(100)
  196. msg << chunk.size.to_s(16) << crlf
  197. msg << chunk << crlf
  198. end
  199. }
  200. msg << "0" << crlf
  201. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  202. req.parse(StringIO.new(msg))
  203. assert_equal(File.read(__FILE__), req.body)
  204. end
  205. def test_bad_messages
  206. param = "foo=1;foo=2;foo=3;bar=x"
  207. msg = <<-_end_of_message_
  208. POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
  209. Host: test.ruby-lang.org:8080
  210. Content-Type: application/x-www-form-urlencoded
  211. #{param}
  212. _end_of_message_
  213. assert_raises(WEBrick::HTTPStatus::LengthRequired){
  214. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  215. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  216. req.body
  217. }
  218. msg = <<-_end_of_message_
  219. POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
  220. Host: test.ruby-lang.org:8080
  221. Content-Length: 100000
  222. body is too short.
  223. _end_of_message_
  224. assert_raises(WEBrick::HTTPStatus::BadRequest){
  225. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  226. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  227. req.body
  228. }
  229. msg = <<-_end_of_message_
  230. POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
  231. Host: test.ruby-lang.org:8080
  232. Transfer-Encoding: foobar
  233. body is too short.
  234. _end_of_message_
  235. assert_raises(WEBrick::HTTPStatus::NotImplemented){
  236. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  237. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  238. req.body
  239. }
  240. end
  241. end