1 # -*- encoding: binary -*-
3 # Copyright (c) 2005 Zed A. Shaw
4 # You can redistribute it and/or modify it under the same terms as Ruby 1.8 or
5 # the GPLv2+ (GPLv3+ preferred)
7 # Additional work donated by contributors. See git history
8 # for more information.
10 require './test/test_helper'
17 while env['rack.input'].read(4096)
19 [200, { 'Content-Type' => 'text/plain' }, ['hello!\n']]
20 rescue Unicorn::ClientShutdown, Unicorn::HttpParserError => e
21 $stderr.syswrite("#{e.class}: #{e.message} #{e.backtrace.empty?}\n")
26 class TestEarlyHintsHandler
28 while env['rack.input'].read(4096)
30 env['rack.early_hints'].call(
31 "Link" => "</style.css>; rel=preload; as=style\n</script.js>; rel=preload"
33 [200, { 'Content-Type' => 'text/plain' }, ['hello!\n']]
37 class WebServerTest < Test::Unit::TestCase
40 @valid_request = "GET / HTTP/1.1\r\nHost: www.zedshaw.com\r\nContent-Type: text/plain\r\n\r\n"
42 @tester = TestHandler.new
44 @server = HttpServer.new(@tester, :listeners => [ "127.0.0.1:#{@port}" ] )
51 wait_workers_ready("test_stderr.#$$.log", 1)
52 File.truncate("test_stderr.#$$.log", 0)
58 def test_preload_app_config
60 tmp = Tempfile.new('test_preload_app_config')
61 ObjectSpace.undefine_finalizer(tmp)
66 lambda { |env| [ 200, { 'Content-Type' => 'text/plain' }, [ "#$$\n" ] ] }
69 @server = HttpServer.new(app, :listeners => [ "127.0.0.1:#@port"] )
72 results = hit(["http://localhost:#@port/"])
73 worker_pid = results[0].to_i
74 assert worker_pid != 0
76 loader_pid = tmp.sysread(4096).to_i
77 assert loader_pid != 0
78 assert_equal worker_pid, loader_pid
82 @server = HttpServer.new(app, :listeners => [ "127.0.0.1:#@port"],
86 results = hit(["http://localhost:#@port/"])
87 worker_pid = results[0].to_i
88 assert worker_pid != 0
90 loader_pid = tmp.sysread(4096).to_i
91 assert_equal $$, loader_pid
92 assert worker_pid != loader_pid
100 @server = HttpServer.new(TestEarlyHintsHandler.new,
101 :listeners => [ "127.0.0.1:#@port"],
102 :early_hints => true)
106 sock = TCPSocket.new('127.0.0.1', @port)
107 sock.syswrite("GET / HTTP/1.0\r\n\r\n")
109 responses = sock.read(4096)
110 assert_match %r{\AHTTP/1.[01] 103\b}, responses
111 assert_match %r{^Link: </style\.css>}, responses
112 assert_match %r{^Link: </script\.js>}, responses
114 assert_match %r{^HTTP/1.[01] 200\b}, responses
119 app = lambda { |env| raise RuntimeError, "hello" }
122 @server = HttpServer.new(app, :listeners => [ "127.0.0.1:#@port"] )
125 sock = TCPSocket.new('127.0.0.1', @port)
126 sock.syswrite("GET / HTTP/1.0\r\n\r\n")
127 assert_match %r{\AHTTP/1.[01] 500\b}, sock.sysread(4096)
128 assert_nil sock.close
131 def test_simple_server
132 results = hit(["http://localhost:#{@port}/test"])
133 assert_equal 'hello!\n', results[0], "Handler didn't really run"
136 def test_client_shutdown_writes
138 sock = TCPSocket.new('127.0.0.1', @port)
139 sock.syswrite("PUT /hello HTTP/1.1\r\n")
140 sock.syswrite("Host: example.com\r\n")
141 sock.syswrite("Transfer-Encoding: chunked\r\n")
142 sock.syswrite("Trailer: X-Foo\r\n")
143 sock.syswrite("\r\n")
144 sock.syswrite("%x\r\n" % [ bs ])
145 sock.syswrite("F" * bs)
146 sock.syswrite("\r\n0\r\nX-")
147 "Foo: bar\r\n\r\n".each_byte do |x|
151 # we wrote the entire request before shutting down, server should
152 # continue to process our request and never hit EOFError on our sock
153 sock.shutdown(Socket::SHUT_WR)
155 assert_equal 'hello!\n', buf.split(/\r\n\r\n/).last
156 next_client = Net::HTTP.get(URI.parse("http://127.0.0.1:#@port/"))
157 assert_equal 'hello!\n', next_client
158 lines = File.readlines("test_stderr.#$$.log")
159 assert lines.grep(/^Unicorn::ClientShutdown: /).empty?
160 assert_nil sock.close
163 def test_client_shutdown_write_truncates
165 sock = TCPSocket.new('127.0.0.1', @port)
166 sock.syswrite("PUT /hello HTTP/1.1\r\n")
167 sock.syswrite("Host: example.com\r\n")
168 sock.syswrite("Transfer-Encoding: chunked\r\n")
169 sock.syswrite("Trailer: X-Foo\r\n")
170 sock.syswrite("\r\n")
171 sock.syswrite("%x\r\n" % [ bs ])
172 sock.syswrite("F" * (bs / 2.0))
174 # shutdown prematurely, this will force the server to abort
175 # processing on us even during app dispatch
176 sock.shutdown(Socket::SHUT_WR)
177 IO.select([sock], nil, nil, 60) or raise "Timed out"
180 next_client = Net::HTTP.get(URI.parse("http://127.0.0.1:#@port/"))
181 assert_equal 'hello!\n', next_client
182 lines = File.readlines("test_stderr.#$$.log")
183 lines = lines.grep(/^Unicorn::ClientShutdown: bytes_read=\d+/)
184 assert_equal 1, lines.size
185 assert_match %r{\AUnicorn::ClientShutdown: bytes_read=\d+ true$}, lines[0]
186 assert_nil sock.close
189 def test_client_malformed_body
191 sock = TCPSocket.new('127.0.0.1', @port)
192 sock.syswrite("PUT /hello HTTP/1.1\r\n")
193 sock.syswrite("Host: example.com\r\n")
194 sock.syswrite("Transfer-Encoding: chunked\r\n")
195 sock.syswrite("Trailer: X-Foo\r\n")
196 sock.syswrite("\r\n")
197 sock.syswrite("%x\r\n" % [ bs ])
198 sock.syswrite("F" * bs)
200 File.open("/dev/urandom", "rb") { |fp| sock.syswrite(fp.sysread(16384)) }
203 assert_nil sock.close
204 next_client = Net::HTTP.get(URI.parse("http://127.0.0.1:#@port/"))
205 assert_equal 'hello!\n', next_client
206 lines = File.readlines("test_stderr.#$$.log")
207 lines = lines.grep(/^Unicorn::HttpParserError: .* true$/)
208 assert_equal 1, lines.size
211 def do_test(string, chunk, close_after=nil, shutdown_delay=0)
212 # Do not use instance variables here, because it needs to be thread safe
213 socket = TCPSocket.new("127.0.0.1", @port);
214 request = StringIO.new(string)
217 while data = request.read(chunk)
218 chunks_out += socket.write(data)
221 if close_after and chunks_out > close_after
226 sleep(shutdown_delay)
227 socket.write(" ") # Some platforms only raise the exception on attempted write
231 def test_trickle_attack
232 do_test(@valid_request, 3)
235 def test_close_client
236 assert_raises IOError do
237 do_test(@valid_request, 10, 20)
243 do_test("GET /test HTTP/BAD", 3)
248 assert_equal @server.logger, Unicorn::HttpRequest::DEFAULTS["rack.logger"]
251 def test_logger_changed
252 tmp = Logger.new($stdout)
254 assert_equal tmp, Unicorn::HttpRequest::DEFAULTS["rack.logger"]
257 def test_bad_client_400
258 sock = TCPSocket.new('127.0.0.1', @port)
259 sock.syswrite("GET / HTTP/1.0\r\nHost: foo\rbar\r\n\r\n")
260 assert_match %r{\AHTTP/1.[01] 400\b}, sock.sysread(4096)
261 assert_nil sock.close
265 sock = TCPSocket.new('127.0.0.1', @port)
266 sock.syswrite("GET /hello\r\n")
267 assert_match 'hello!\n', sock.sysread(4096)
268 assert_nil sock.close
271 def test_header_is_too_long
273 long = "GET /test HTTP/1.1\r\n" + ("X-Big: stuff\r\n" * 15000) + "\r\n"
274 assert_raises Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EINVAL, IOError do
275 do_test(long, long.length/2, 10)
280 def test_file_streamed_request
281 body = "a" * (Unicorn::Const::MAX_BODY * 2)
282 long = "PUT /test HTTP/1.1\r\nContent-length: #{body.length}\r\n\r\n" + body
283 do_test(long, Unicorn::Const::CHUNK_SIZE * 2 - 400)
286 def test_file_streamed_request_bad_body
287 body = "a" * (Unicorn::Const::MAX_BODY * 2)
288 long = "GET /test HTTP/1.1\r\nContent-ength: #{body.length}\r\n\r\n" + body
289 assert_raises(EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,
291 do_test(long, Unicorn::Const::CHUNK_SIZE * 2 - 400)
295 def test_listener_names
296 assert_equal [ "127.0.0.1:#@port" ], Unicorn.listener_names