unicorn.git  about / heads / tags
Rack HTTP server for Unix and fast clients
blob 7986ca75d872a9f9eb2f9486291618326572273e 5560 bytes (raw)
$ git show HEAD:test/unit/test_stream_input.rb	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
 
# -*- encoding: binary -*-

require 'test/unit'
require 'digest/sha1'
require 'unicorn'

class TestStreamInput < Test::Unit::TestCase
  def setup
    @rs = "\n"
    $/ == "\n" or abort %q{test broken if \$/ != "\\n"}
    @env = {}
    @rd, @wr = UNIXSocket.pair
    @rd.sync = @wr.sync = true
    @start_pid = $$
  end

  def teardown
    return if $$ != @start_pid
    @rd.close rescue nil
    @wr.close rescue nil
    Process.waitall
  end

  def test_read_negative
    r = init_request('hello')
    si = Unicorn::StreamInput.new(@rd, r)
    assert_raises(ArgumentError) { si.read(-1) }
    assert_equal 'hello', si.read
  end

  def test_read_small
    r = init_request('hello')
    si = Unicorn::StreamInput.new(@rd, r)
    assert_equal 'hello', si.read
    assert_equal '', si.read
    assert_nil si.read(5)
    assert_nil si.gets
  end

  def test_gets_oneliner
    r = init_request('hello')
    si = Unicorn::StreamInput.new(@rd, r)
    assert_equal 'hello', si.gets
    assert_nil si.gets
  end

  def test_gets_multiline
    r = init_request("a\nb\n\n")
    si = Unicorn::StreamInput.new(@rd, r)
    assert_equal "a\n", si.gets
    assert_equal "b\n", si.gets
    assert_equal "\n", si.gets
    assert_nil si.gets
  end

  def test_gets_empty_rs
    r = init_request("a\nb\n\n")
    si = Unicorn::StreamInput.new(@rd, r)
    pid = fork do # to avoid $/ warning (hopefully)
      $/ = nil
      @rd.close
      @wr.write(si.gets)
      @wr.close
    end
    @wr.close
    assert_equal "a\nb\n\n", @rd.read
    pid, status = Process.waitpid2(pid)
    assert_predicate status, :success?
  end

  def test_read_with_equal_len
    r = init_request("abcde")
    si = Unicorn::StreamInput.new(@rd, r)
    assert_equal "abcde", si.read(5)
    assert_nil si.read(5)
  end

  def test_big_body_multi
    r = init_request('.', Unicorn::Const::MAX_BODY + 1)
    si = Unicorn::StreamInput.new(@rd, r)
    assert_equal Unicorn::Const::MAX_BODY, @parser.content_length
    assert ! @parser.body_eof?
    nr = Unicorn::Const::MAX_BODY / 4
    pid = fork {
      @rd.close
      nr.times { @wr.write('....') }
      @wr.close
    }
    @wr.close
    assert_equal '.', si.read(1)
    nr.times { |x|
      assert_equal '....', si.read(4), "nr=#{x}"
    }
    assert_nil si.read(1)
    pid, status = Process.waitpid2(pid)
    assert status.success?
  end

  def test_gets_long
    r = init_request("hello", 5 + (4096 * 4 * 3) + "#{@rs}foo#{@rs}".size)
    si = Unicorn::StreamInput.new(@rd, r)
    status = line = nil
    pid = fork {
      @rd.close
      3.times { @wr.write("ffff" * 4096) }
      @wr.write "#{@rs}foo#{@rs}"
      @wr.close
    }
    @wr.close
    line = si.gets
    assert_equal(4096 * 4 * 3 + 5 + $/.size, line.size)
    assert_equal("hello" << ("ffff" * 4096 * 3) << "#{@rs}", line)
    line = si.gets
    assert_equal "foo#{@rs}", line
    assert_nil si.gets
    pid, status = Process.waitpid2(pid)
    assert status.success?
  end

  def test_read_with_buffer
    r = init_request('hello')
    si = Unicorn::StreamInput.new(@rd, r)
    buf = ''
    rv = si.read(4, buf)
    assert_equal 'hell', rv
    assert_equal 'hell', buf
    assert_equal rv.object_id, buf.object_id
    assert_equal 'o', si.read
    assert_equal nil, si.read(5, buf)
  end

  def test_read_with_buffer_clobbers
    r = init_request('hello')
    si = Unicorn::StreamInput.new(@rd, r)
    buf = 'foo'
    assert_equal 'hello', si.read(nil, buf)
    assert_equal 'hello', buf
    assert_equal '', si.read(nil, buf)
    assert_equal '', buf
    buf = 'asdf'
    assert_nil si.read(5, buf)
    assert_equal '', buf
  end

  def test_read_zero
    r = init_request('hello')
    si = Unicorn::StreamInput.new(@rd, r)
    assert_equal '', si.read(0)
    buf = 'asdf'
    rv = si.read(0, buf)
    assert_equal rv.object_id, buf.object_id
    assert_equal '', buf
    assert_equal 'hello', si.read
    assert_nil si.read(5)
    assert_equal '', si.read(0)
    buf = 'hello'
    rv = si.read(0, buf)
    assert_equal rv.object_id, buf.object_id
    assert_equal '', rv
  end

  def test_gets_read_mix
    r = init_request("hello\nasdfasdf")
    si = Unicorn::StreamInput.new(@rd, r)
    assert_equal "hello\n", si.gets
    assert_equal "asdfasdf", si.read(9)
    assert_nil si.read(9)
  end

  def test_gets_read_mix_chunked
    r = @parser = Unicorn::HttpParser.new
    body = "6\r\nhello"
    @buf = "POST / HTTP/1.1\r\n" \
           "Host: localhost\r\n" \
           "Transfer-Encoding: chunked\r\n" \
           "\r\n#{body}"
    assert_equal @env, @parser.headers(@env, @buf)
    assert_equal body, @buf
    si = Unicorn::StreamInput.new(@rd, r)
    @wr.syswrite "\n\r\n"
    assert_equal "hello\n", si.gets
    @wr.syswrite "8\r\nasdfasdf\r\n"
    assert_equal"asdfasdf", si.read(9) + si.read(9)
    @wr.syswrite "0\r\n\r\n"
    assert_nil si.read(9)
  end

  def test_gets_read_mix_big
    r = init_request("hello\n#{'.' * 65536}")
    si = Unicorn::StreamInput.new(@rd, r)
    assert_equal "hello\n", si.gets
    assert_equal '.' * 16384, si.read(16384)
    assert_equal '.' * 16383, si.read(16383)
    assert_equal '.' * 16384, si.read(16384)
    assert_equal '.' * 16385, si.read(16385)
    assert_nil si.gets
  end

  def init_request(body, size = nil)
    @parser = Unicorn::HttpParser.new
    body = body.to_s.freeze
    @buf = "POST / HTTP/1.1\r\n" \
           "Host: localhost\r\n" \
           "Content-Length: #{size || body.size}\r\n" \
           "\r\n#{body}"
    assert_equal @env, @parser.headers(@env, @buf)
    assert_equal body, @buf
    @parser
  end
end

git clone https://yhbt.net/unicorn.git