set env["rack.logger"] for applications
[unicorn.git] / test / unit / test_server.rb
blob00705d07f978a78df6567f7934f0563e8049dd6a
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.
6 # Additional work donated by contributors.  See http://mongrel.rubyforge.org/attributions.html
7 # for more information.
9 require 'test/test_helper'
11 include Unicorn
13 class TestHandler 
15   def call(env)
16     while env['rack.input'].read(4096)
17     end
18     [200, { 'Content-Type' => 'text/plain' }, ['hello!\n']]
19     rescue Unicorn::ClientShutdown, Unicorn::HttpParserError => e
20       $stderr.syswrite("#{e.class}: #{e.message} #{e.backtrace.empty?}\n")
21       raise e
22    end
23 end
26 class WebServerTest < Test::Unit::TestCase
28   def setup
29     @valid_request = "GET / HTTP/1.1\r\nHost: www.zedshaw.com\r\nContent-Type: text/plain\r\n\r\n"
30     @port = unused_port
31     @tester = TestHandler.new
32     redirect_test_io do
33       @server = HttpServer.new(@tester, :listeners => [ "127.0.0.1:#{@port}" ] )
34       @server.start
35     end
36   end
38   def teardown
39     redirect_test_io do
40       wait_workers_ready("test_stderr.#$$.log", 1)
41       File.truncate("test_stderr.#$$.log", 0)
42       @server.stop(true)
43     end
44   end
46   def test_preload_app_config
47     teardown
48     tmp = Tempfile.new('test_preload_app_config')
49     ObjectSpace.undefine_finalizer(tmp)
50     app = lambda { ||
51       tmp.sysseek(0)
52       tmp.truncate(0)
53       tmp.syswrite($$)
54       lambda { |env| [ 200, { 'Content-Type' => 'text/plain' }, [ "#$$\n" ] ] }
55     }
56     redirect_test_io do
57       @server = HttpServer.new(app, :listeners => [ "127.0.0.1:#@port"] )
58       @server.start
59     end
60     results = hit(["http://localhost:#@port/"])
61     worker_pid = results[0].to_i
62     assert worker_pid != 0
63     tmp.sysseek(0)
64     loader_pid = tmp.sysread(4096).to_i
65     assert loader_pid != 0
66     assert_equal worker_pid, loader_pid
67     teardown
69     redirect_test_io do
70       @server = HttpServer.new(app, :listeners => [ "127.0.0.1:#@port"],
71                                :preload_app => true)
72       @server.start
73     end
74     results = hit(["http://localhost:#@port/"])
75     worker_pid = results[0].to_i
76     assert worker_pid != 0
77     tmp.sysseek(0)
78     loader_pid = tmp.sysread(4096).to_i
79     assert_equal $$, loader_pid
80     assert worker_pid != loader_pid
81     ensure
82       tmp.close!
83   end
85   def test_broken_app
86     teardown
87     app = lambda { |env| raise RuntimeError, "hello" }
88     # [200, {}, []] }
89     redirect_test_io do
90       @server = HttpServer.new(app, :listeners => [ "127.0.0.1:#@port"] )
91       @server.start
92     end
93     sock = nil
94     assert_nothing_raised do
95       sock = TCPSocket.new('127.0.0.1', @port)
96       sock.syswrite("GET / HTTP/1.0\r\n\r\n")
97     end
99     assert_match %r{\AHTTP/1.[01] 500\b}, sock.sysread(4096)
100     assert_nothing_raised { sock.close }
101   end
103   def test_simple_server
104     results = hit(["http://localhost:#{@port}/test"])
105     assert_equal 'hello!\n', results[0], "Handler didn't really run"
106   end
108   def test_client_shutdown_writes
109     sock = nil
110     buf = nil
111     bs = 15609315 * rand
112     assert_nothing_raised do
113       sock = TCPSocket.new('127.0.0.1', @port)
114       sock.syswrite("PUT /hello HTTP/1.1\r\n")
115       sock.syswrite("Host: example.com\r\n")
116       sock.syswrite("Transfer-Encoding: chunked\r\n")
117       sock.syswrite("Trailer: X-Foo\r\n")
118       sock.syswrite("\r\n")
119       sock.syswrite("%x\r\n" % [ bs ])
120       sock.syswrite("F" * bs)
121       sock.syswrite("\r\n0\r\nX-")
122       "Foo: bar\r\n\r\n".each_byte do |x|
123         sock.syswrite x.chr
124         sleep 0.05
125       end
126       # we wrote the entire request before shutting down, server should
127       # continue to process our request and never hit EOFError on our sock
128       sock.shutdown(Socket::SHUT_WR)
129       buf = sock.read
130     end
131     assert_equal 'hello!\n', buf.split(/\r\n\r\n/).last
132     next_client = Net::HTTP.get(URI.parse("http://127.0.0.1:#@port/"))
133     assert_equal 'hello!\n', next_client
134     lines = File.readlines("test_stderr.#$$.log")
135     assert lines.grep(/^Unicorn::ClientShutdown: /).empty?
136     assert_nothing_raised { sock.close }
137   end
139   def test_client_shutdown_write_truncates
140     sock = nil
141     buf = nil
142     bs = 15609315 * rand
143     assert_nothing_raised do
144       sock = TCPSocket.new('127.0.0.1', @port)
145       sock.syswrite("PUT /hello HTTP/1.1\r\n")
146       sock.syswrite("Host: example.com\r\n")
147       sock.syswrite("Transfer-Encoding: chunked\r\n")
148       sock.syswrite("Trailer: X-Foo\r\n")
149       sock.syswrite("\r\n")
150       sock.syswrite("%x\r\n" % [ bs ])
151       sock.syswrite("F" * (bs / 2.0))
153       # shutdown prematurely, this will force the server to abort
154       # processing on us even during app dispatch
155       sock.shutdown(Socket::SHUT_WR)
156       IO.select([sock], nil, nil, 60) or raise "Timed out"
157       buf = sock.read
158     end
159     assert_equal "", buf
160     next_client = Net::HTTP.get(URI.parse("http://127.0.0.1:#@port/"))
161     assert_equal 'hello!\n', next_client
162     lines = File.readlines("test_stderr.#$$.log")
163     lines = lines.grep(/^Unicorn::ClientShutdown: bytes_read=\d+/)
164     assert_equal 1, lines.size
165     assert_match %r{\AUnicorn::ClientShutdown: bytes_read=\d+ true$}, lines[0]
166     assert_nothing_raised { sock.close }
167   end
169   def test_client_malformed_body
170     sock = nil
171     buf = nil
172     bs = 15653984
173     assert_nothing_raised do
174       sock = TCPSocket.new('127.0.0.1', @port)
175       sock.syswrite("PUT /hello HTTP/1.1\r\n")
176       sock.syswrite("Host: example.com\r\n")
177       sock.syswrite("Transfer-Encoding: chunked\r\n")
178       sock.syswrite("Trailer: X-Foo\r\n")
179       sock.syswrite("\r\n")
180       sock.syswrite("%x\r\n" % [ bs ])
181       sock.syswrite("F" * bs)
182     end
183     begin
184       File.open("/dev/urandom", "rb") { |fp| sock.syswrite(fp.sysread(16384)) }
185     rescue
186     end
187     assert_nothing_raised { sock.close }
188     next_client = Net::HTTP.get(URI.parse("http://127.0.0.1:#@port/"))
189     assert_equal 'hello!\n', next_client
190     lines = File.readlines("test_stderr.#$$.log")
191     lines = lines.grep(/^Unicorn::HttpParserError: .* true$/)
192     assert_equal 1, lines.size
193   end
195   def do_test(string, chunk, close_after=nil, shutdown_delay=0)
196     # Do not use instance variables here, because it needs to be thread safe
197     socket = TCPSocket.new("127.0.0.1", @port);
198     request = StringIO.new(string)
199     chunks_out = 0
201     while data = request.read(chunk)
202       chunks_out += socket.write(data)
203       socket.flush
204       sleep 0.2
205       if close_after and chunks_out > close_after
206         socket.close
207         sleep 1
208       end
209     end
210     sleep(shutdown_delay)
211     socket.write(" ") # Some platforms only raise the exception on attempted write
212     socket.flush
213   end
215   def test_trickle_attack
216     do_test(@valid_request, 3)
217   end
219   def test_close_client
220     assert_raises IOError do
221       do_test(@valid_request, 10, 20)
222     end
223   end
225   def test_bad_client
226     redirect_test_io do
227       do_test("GET /test HTTP/BAD", 3)
228     end
229   end
231   def test_logger_set
232     assert_equal @server.logger, Unicorn::HttpRequest::DEFAULTS["rack.logger"]
233   end
235   def test_logger_changed
236     tmp = Logger.new($stdout)
237     @server.logger = tmp
238     assert_equal tmp, Unicorn::HttpRequest::DEFAULTS["rack.logger"]
239   end
241   def test_bad_client_400
242     sock = nil
243     assert_nothing_raised do
244       sock = TCPSocket.new('127.0.0.1', @port)
245       sock.syswrite("GET / HTTP/1.0\r\nHost: foo\rbar\r\n\r\n")
246     end
247     assert_match %r{\AHTTP/1.[01] 400\b}, sock.sysread(4096)
248     assert_nothing_raised { sock.close }
249   end
251   def test_http_0_9
252     sock = nil
253     assert_nothing_raised do
254       sock = TCPSocket.new('127.0.0.1', @port)
255       sock.syswrite("GET /hello\r\n")
256     end
257     assert_match 'hello!\n', sock.sysread(4096)
258     assert_nothing_raised { sock.close }
259   end
261   def test_header_is_too_long
262     redirect_test_io do
263       long = "GET /test HTTP/1.1\r\n" + ("X-Big: stuff\r\n" * 15000) + "\r\n"
264       assert_raises Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EINVAL, IOError do
265         do_test(long, long.length/2, 10)
266       end
267     end
268   end
270   def test_file_streamed_request
271     body = "a" * (Unicorn::Const::MAX_BODY * 2)
272     long = "PUT /test HTTP/1.1\r\nContent-length: #{body.length}\r\n\r\n" + body
273     do_test(long, Unicorn::Const::CHUNK_SIZE * 2 -400)
274   end
276   def test_file_streamed_request_bad_body
277     body = "a" * (Unicorn::Const::MAX_BODY * 2)
278     long = "GET /test HTTP/1.1\r\nContent-ength: #{body.length}\r\n\r\n" + body
279     assert_raises(EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,
280                   Errno::EBADF) {
281       do_test(long, Unicorn::Const::CHUNK_SIZE * 2 -400)
282     }
283   end