kcar.git  about / heads / tags
bytestream to Rack response converter
blob 84e609d41bad8b2bab776cf70041c5ca510d8d01 10091 bytes (raw)
$ git show HEAD:test/test_parser.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
 
# -*- encoding: binary -*-
require 'test/unit'
require 'pp'
require 'kcar'
require 'rack'

class TestParser < Test::Unit::TestCase

  def setup
    @hp = Kcar::Parser.new
  end

  def test_reset
    assert_nothing_raised { @hp.reset }
  end

  def test_parser_status_eof
    buf = "HTTP/1.0 200 OK\r\n\r\n"
    hdr = []
    hdr_object_id = hdr.object_id
    response = @hp.headers(hdr, buf)
    assert_equal(["200 OK", hdr], response)
    assert hdr.empty?
    assert ! @hp.keepalive?
    assert_equal hdr_object_id, hdr.object_id
    assert_equal "", buf
  end

  def test_parser_status_eof_one_one
    buf = "HTTP/1.1 200 OK\r\n\r\n"
    hdr = []
    response = @hp.headers(hdr, buf)
    assert_equal(["200 OK", hdr], response)
    assert hdr.empty?
    assert @hp.keepalive? # no content-length
  end

  def test_parser_status_with_content_length
    buf = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
    hdr = []
    response = @hp.headers(hdr, buf)
    assert_equal(["200 OK", hdr], response)
    assert_equal([%w(Content-Length 0)], hdr)
    assert @hp.keepalive?
  end

  def test_parser_content_length
    buf = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"
    rv = @hp.headers([], buf)
    assert_equal "200 OK", rv[0]
    assert_equal([ %w(Content-Length 5) ], rv[1])
    assert_equal 2, rv.size
    assert_equal "", buf
    assert_equal 5, @hp.body_bytes_left
  end

  def test_parser_content_length_with_body
    buf = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nabcde"
    rv = @hp.headers([], buf)
    assert_equal "200 OK", rv[0]
    assert_equal([ %w(Content-Length 5) ], rv[1])
    assert_equal "abcde", buf
    assert_equal 5, @hp.body_bytes_left
  end

  def test_bad_crlf
    buf = "HTTP/1.1 200 OK\nContent-Length: 5\n\r\nabcde"
    rv = @hp.headers([], buf)
    assert_equal "200 OK", rv[0]
    assert_equal([ %w(Content-Length 5) ], rv[1])
    assert_equal "abcde", buf
    assert_equal 5, @hp.body_bytes_left
    assert ! @hp.chunked?
  end

  def test_chunky_bad_crlf
    buf = "HTTP/1.1 200 OK\n" \
          "Transfer-Encoding: chunked\n\n" \
          "6\nabcdef\n0\n\n"
    rv = @hp.headers([], buf)
    assert_equal "200 OK", rv[0]
    assert_equal([ %w(Transfer-Encoding chunked) ], rv[1])
    assert_equal "6\nabcdef\n0\n\n", buf
    assert_nil @hp.body_bytes_left
    assert @hp.chunked?
    assert_nil @hp.filter_body(tmp = "", buf)
    assert_equal "abcdef", tmp
    assert @hp.keepalive?
  end

  def test_chunky
    buf = "HTTP/1.1 200 OK\r\n" \
          "Transfer-Encoding: chunked\r\n\r\n" \
          "6\r\nabcdef\r\n0\r\n\r\n"
    rv = @hp.headers([], buf)
    assert_equal "200 OK", rv[0]
    assert_equal([ %w(Transfer-Encoding chunked) ], rv[1])
    assert_equal "6\r\nabcdef\r\n0\r\n\r\n", buf
    assert_nil @hp.body_bytes_left
    assert @hp.chunked?
    assert_nil @hp.filter_body(tmp = "", buf)
    assert_equal "abcdef", tmp
    assert @hp.body_eof?
    assert @hp.keepalive?
  end

  def test_chunky_two_step
    buf = "HTTP/1.1 200 OK\r\n" \
          "Transfer-Encoding: chunked\r\n\r\n" \
          "6\r\nabcd"
    buf2 = "ef\r\n0\r\n\r\n"
    rv = @hp.headers([], buf)
    assert_equal "200 OK", rv[0]
    assert_equal([ %w(Transfer-Encoding chunked) ], rv[1])
    assert_equal "6\r\nabcd", buf
    assert_nil @hp.body_bytes_left
    assert @hp.chunked?
    assert_nil @hp.filter_body(tmp = "", buf)
    assert_equal "abcd", tmp
    assert_equal "", buf
    assert ! @hp.body_eof?
    assert_nil @hp.filter_body(tmp = "", buf2)
    assert_equal "ef", tmp
    assert @hp.body_eof?
    assert_equal({}, @hp.trailers(tmp = {}, buf2))
    assert @hp.keepalive?
    assert_nothing_raised { @hp.reset }
  end

  def test_trailers_ary
    buf = "HTTP/1.1 200 OK\r\n" \
          "Trailer: Foo\r\n" \
          "Transfer-Encoding: chunked\r\n\r\n" \
          "6\r\nabcdef\r\n0\r\nFoo: bar\r\n\r\n"
    rv = @hp.headers([], buf)
    assert_equal "200 OK", rv[0]
    assert_equal([ %w(Trailer Foo), %w(Transfer-Encoding chunked) ], rv[1])
    assert_equal "6\r\nabcdef\r\n0\r\nFoo: bar\r\n\r\n", buf
    assert_nil @hp.body_bytes_left
    assert @hp.chunked?
    assert_nil @hp.filter_body(tmp = "", buf)
    assert_equal "abcdef", tmp
    assert @hp.body_eof?
    expect = [ %w(Trailer Foo),
               %w(Transfer-Encoding chunked),
               %w(Foo bar) ]
    assert_equal(expect, @hp.trailers(rv[1], buf))
    assert @hp.keepalive?
    assert_nothing_raised { @hp.reset }
  end

  def test_extract_trailers_ary
    tmp = [ %w(Trailer Foo), %w(Transfer-Encoding chunked), %w(Foo bar) ]
    assert_equal [ %w(Foo bar) ], @hp.extract_trailers(tmp)
  end

  def test_extract_trailers_hash
    tmp = {
      'Trailer' => 'Foo',
      'Transfer-Encoding' => 'chunked',
      'Foo' => 'bar'
    }
    assert_equal [ %w(Foo bar) ], @hp.extract_trailers(tmp)
  end

  def test_extract_trailers_header_hash
    tmp = Rack::Utils::HeaderHash.new(
      'Trailer' => 'foo',
      'Transfer-Encoding' => 'chunked',
      'Foo' => 'bar'
    )
    assert_equal [ %w(foo bar) ], @hp.extract_trailers(tmp)
  end

  def test_repeated_headers_rack_hash
    hdr = Rack::Utils::HeaderHash.new
    buf = "HTTP/1.1 200 OK\r\nSet-Cookie: a=b\r\n"
    assert_nil  @hp.headers(hdr, buf)
    assert_equal({ 'Set-Cookie' => 'a=b' }, hdr.to_hash)
    assert_nil @hp.headers(hdr, buf << "set-cookie: c=d\r\n")
    assert_equal([ "200 OK", hdr ], @hp.headers(hdr, buf << "\r\n"))
    assert_equal "", buf
    assert_equal({ 'Set-Cookie' => "a=b\nc=d" }, hdr.to_hash)
  end

  def test_repeated_headers_plain_hash
    hdr = {}
    buf = "HTTP/1.1 200 OK\r\nSet-Cookie: a=b\r\n"
    assert_nil  @hp.headers(hdr, buf)
    assert_equal({ 'Set-Cookie' => 'a=b' }, hdr)
    assert_nil @hp.headers(hdr, buf << "set-cookie: c=d\r\n")
    assert_equal([ "200 OK", hdr ], @hp.headers(hdr, buf << "\r\n"))
    assert_equal "", buf
    assert_equal({ 'Set-Cookie' => 'a=b', 'set-cookie' => 'c=d' }, hdr)
  end

  def test_repeated_headers_array
    hdr = []
    buf = "HTTP/1.1 200 OK\r\nSet-Cookie: a=b\r\n"
    assert_nil  @hp.headers(hdr, buf)
    assert_equal([ %w(Set-Cookie a=b) ] , hdr)
    assert_nil @hp.headers(hdr, buf << "set-cookie: c=d\r\n")
    assert_equal([ "200 OK", hdr ], @hp.headers(hdr, buf << "\r\n"))
    assert_equal "", buf
    assert_equal([ %w(Set-Cookie a=b), %w(set-cookie c=d) ], hdr)
  end

  def test_long_line_headers_array
    hdr = []
    buf = "HTTP/1.1 200 OK\r\na: b\r\n"
    assert_nil  @hp.headers(hdr, buf)
    assert_equal([ %w(a b) ] , hdr)
    assert_nil @hp.headers(hdr, buf << " c\r\n")
    assert_equal([ [ 'a', 'b c'] ], hdr)
    assert_nil @hp.headers(hdr, buf << " d\n")
    assert_equal([ [ 'a', 'b c d'] ], hdr)
    assert_equal([ "200 OK", hdr ], @hp.headers(hdr, buf << "\r\n"))
    assert_equal([ [ 'a', 'b c d'] ], hdr)
  end

  def test_long_line_headers_plain_hash
    hdr = {}
    buf = "HTTP/1.1 200 OK\r\na: b\r\n"
    assert_nil  @hp.headers(hdr, buf)
    assert_equal({ 'a' => 'b' }, hdr)
    assert_nil @hp.headers(hdr, buf << " c\r\n")
    assert_equal({ 'a' => 'b c' }, hdr)
    assert_nil @hp.headers(hdr, buf << " d\r\n")
    assert_equal({ 'a' => 'b c d' }, hdr)
    assert_equal([ "200 OK", hdr ], @hp.headers(hdr, buf << "\r\n"))
    assert_equal({ 'a' => 'b c d' }, hdr)
  end

  def test_long_line_headers_rack_hash
    hdr = Rack::Utils::HeaderHash.new
    buf = "HTTP/1.1 200 OK\r\na: b\r\n"
    assert_nil  @hp.headers(hdr, buf)
    assert_equal({ 'a' => 'b' }, hdr.to_hash)
    assert_nil @hp.headers(hdr, buf << " c\r\n")
    assert_equal({ 'a' => 'b c' }, hdr)
    assert_nil @hp.headers(hdr, buf << " d\r\n")
    assert_equal({ 'a' => 'b c d' }, hdr)
    assert_nil @hp.headers(hdr, buf << "A: e\r\n")
    assert_equal([ "200 OK", hdr ], @hp.headers(hdr, buf << "\r\n"))
    assert_equal({ 'a' => "b c d\ne"}, hdr.to_hash)
  end

  def test_content_length_invalid
    assert_raises(Kcar::ParserError) do
      @hp.headers([], "HTTP/1.1 200 OK\r\nContent-Length: 5a\r\n\r\n")
    end
    assert_raises(Kcar::ParserError) do
      @hp.headers([], "HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\n")
    end
  end

  def test_bad_cr
    assert_raises(Kcar::ParserError) do
      @hp.headers([], "HTTP/1.1 200 OK\r\nContent-Length: 5\r\nA:\rb")
    end
  end

  def test_leading_tab
    resp = "HTTP/1.1 200 OK\r\nHost:\texample.com\r\n\r\n"
    assert @hp.headers(env = {}, resp)
    assert_equal 'example.com', env['Host']
  end

  def test_trailing_whitespace
    resp = "HTTP/1.1 200 OK\r\nHost: example.com \r\n\r\n"
    assert @hp.headers(env = {}, resp)
    assert_equal 'example.com', env['Host']
  end

  def test_trailing_tab
    resp = "HTTP/1.1 200 OK\r\nHost: example.com\t\r\n\r\n"
    assert @hp.headers(env = {}, resp)
    assert_equal 'example.com', env['Host']
  end

  def test_trailing_multiple_linear_whitespace
    resp = "HTTP/1.1 200 OK\r\nHost: example.com\t \t \t\r\n\r\n"
    assert @hp.headers(env = {}, resp)
    assert_equal 'example.com', env['Host']
  end

  def test_embedded_linear_whitespace_ok
    resp = "HTTP/1.1 200 OK\r\nX-Space: hello\t world\t \r\n\r\n"
    assert @hp.headers(env = {}, resp)
    assert_equal "hello\t world", env["X-Space"]
  end

  def test_empty_header
    resp = "HTTP/1.1 200 OK\r\nHost:  \r\n\r\n"
    assert @hp.headers(env = {}, resp)
    assert_equal '', env['Host']
  end

  def test_memsize
    require 'objspace'
    n = ObjectSpace.memsize_of(@hp)
    assert_kind_of Integer, n
    warn "memsize: #{n}\n" if $DEBUG
  rescue LoadError
    warn 'ObjectSpace not available'
  end

  def test_uminus_dd
    # oddly, opt_str_freeze is not always effective:
    # https://bugs.ruby-lang.org/issues/13282
    a = -(%w(H o s t).join)
    b = -(%w(H o s t).join)
    if a.object_id == b.object_id
      resp = "HTTP/1.1 200 OK\r\nHost: example.com\r\n\r\n"
      assert @hp.headers(e = {}, resp.dup)
      @hp.reset
      assert @hp.headers(f = {}, resp.dup)
      assert_same e.keys[0], f.keys[0]
      assert_same a, e.keys[0]
    else
      warn "String#-@ does not dedupe with #{RUBY_ENGINE}-#{RUBY_VERSION}"
    end
  end
end

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