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