PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/nicksieger/jruby
Ruby | 349 lines | 309 code | 38 blank | 2 comment | 1 complexity | f140f4f61a5b41855059d8d70eff02c0 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_request_uri_too_large
  54. msg = <<-_end_of_message_
  55. GET /#{"a"*1024} HTTP/1.1
  56. _end_of_message_
  57. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  58. assert_raise(WEBrick::HTTPStatus::RequestURITooLarge){
  59. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  60. }
  61. end
  62. def test_parse_headers
  63. msg = <<-_end_of_message_
  64. GET /path HTTP/1.1
  65. Host: test.ruby-lang.org:8080
  66. Connection: close
  67. Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1,
  68. text/html;level=2;q=0.4, */*;q=0.5
  69. Accept-Encoding: compress;q=0.5
  70. Accept-Encoding: gzip;q=1.0, identity; q=0.4, *;q=0
  71. Accept-Language: en;q=0.5, *; q=0
  72. Accept-Language: ja
  73. Content-Type: text/plain
  74. Content-Length: 7
  75. X-Empty-Header:
  76. foobar
  77. _end_of_message_
  78. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  79. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  80. assert_equal(
  81. URI.parse("http://test.ruby-lang.org:8080/path"), req.request_uri)
  82. assert_equal("test.ruby-lang.org", req.host)
  83. assert_equal(8080, req.port)
  84. assert_equal(false, req.keep_alive?)
  85. assert_equal(
  86. %w(text/html;level=1 text/html */* text/html;level=2 text/*),
  87. req.accept)
  88. assert_equal(%w(gzip compress identity *), req.accept_encoding)
  89. assert_equal(%w(ja en *), req.accept_language)
  90. assert_equal(7, req.content_length)
  91. assert_equal("text/plain", req.content_type)
  92. assert_equal("foobar\n", req.body)
  93. assert_equal("", req["x-empty-header"])
  94. assert_equal(nil, req["x-no-header"])
  95. assert(req.query.empty?)
  96. end
  97. def test_parse_header2()
  98. msg = <<-_end_of_message_
  99. POST /foo/bar/../baz?q=a HTTP/1.0
  100. Content-Length: 9
  101. User-Agent:
  102. FOO BAR
  103. BAZ
  104. hogehoge
  105. _end_of_message_
  106. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  107. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  108. assert_equal("POST", req.request_method)
  109. assert_equal("/foo/baz", req.path)
  110. assert_equal("", req.script_name)
  111. assert_equal("/foo/baz", req.path_info)
  112. assert_equal("9", req['content-length'])
  113. assert_equal("FOO BAR BAZ", req['user-agent'])
  114. assert_equal("hogehoge\n", req.body)
  115. end
  116. def test_parse_headers3
  117. msg = <<-_end_of_message_
  118. GET /path HTTP/1.1
  119. Host: test.ruby-lang.org
  120. _end_of_message_
  121. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  122. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  123. assert_equal(URI.parse("http://test.ruby-lang.org/path"), req.request_uri)
  124. assert_equal("test.ruby-lang.org", req.host)
  125. assert_equal(80, req.port)
  126. msg = <<-_end_of_message_
  127. GET /path HTTP/1.1
  128. Host: 192.168.1.1
  129. _end_of_message_
  130. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  131. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  132. assert_equal(URI.parse("http://192.168.1.1/path"), req.request_uri)
  133. assert_equal("192.168.1.1", req.host)
  134. assert_equal(80, req.port)
  135. msg = <<-_end_of_message_
  136. GET /path HTTP/1.1
  137. Host: [fe80::208:dff:feef:98c7]
  138. _end_of_message_
  139. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  140. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  141. assert_equal(URI.parse("http://[fe80::208:dff:feef:98c7]/path"),
  142. req.request_uri)
  143. assert_equal("[fe80::208:dff:feef:98c7]", req.host)
  144. assert_equal(80, req.port)
  145. msg = <<-_end_of_message_
  146. GET /path HTTP/1.1
  147. Host: 192.168.1.1:8080
  148. _end_of_message_
  149. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  150. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  151. assert_equal(URI.parse("http://192.168.1.1:8080/path"), req.request_uri)
  152. assert_equal("192.168.1.1", req.host)
  153. assert_equal(8080, req.port)
  154. msg = <<-_end_of_message_
  155. GET /path HTTP/1.1
  156. Host: [fe80::208:dff:feef:98c7]:8080
  157. _end_of_message_
  158. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  159. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  160. assert_equal(URI.parse("http://[fe80::208:dff:feef:98c7]:8080/path"),
  161. req.request_uri)
  162. assert_equal("[fe80::208:dff:feef:98c7]", req.host)
  163. assert_equal(8080, req.port)
  164. end
  165. def test_parse_get_params
  166. param = "foo=1;foo=2;foo=3;bar=x"
  167. msg = <<-_end_of_message_
  168. GET /path?#{param} HTTP/1.1
  169. Host: test.ruby-lang.org:8080
  170. _end_of_message_
  171. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  172. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  173. query = req.query
  174. assert_equal("1", query["foo"])
  175. assert_equal(["1", "2", "3"], query["foo"].to_ary)
  176. assert_equal(["1", "2", "3"], query["foo"].list)
  177. assert_equal("x", query["bar"])
  178. assert_equal(["x"], query["bar"].list)
  179. end
  180. def test_parse_post_params
  181. param = "foo=1;foo=2;foo=3;bar=x"
  182. msg = <<-_end_of_message_
  183. POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
  184. Host: test.ruby-lang.org:8080
  185. Content-Length: #{param.size}
  186. Content-Type: application/x-www-form-urlencoded
  187. #{param}
  188. _end_of_message_
  189. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  190. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  191. query = req.query
  192. assert_equal("1", query["foo"])
  193. assert_equal(["1", "2", "3"], query["foo"].to_ary)
  194. assert_equal(["1", "2", "3"], query["foo"].list)
  195. assert_equal("x", query["bar"])
  196. assert_equal(["x"], query["bar"].list)
  197. end
  198. def test_chunked
  199. crlf = "\x0d\x0a"
  200. msg = <<-_end_of_message_
  201. POST /path HTTP/1.1
  202. Host: test.ruby-lang.org:8080
  203. Transfer-Encoding: chunked
  204. _end_of_message_
  205. msg.gsub!(/^ {6}/, "")
  206. open(__FILE__){|io|
  207. while chunk = io.read(100)
  208. msg << chunk.size.to_s(16) << crlf
  209. msg << chunk << crlf
  210. end
  211. }
  212. msg << "0" << crlf
  213. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  214. req.parse(StringIO.new(msg))
  215. assert_equal(File.read(__FILE__), req.body)
  216. end
  217. def test_forwarded
  218. msg = <<-_end_of_message_
  219. GET /foo HTTP/1.1
  220. Host: localhost:10080
  221. User-Agent: w3m/0.5.2
  222. X-Forwarded-For: 123.123.123.123
  223. X-Forwarded-Host: forward.example.com
  224. X-Forwarded-Server: server.example.com
  225. Connection: Keep-Alive
  226. _end_of_message_
  227. msg.gsub!(/^ {6}/, "")
  228. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  229. req.parse(StringIO.new(msg))
  230. assert_equal("server.example.com", req.server_name)
  231. assert_equal("http://forward.example.com/foo", req.request_uri.to_s)
  232. assert_equal("forward.example.com", req.host)
  233. assert_equal(80, req.port)
  234. assert_equal("123.123.123.123", req.remote_ip)
  235. assert(!req.ssl?)
  236. msg = <<-_end_of_message_
  237. GET /foo HTTP/1.1
  238. Host: localhost:10080
  239. User-Agent: w3m/0.5.2
  240. X-Forwarded-For: 192.168.1.10, 172.16.1.1, 123.123.123.123
  241. X-Forwarded-Host: forward.example.com:8080
  242. X-Forwarded-Server: server.example.com
  243. Connection: Keep-Alive
  244. _end_of_message_
  245. msg.gsub!(/^ {6}/, "")
  246. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  247. req.parse(StringIO.new(msg))
  248. assert_equal("server.example.com", req.server_name)
  249. assert_equal("http://forward.example.com:8080/foo", req.request_uri.to_s)
  250. assert_equal("forward.example.com", req.host)
  251. assert_equal(8080, req.port)
  252. assert_equal("123.123.123.123", req.remote_ip)
  253. assert(!req.ssl?)
  254. msg = <<-_end_of_message_
  255. GET /foo HTTP/1.1
  256. Host: localhost:10080
  257. Client-IP: 234.234.234.234
  258. X-Forwarded-Proto: https
  259. X-Forwarded-For: 192.168.1.10, 10.0.0.1, 123.123.123.123
  260. X-Forwarded-Host: forward.example.com
  261. X-Forwarded-Server: server.example.com
  262. X-Requested-With: XMLHttpRequest
  263. Connection: Keep-Alive
  264. _end_of_message_
  265. msg.gsub!(/^ {6}/, "")
  266. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  267. req.parse(StringIO.new(msg))
  268. assert_equal("server.example.com", req.server_name)
  269. assert_equal("https://forward.example.com/foo", req.request_uri.to_s)
  270. assert_equal("forward.example.com", req.host)
  271. assert_equal(443, req.port)
  272. assert_equal("234.234.234.234", req.remote_ip)
  273. assert(req.ssl?)
  274. end
  275. def test_bad_messages
  276. param = "foo=1;foo=2;foo=3;bar=x"
  277. msg = <<-_end_of_message_
  278. POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
  279. Host: test.ruby-lang.org:8080
  280. Content-Type: application/x-www-form-urlencoded
  281. #{param}
  282. _end_of_message_
  283. assert_raise(WEBrick::HTTPStatus::LengthRequired){
  284. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  285. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  286. req.body
  287. }
  288. msg = <<-_end_of_message_
  289. POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
  290. Host: test.ruby-lang.org:8080
  291. Content-Length: 100000
  292. body is too short.
  293. _end_of_message_
  294. assert_raise(WEBrick::HTTPStatus::BadRequest){
  295. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  296. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  297. req.body
  298. }
  299. msg = <<-_end_of_message_
  300. POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
  301. Host: test.ruby-lang.org:8080
  302. Transfer-Encoding: foobar
  303. body is too short.
  304. _end_of_message_
  305. assert_raise(WEBrick::HTTPStatus::NotImplemented){
  306. req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  307. req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
  308. req.body
  309. }
  310. end
  311. end