96eb268836e74a4ebe1ac55a37058820e6dcd861
[unicorn.git] / test / unit / test_tee_input.rb
blob96eb268836e74a4ebe1ac55a37058820e6dcd861
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     assert_nothing_raised { line = ti.gets }
44     assert_equal(4096 * 4 * 3 + 5 + $/.size, line.size)
45     assert_equal("hello" << ("ffff" * 4096 * 3) << "#$/", line)
46     assert_nothing_raised { line = ti.gets }
47     assert_equal "foo#$/", line
48     assert_nil ti.gets
49     assert_nothing_raised { 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     assert_nothing_raised { line = ti.gets }
64     assert_equal("hello#$/", line)
65     assert_nothing_raised { line = ti.gets }
66     assert_equal "foo", line
67     assert_nil ti.gets
68     assert_nothing_raised { 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     status = nil
150     assert_nothing_raised { pid, status = Process.waitpid2(pid) }
151     assert status.success?
152   end
154   def test_chunked
155     @parser = Unicorn::HttpParser.new
156     @parser.buf << "POST / HTTP/1.1\r\n" \
157                    "Host: localhost\r\n" \
158                    "Transfer-Encoding: chunked\r\n" \
159                    "\r\n"
160     assert @parser.parse
161     assert_equal "", @parser.buf
163     pid = fork {
164       @rd.close
165       5.times { @wr.write("5\r\nabcde\r\n") }
166       @wr.write("0\r\n\r\n")
167     }
168     @wr.close
169     ti = TeeInput.new(@rd, @parser)
170     assert_nil @parser.content_length
171     assert_nil ti.len
172     assert ! @parser.body_eof?
173     assert_equal 25, ti.size
174     assert @parser.body_eof?
175     assert_equal 25, ti.len
176     assert_equal 0, ti.tmp.pos
177     assert_nothing_raised { ti.rewind }
178     assert_equal 0, ti.tmp.pos
179     assert_equal 'abcdeabcdeabcdeabcde', ti.read(20)
180     assert_equal 20, ti.tmp.pos
181     assert_nothing_raised { ti.rewind }
182     assert_equal 0, ti.tmp.pos
183     assert_kind_of File, ti.tmp
184     status = nil
185     assert_nothing_raised { pid, status = Process.waitpid2(pid) }
186     assert status.success?
187   end
189   def test_chunked_ping_pong
190     @parser = Unicorn::HttpParser.new
191     buf = @parser.buf
192     buf << "POST / HTTP/1.1\r\n" \
193            "Host: localhost\r\n" \
194            "Transfer-Encoding: chunked\r\n" \
195            "\r\n"
196     assert @parser.parse
197     assert_equal "", buf
198     chunks = %w(aa bbb cccc dddd eeee)
199     rd, wr = IO.pipe
201     pid = fork {
202       chunks.each do |chunk|
203         rd.read(1) == "." and
204           @wr.write("#{'%x' % [ chunk.size]}\r\n#{chunk}\r\n")
205       end
206       @wr.write("0\r\n\r\n")
207     }
208     ti = TeeInput.new(@rd, @parser)
209     assert_nil @parser.content_length
210     assert_nil ti.len
211     assert ! @parser.body_eof?
212     chunks.each do |chunk|
213       wr.write('.')
214       assert_equal chunk, ti.read(16384)
215     end
216     _, status = Process.waitpid2(pid)
217     assert status.success?
218   end
220   def test_chunked_with_trailer
221     @parser = Unicorn::HttpParser.new
222     buf = @parser.buf
223     buf << "POST / HTTP/1.1\r\n" \
224            "Host: localhost\r\n" \
225            "Trailer: Hello\r\n" \
226            "Transfer-Encoding: chunked\r\n" \
227            "\r\n"
228     assert @parser.parse
229     assert_equal "", buf
231     pid = fork {
232       @rd.close
233       5.times { @wr.write("5\r\nabcde\r\n") }
234       @wr.write("0\r\n")
235       @wr.write("Hello: World\r\n\r\n")
236     }
237     @wr.close
238     ti = TeeInput.new(@rd, @parser)
239     assert_nil @parser.content_length
240     assert_nil ti.len
241     assert ! @parser.body_eof?
242     assert_equal 25, ti.size
243     assert_equal "World", @parser.env['HTTP_HELLO']
244     status = nil
245     assert_nothing_raised { pid, status = Process.waitpid2(pid) }
246     assert status.success?
247   end
249   def test_chunked_and_size_slow
250     @parser = Unicorn::HttpParser.new
251     buf = @parser.buf
252     buf << "POST / HTTP/1.1\r\n" \
253            "Host: localhost\r\n" \
254            "Trailer: Hello\r\n" \
255            "Transfer-Encoding: chunked\r\n" \
256            "\r\n"
257     assert @parser.parse
258     assert_equal "", buf
260     @wr.write("9\r\nabcde")
261     ti = TeeInput.new(@rd, @parser)
262     assert_nil @parser.content_length
263     assert_equal "abcde", ti.read(9)
264     assert ! @parser.body_eof?
265     @wr.write("fghi\r\n0\r\nHello: World\r\n\r\n")
266     assert_equal 9, ti.size
267     assert_equal "fghi", ti.read(9)
268     assert_equal nil, ti.read(9)
269     assert_equal "World", @parser.env['HTTP_HELLO']
270   end
272   def test_gets_read_mix
273     r = init_request("hello\nasdfasdf")
274     ti = Unicorn::TeeInput.new(@rd, r)
275     assert_equal "hello\n", ti.gets
276     assert_equal "asdfasdf", ti.read(9)
277     assert_nil ti.read(9)
278   end
280 private
282   def init_request(body, size = nil)
283     @parser = Unicorn::HttpParser.new
284     body = body.to_s.freeze
285     buf = @parser.buf
286     buf << "POST / HTTP/1.1\r\n" \
287            "Host: localhost\r\n" \
288            "Content-Length: #{size || body.size}\r\n" \
289            "\r\n#{body}"
290     assert @parser.parse
291     assert_equal body, buf
292     @buf = buf
293     @parser
294   end