tests: remove assert_nothing_raised (part 2)
[unicorn.git] / test / unit / test_tee_input.rb
blob0c2c9413f593b56e0e9fca621d340aca7e669e0c
1 # -*- encoding: binary -*-
3 require 'test/unit'
4 require 'digest/sha1'
5 require 'unicorn'
7 class TeeInput < Unicorn::TeeInput
8   attr_accessor :tmp, :len
9 end
11 class TestTeeInput < Test::Unit::TestCase
13   def setup
14     @rs = $/
15     @rd, @wr = Kgio::UNIXSocket.pair
16     @rd.sync = @wr.sync = true
17     @start_pid = $$
18   end
20   def teardown
21     return if $$ != @start_pid
22     $/ = @rs
23     @rd.close rescue nil
24     @wr.close rescue nil
25     begin
26       Process.wait
27     rescue Errno::ECHILD
28       break
29     end while true
30   end
32   def test_gets_long
33     r = init_request("hello", 5 + (4096 * 4 * 3) + "#$/foo#$/".size)
34     ti = TeeInput.new(@rd, r)
35     status = line = nil
36     pid = fork {
37       @rd.close
38       3.times { @wr.write("ffff" * 4096) }
39       @wr.write "#$/foo#$/"
40       @wr.close
41     }
42     @wr.close
43     line = ti.gets
44     assert_equal(4096 * 4 * 3 + 5 + $/.size, line.size)
45     assert_equal("hello" << ("ffff" * 4096 * 3) << "#$/", line)
46     line = ti.gets
47     assert_equal "foo#$/", line
48     assert_nil ti.gets
49     pid, status = Process.waitpid2(pid)
50     assert status.success?
51   end
53   def test_gets_short
54     r = init_request("hello", 5 + "#$/foo".size)
55     ti = TeeInput.new(@rd, r)
56     status = line = nil
57     pid = fork {
58       @rd.close
59       @wr.write "#$/foo"
60       @wr.close
61     }
62     @wr.close
63     line = ti.gets
64     assert_equal("hello#$/", line)
65     line = ti.gets
66     assert_equal "foo", line
67     assert_nil ti.gets
68     pid, status = Process.waitpid2(pid)
69     assert status.success?
70   end
72   def test_small_body
73     r = init_request('hello')
74     ti = TeeInput.new(@rd, r)
75     assert_equal 0, @parser.content_length
76     assert @parser.body_eof?
77     assert_equal StringIO, ti.tmp.class
78     assert_equal 0, ti.tmp.pos
79     assert_equal 5, ti.size
80     assert_equal 'hello', ti.read
81     assert_equal '', ti.read
82     assert_nil ti.read(4096)
83     assert_equal 5, ti.size
84   end
86   def test_read_with_buffer
87     r = init_request('hello')
88     ti = TeeInput.new(@rd, r)
89     buf = ''
90     rv = ti.read(4, buf)
91     assert_equal 'hell', rv
92     assert_equal 'hell', buf
93     assert_equal rv.object_id, buf.object_id
94     assert_equal 'o', ti.read
95     assert_equal nil, ti.read(5, buf)
96     assert_equal 0, ti.rewind
97     assert_equal 'hello', ti.read(5, buf)
98     assert_equal 'hello', buf
99   end
101   def test_big_body
102     r = init_request('.' * Unicorn::Const::MAX_BODY << 'a')
103     ti = TeeInput.new(@rd, r)
104     assert_equal 0, @parser.content_length
105     assert @parser.body_eof?
106     assert_kind_of File, ti.tmp
107     assert_equal 0, ti.tmp.pos
108     assert_equal Unicorn::Const::MAX_BODY + 1, ti.size
109   end
111   def test_read_in_full_if_content_length
112     a, b = 300, 3
113     r = init_request('.' * b, 300)
114     assert_equal 300, @parser.content_length
115     ti = TeeInput.new(@rd, r)
116     pid = fork {
117       @wr.write('.' * 197)
118       sleep 1 # still a *potential* race here that would make the test moot...
119       @wr.write('.' * 100)
120     }
121     assert_equal a, ti.read(a).size
122     _, status = Process.waitpid2(pid)
123     assert status.success?
124     @wr.close
125   end
127   def test_big_body_multi
128     r = init_request('.', Unicorn::Const::MAX_BODY + 1)
129     ti = TeeInput.new(@rd, r)
130     assert_equal Unicorn::Const::MAX_BODY, @parser.content_length
131     assert ! @parser.body_eof?
132     assert_kind_of File, ti.tmp
133     assert_equal 0, ti.tmp.pos
134     assert_equal Unicorn::Const::MAX_BODY + 1, ti.size
135     nr = Unicorn::Const::MAX_BODY / 4
136     pid = fork {
137       @rd.close
138       nr.times { @wr.write('....') }
139       @wr.close
140     }
141     @wr.close
142     assert_equal '.', ti.read(1)
143     assert_equal Unicorn::Const::MAX_BODY + 1, ti.size
144     nr.times { |x|
145       assert_equal '....', ti.read(4), "nr=#{x}"
146       assert_equal Unicorn::Const::MAX_BODY + 1, ti.size
147     }
148     assert_nil ti.read(1)
149     pid, status = Process.waitpid2(pid)
150     assert status.success?
151   end
153   def test_chunked
154     @parser = Unicorn::HttpParser.new
155     @parser.buf << "POST / HTTP/1.1\r\n" \
156                    "Host: localhost\r\n" \
157                    "Transfer-Encoding: chunked\r\n" \
158                    "\r\n"
159     assert @parser.parse
160     assert_equal "", @parser.buf
162     pid = fork {
163       @rd.close
164       5.times { @wr.write("5\r\nabcde\r\n") }
165       @wr.write("0\r\n\r\n")
166     }
167     @wr.close
168     ti = TeeInput.new(@rd, @parser)
169     assert_nil @parser.content_length
170     assert_nil ti.len
171     assert ! @parser.body_eof?
172     assert_equal 25, ti.size
173     assert @parser.body_eof?
174     assert_equal 25, ti.len
175     assert_equal 0, ti.tmp.pos
176     ti.rewind
177     assert_equal 0, ti.tmp.pos
178     assert_equal 'abcdeabcdeabcdeabcde', ti.read(20)
179     assert_equal 20, ti.tmp.pos
180     ti.rewind
181     assert_equal 0, ti.tmp.pos
182     assert_kind_of File, ti.tmp
183     status = nil
184     pid, status = Process.waitpid2(pid)
185     assert status.success?
186   end
188   def test_chunked_ping_pong
189     @parser = Unicorn::HttpParser.new
190     buf = @parser.buf
191     buf << "POST / HTTP/1.1\r\n" \
192            "Host: localhost\r\n" \
193            "Transfer-Encoding: chunked\r\n" \
194            "\r\n"
195     assert @parser.parse
196     assert_equal "", buf
197     chunks = %w(aa bbb cccc dddd eeee)
198     rd, wr = IO.pipe
200     pid = fork {
201       chunks.each do |chunk|
202         rd.read(1) == "." and
203           @wr.write("#{'%x' % [ chunk.size]}\r\n#{chunk}\r\n")
204       end
205       @wr.write("0\r\n\r\n")
206     }
207     ti = TeeInput.new(@rd, @parser)
208     assert_nil @parser.content_length
209     assert_nil ti.len
210     assert ! @parser.body_eof?
211     chunks.each do |chunk|
212       wr.write('.')
213       assert_equal chunk, ti.read(16384)
214     end
215     _, status = Process.waitpid2(pid)
216     assert status.success?
217   end
219   def test_chunked_with_trailer
220     @parser = Unicorn::HttpParser.new
221     buf = @parser.buf
222     buf << "POST / HTTP/1.1\r\n" \
223            "Host: localhost\r\n" \
224            "Trailer: Hello\r\n" \
225            "Transfer-Encoding: chunked\r\n" \
226            "\r\n"
227     assert @parser.parse
228     assert_equal "", buf
230     pid = fork {
231       @rd.close
232       5.times { @wr.write("5\r\nabcde\r\n") }
233       @wr.write("0\r\n")
234       @wr.write("Hello: World\r\n\r\n")
235     }
236     @wr.close
237     ti = TeeInput.new(@rd, @parser)
238     assert_nil @parser.content_length
239     assert_nil ti.len
240     assert ! @parser.body_eof?
241     assert_equal 25, ti.size
242     assert_equal "World", @parser.env['HTTP_HELLO']
243     pid, status = Process.waitpid2(pid)
244     assert status.success?
245   end
247   def test_chunked_and_size_slow
248     @parser = Unicorn::HttpParser.new
249     buf = @parser.buf
250     buf << "POST / HTTP/1.1\r\n" \
251            "Host: localhost\r\n" \
252            "Trailer: Hello\r\n" \
253            "Transfer-Encoding: chunked\r\n" \
254            "\r\n"
255     assert @parser.parse
256     assert_equal "", buf
258     @wr.write("9\r\nabcde")
259     ti = TeeInput.new(@rd, @parser)
260     assert_nil @parser.content_length
261     assert_equal "abcde", ti.read(9)
262     assert ! @parser.body_eof?
263     @wr.write("fghi\r\n0\r\nHello: World\r\n\r\n")
264     assert_equal 9, ti.size
265     assert_equal "fghi", ti.read(9)
266     assert_equal nil, ti.read(9)
267     assert_equal "World", @parser.env['HTTP_HELLO']
268   end
270   def test_gets_read_mix
271     r = init_request("hello\nasdfasdf")
272     ti = Unicorn::TeeInput.new(@rd, r)
273     assert_equal "hello\n", ti.gets
274     assert_equal "asdfasdf", ti.read(9)
275     assert_nil ti.read(9)
276   end
278 private
280   def init_request(body, size = nil)
281     @parser = Unicorn::HttpParser.new
282     body = body.to_s.freeze
283     buf = @parser.buf
284     buf << "POST / HTTP/1.1\r\n" \
285            "Host: localhost\r\n" \
286            "Content-Length: #{size || body.size}\r\n" \
287            "\r\n#{body}"
288     assert @parser.parse
289     assert_equal body, buf
290     @buf = buf
291     @parser
292   end