File: chunked_content_spec.cr

package info (click to toggle)
crystal 1.6.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,956 kB
  • sloc: javascript: 1,712; sh: 592; cpp: 541; makefile: 243; ansic: 119; python: 105; xml: 32
file content (271 lines) | stat: -rw-r--r-- 7,000 bytes parent folder | download | duplicates (2)
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
require "spec"
require "http/content"

describe HTTP::ChunkedContent do
  it "delays reading the next chunk as soon as one is consumed (#3270)" do
    mem = IO::Memory.new("4\r\n123\n\r\n0\r\n\r\n")
    content = HTTP::ChunkedContent.new(mem)
    bytes = uninitialized UInt8[4]
    bytes_read = content.read(bytes.to_slice)
    bytes_read.should eq(4)
    String.new(bytes.to_slice).should eq("123\n")
    mem.pos.should eq(7) # only this chunk was read
  end

  it "peeks" do
    mem = IO::Memory.new("4\r\n123\n\r\n0\r\n\r\n")
    content = HTTP::ChunkedContent.new(mem)

    content.peek.should eq("123\n".to_slice)
    content.skip(4)
    content.peek.should eq Bytes.empty
  end

  it "peeks into next chunk" do
    mem = IO::Memory.new("4\r\n123\n\r\n3\r\n456\r\n0\r\n\r\n")
    content = HTTP::ChunkedContent.new(mem)

    content.skip(4)
    content.peek.should eq("456".to_slice)
    content.gets_to_end.should eq("456")
  end

  it "skips" do
    mem = IO::Memory.new("4\r\n123\n\r\n0\r\n\r\n")
    content = HTTP::ChunkedContent.new(mem)

    content.skip(2)
    content.read_char.should eq('3')

    expect_raises(IO::EOFError) do
      content.skip(10)
    end
  end

  it "skips (2)" do
    mem = IO::Memory.new("4\r\n123\n\r\n0\r\n\r\n")
    content = HTTP::ChunkedContent.new(mem)

    content.skip(2)
    content.gets_to_end.should eq("3\n")
  end

  it "skips (3)" do
    mem = IO::Memory.new("4\r\n123\n\r\n3\r\n456\r\n0\r\n\r\n")
    content = HTTP::ChunkedContent.new(mem)

    content.skip(4)
    content.gets_to_end.should eq("456")
  end

  it "#gets reads multiple chunks" do
    mem = IO::Memory.new("1\r\nA\r\n1\r\nB\r\n0\r\n\r\n")
    content = HTTP::ChunkedContent.new(mem)

    content.gets.should eq "AB"
    mem.pos.should eq mem.bytesize
  end

  it "#gets reads multiple chunks with \n" do
    # The RFC format requires CRLF, but several standard implementations also
    # accept LF, so we should too.
    mem = IO::Memory.new("1\nA\n1\nB\n0\n\n")
    content = HTTP::ChunkedContent.new(mem)

    content.gets.should eq "AB"
    mem.pos.should eq mem.bytesize
  end

  it "#read reads empty content" do
    mem = IO::Memory.new("0\r\n\r\n")
    content = HTTP::ChunkedContent.new(mem)

    content.read(Bytes.new(5)).should eq 0
    mem.pos.should eq mem.bytesize
  end

  it "#read_byte reads empty content" do
    mem = IO::Memory.new("0\r\n\r\n")
    content = HTTP::ChunkedContent.new(mem)

    content.read_byte.should be_nil
    mem.pos.should eq mem.bytesize
  end

  it "#peek reads empty content" do
    mem = IO::Memory.new("0\r\n\r\n")
    content = HTTP::ChunkedContent.new(mem)

    content.peek.should eq Bytes.empty
    mem.pos.should eq mem.bytesize
  end

  it "#read handles interrupted io" do
    mem = IO::Memory.new("3\r\n123\r\n")
    content = HTTP::ChunkedContent.new(mem)

    content.skip(3)
    expect_raises IO::EOFError do
      content.read Bytes.new(5)
    end
  end

  it "#read_byte handles interrupted io" do
    mem = IO::Memory.new("3\r\n123\r\n")
    content = HTTP::ChunkedContent.new(mem)

    content.skip(3)
    expect_raises IO::EOFError do
      content.read_byte
    end
  end

  it "#peek handles interrupted io" do
    mem = IO::Memory.new("3\r\n123\r\n")
    content = HTTP::ChunkedContent.new(mem)

    content.skip(3)
    expect_raises IO::EOFError do
      content.peek
    end
  end

  it "#read handles empty data" do
    mem = IO::Memory.new("3\r\n")
    content = HTTP::ChunkedContent.new(mem)

    expect_raises IO::EOFError do
      content.read Bytes.new(1)
    end
  end

  it "#read_byte handles empty data" do
    mem = IO::Memory.new("3\r\n")
    content = HTTP::ChunkedContent.new(mem)

    expect_raises IO::Error do
      content.read_byte
    end
  end

  it "#peek handles empty data" do
    mem = IO::Memory.new("3\r\n")
    content = HTTP::ChunkedContent.new(mem)

    expect_raises IO::EOFError do
      content.peek
    end
  end

  it "handles empty io" do
    mem = IO::Memory.new("")
    chunked = HTTP::ChunkedContent.new(mem)
    expect_raises IO::EOFError do
      chunked.gets
    end
  end

  it "reads chunk extensions" do
    mem = IO::Memory.new(%(1;progress=50;foo="bar"\r\nA\r\n1;progress=100\r\nB\r\n0\r\n\r\n"))

    chunked = HTTP::ChunkedContent.new(mem)
    chunked.gets(2).should eq "AB"
  end

  it "reads chunked trailer part" do
    mem = IO::Memory.new("0\r\nAdditional-Header: Foo\r\n\r\n")

    chunked = HTTP::ChunkedContent.new(mem)
    chunked.gets.should be_nil
    mem.gets.should be_nil
    chunked.headers.should eq HTTP::Headers{"Additional-Header" => "Foo"}
  end

  it "fails if unterminated chunked trailer part" do
    mem = IO::Memory.new("0\r\nAdditional-Header: Foo")

    chunked = HTTP::ChunkedContent.new(mem)
    expect_raises IO::EOFError do
      chunked.gets
    end
  end

  describe "long trailer part" do
    it "fails for long single header" do
      mem = IO::Memory.new("0\r\nFoo: Bar Baz Qux\r\n\r\n")

      chunked = HTTP::ChunkedContent.new(mem, max_headers_size: 12)
      expect_raises(IO::Error, "Trailing headers too long") do
        chunked.gets
      end
      chunked.headers.should be_empty
    end

    it "fails for long combined headers" do
      mem = IO::Memory.new("0\r\nFoo: Bar\r\nBaz: Qux\r\n\r\n")

      chunked = HTTP::ChunkedContent.new(mem, max_headers_size: 12)
      expect_raises(IO::Error, "Trailing headers too long") do
        chunked.gets
      end
      chunked.headers.should eq HTTP::Headers{"Foo" => "Bar"}
    end
  end

  it "fails if not properly delimited" do
    mem = IO::Memory.new("0\r\n")

    chunked = HTTP::ChunkedContent.new(mem)
    expect_raises IO::EOFError do
      chunked.gets
    end
  end

  it "fails if not properly delimited" do
    mem = IO::Memory.new("1\r\nA\r\n0\r\n")

    chunked = HTTP::ChunkedContent.new(mem)
    expect_raises IO::EOFError do
      chunked.gets
    end
  end

  it "fails if invalid chunk size" do
    mem = IO::Memory.new("G\r\n")

    chunked = HTTP::ChunkedContent.new(mem)
    expect_raises IO::Error, "Invalid HTTP chunked content: invalid chunk size" do
      chunked.gets
    end
  end

  it "#read stops reading after final chunk" do
    mem = IO::Memory.new("0\r\n\r\n1\r\nA\r\n0\r\n\r\n")

    chunked = HTTP::ChunkedContent.new(mem)
    chunked.read(Bytes.new(8)).should eq 0
    mem.pos.should eq 5
    chunked.read(Bytes.new(8)).should eq 0
    mem.pos.should eq 5
  end

  it "#read_byte stops reading after final chunk" do
    mem = IO::Memory.new("0\r\n\r\n1\r\nA\r\n0\r\n\r\n")

    chunked = HTTP::ChunkedContent.new(mem)
    chunked.read_byte.should be_nil
    mem.pos.should eq 5
    chunked.read_byte.should be_nil
    mem.pos.should eq 5
  end

  it "#peek stops reading after final chunk" do
    mem = IO::Memory.new("0\r\n\r\n1\r\nA\r\n0\r\n\r\n")

    chunked = HTTP::ChunkedContent.new(mem)
    chunked.peek.should eq Bytes.empty
    mem.pos.should eq 5
    chunked.peek.should eq Bytes.empty
    mem.pos.should eq 5
  end
end