1 # -*- encoding: binary -*-
3 # Copyright (c) 2009 Eric Wong
4 # You can redistribute it and/or modify it under the same terms as Ruby 1.8 or
5 # the GPLv2+ (GPLv3+ preferred)
7 require 'test/test_helper'
11 class RequestTest < Test::Unit::TestCase
13 class MockRequest < StringIO
14 alias_method :readpartial, :sysread
15 alias_method :kgio_read!, :sysread
16 alias_method :read_nonblock, :sysread
23 @request = HttpRequest.new
24 @app = lambda do |env|
25 [ 200, { 'Content-Length' => '0', 'Content-Type' => 'text/plain' }, [] ]
27 @lint = Rack::Lint.new(@app)
31 client = MockRequest.new("OPTIONS * HTTP/1.1\r\n" \
33 env = @request.read(client)
34 assert_equal '', env['REQUEST_PATH']
35 assert_equal '', env['PATH_INFO']
36 assert_equal '*', env['REQUEST_URI']
40 def test_absolute_uri_with_query
41 client = MockRequest.new("GET http://e:3/x?y=z HTTP/1.1\r\n" \
43 env = @request.read(client)
44 assert_equal '/x', env['REQUEST_PATH']
45 assert_equal '/x', env['PATH_INFO']
46 assert_equal 'y=z', env['QUERY_STRING']
50 def test_absolute_uri_with_fragment
51 client = MockRequest.new("GET http://e:3/x#frag HTTP/1.1\r\n" \
53 env = @request.read(client)
54 assert_equal '/x', env['REQUEST_PATH']
55 assert_equal '/x', env['PATH_INFO']
56 assert_equal '', env['QUERY_STRING']
57 assert_equal 'frag', env['FRAGMENT']
61 def test_absolute_uri_with_query_and_fragment
62 client = MockRequest.new("GET http://e:3/x?a=b#frag HTTP/1.1\r\n" \
64 env = @request.read(client)
65 assert_equal '/x', env['REQUEST_PATH']
66 assert_equal '/x', env['PATH_INFO']
67 assert_equal 'a=b', env['QUERY_STRING']
68 assert_equal 'frag', env['FRAGMENT']
72 def test_absolute_uri_unsupported_schemes
73 %w(ssh+http://e/ ftp://e/x http+ssh://e/x).each do |abs_uri|
74 client = MockRequest.new("GET #{abs_uri} HTTP/1.1\r\n" \
76 assert_raises(HttpParserError) { @request.read(client) }
80 def test_x_forwarded_proto_https
81 client = MockRequest.new("GET / HTTP/1.1\r\n" \
82 "X-Forwarded-Proto: https\r\n" \
84 env = @request.read(client)
85 assert_equal "https", env['rack.url_scheme']
89 def test_x_forwarded_proto_http
90 client = MockRequest.new("GET / HTTP/1.1\r\n" \
91 "X-Forwarded-Proto: http\r\n" \
93 env = @request.read(client)
94 assert_equal "http", env['rack.url_scheme']
98 def test_x_forwarded_proto_invalid
99 client = MockRequest.new("GET / HTTP/1.1\r\n" \
100 "X-Forwarded-Proto: ftp\r\n" \
102 env = @request.read(client)
103 assert_equal "http", env['rack.url_scheme']
104 res = @lint.call(env)
107 def test_rack_lint_get
108 client = MockRequest.new("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")
109 env = @request.read(client)
110 assert_equal "http", env['rack.url_scheme']
111 assert_equal '127.0.0.1', env['REMOTE_ADDR']
112 res = @lint.call(env)
115 def test_no_content_stringio
116 client = MockRequest.new("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")
117 env = @request.read(client)
118 assert_equal StringIO, env['rack.input'].class
121 def test_zero_content_stringio
122 client = MockRequest.new("PUT / HTTP/1.1\r\n" \
123 "Content-Length: 0\r\n" \
125 env = @request.read(client)
126 assert_equal StringIO, env['rack.input'].class
129 def test_real_content_not_stringio
130 client = MockRequest.new("PUT / HTTP/1.1\r\n" \
131 "Content-Length: 1\r\n" \
133 env = @request.read(client)
134 assert_equal Unicorn::TeeInput, env['rack.input'].class
137 def test_rack_lint_put
138 client = MockRequest.new(
139 "PUT / HTTP/1.1\r\n" \
141 "Content-Length: 5\r\n" \
144 env = @request.read(client)
145 assert ! env.include?(:http_body)
146 res = @lint.call(env)
149 def test_rack_lint_big_put
152 buf = (' ' * bs).freeze
154 client = Tempfile.new('big_put')
155 def client.kgio_addr; '127.0.0.1'; end
156 def client.kgio_read(*args)
160 def client.kgio_read!(*args)
164 "PUT / HTTP/1.1\r\n" \
166 "Content-Length: #{length}\r\n" \
168 count.times { assert_equal bs, client.syswrite(buf) }
169 assert_equal 0, client.sysseek(0)
170 env = @request.read(client)
171 assert ! env.include?(:http_body)
172 assert_equal length, env['rack.input'].size
174 tmp = env['rack.input'].read(bs)
175 tmp << env['rack.input'].read(bs - tmp.size) if tmp.size != bs
176 assert_equal buf, tmp
178 assert_nil env['rack.input'].read(bs)
179 env['rack.input'].rewind
180 res = @lint.call(env)