File: response_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (343 lines) | stat: -rw-r--r-- 14,230 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
require "spec"
require "http/client/response"

class HTTP::Client::Response
  describe Response do
    it "parses response with body" do
      response = Response.from_io(IO::Memory.new("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\nhelloworld"))
      response.version.should eq("HTTP/1.1")
      response.status_code.should eq(200)
      response.status_message.should eq("OK")
      response.headers["content-type"].should eq("text/plain")
      response.headers["content-length"].should eq("5")
      response.body.should eq("hello")
    end

    it "parses response with streamed body" do
      Response.from_io(IO::Memory.new("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\nhelloworld")) do |response|
        response.version.should eq("HTTP/1.1")
        response.status_code.should eq(200)
        response.status_message.should eq("OK")
        response.headers["content-type"].should eq("text/plain")
        response.headers["content-length"].should eq("5")
        response.body?.should be_nil
        response.body_io.gets_to_end.should eq("hello")
      end
    end

    it "parses response with streamed body, huge content-length" do
      Response.from_io(IO::Memory.new("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: #{UInt64::MAX}\r\n\r\nhelloworld")) do |response|
        response.headers["content-length"].should eq("#{UInt64::MAX}")
      end
    end

    it "parses response with body without \\r" do
      response = Response.from_io(IO::Memory.new("HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 5\n\nhelloworld"))
      response.version.should eq("HTTP/1.1")
      response.status_code.should eq(200)
      response.status_message.should eq("OK")
      response.headers["content-type"].should eq("text/plain")
      response.headers["content-length"].should eq("5")
      response.body.should eq("hello")
    end

    it "parses response with body but without content-length" do
      response = Response.from_io(IO::Memory.new("HTTP/1.1 200 OK\r\n\r\nhelloworld"))
      response.status_code.should eq(200)
      response.status_message.should eq("OK")
      response.headers.size.should eq(0)
      response.body.should eq("helloworld")
    end

    it "parses response with empty body but without content-length" do
      response = Response.from_io(IO::Memory.new("HTTP/1.1 404 Not Found\r\n\r\n"))
      response.status_code.should eq(404)
      response.status_message.should eq("Not Found")
      response.headers.size.should eq(0)
      response.body.should eq("")
    end

    it "parses response without body" do
      response = Response.from_io(IO::Memory.new("HTTP/1.1 100 Continue\r\n\r\n"))
      response.status_code.should eq(100)
      response.status_message.should eq("Continue")
      response.headers.size.should eq(0)
      response.body?.should be_nil
    end

    it "parses response without status message" do
      response = Response.from_io(IO::Memory.new("HTTP/1.1 200\r\n\r\n"))
      response.status_code.should eq(200)
      response.status_message.should eq("")
      response.headers.size.should eq(0)
      response.body.should eq("")
    end

    it "parses response with duplicated headers" do
      response = Response.from_io(IO::Memory.new("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\nWarning: 111 Revalidation failed\r\nWarning: 110 Response is stale\r\n\r\nhelloworld"))
      response.headers.get("Warning").should eq(["111 Revalidation failed", "110 Response is stale"])
    end

    it "parses response with cookies" do
      response = Response.from_io(IO::Memory.new("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\nSet-Cookie: a=b\r\nSet-Cookie: c=d\r\n\r\nhelloworld"))
      response.cookies["a"].value.should eq("b")
      response.cookies["c"].value.should eq("d")
    end

    it "parses response with chunked body" do
      response = Response.from_io(io = IO::Memory.new("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nabcde\r\na\r\n0123456789\r\n0\r\n\r\n"))
      response.body.should eq("abcde0123456789")
      io.gets.should be_nil
    end

    it "parses response with streamed chunked body" do
      Response.from_io(io = IO::Memory.new("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nabcde\r\na\r\n0123456789\r\n0\r\n\r\n")) do |response|
        response.body_io.gets_to_end.should eq("abcde0123456789")
        io.gets.should be_nil
      end
    end

    it "parses response with chunked body of size 0" do
      response = Response.from_io(io = IO::Memory.new("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\n"))
      response.body.should eq("")
      io.gets.should be_nil
    end

    it "parses response ignoring body" do
      response = Response.from_io(IO::Memory.new("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\nhelloworld"), true)
      response.version.should eq("HTTP/1.1")
      response.status_code.should eq(200)
      response.status_message.should eq("OK")
      response.headers["content-type"].should eq("text/plain")
      response.headers["content-length"].should eq("5")
      response.body.should eq("")
    end

    it "parses 204 response without body but Content-Length == 0 (#2512)" do
      response = Response.from_io(IO::Memory.new("HTTP/1.1 204 OK\r\nContent-Type: text/plain\r\nContent-Length: 0\r\n\r\n"))
      response.version.should eq("HTTP/1.1")
      response.status_code.should eq(204)
      response.status_message.should eq("OK")
      response.headers["content-type"].should eq("text/plain")
      response.headers["content-length"].should eq("0")
      response.body.should eq("")
    end

    it "parses response without body but Content-Length == 0, block form (#8461)" do
      Response.from_io(IO::Memory.new("HTTP/1.1 301 OK\r\nContent-Length: 0\r\n\r\n")) do |response|
        response.body_io.gets_to_end.should eq("")
      end
    end

    it "parses long request lines" do
      request = Response.from_io?(IO::Memory.new("HTTP/1.1 200 #{"OK" * 600_000}\r\n\r\n"))
      request.should eq(nil)
    end

    it "parses long headers" do
      request = Response.from_io?(IO::Memory.new("HTTP/1.1 200 OK\r\n#{"X-Test-Header: A pretty log header value\r\n" * 100_000}\r\n"))
      request.should eq(nil)
    end

    describe "handle invalid IO" do
      it "missing HTTP header" do
        expect_raises(Exception, "Invalid HTTP response") do
          Response.from_io?(IO::Memory.new("<html>\n</html>"))
        end
      end

      it "unsupported version" do
        expect_raises(Exception, "Unsupported HTTP version: HTML/1.0") do
          Response.from_io?(IO::Memory.new("HTML/1.0 200 OK\n\nNot an HTTP response"))
        end
      end

      it "missing status" do
        expect_raises(Exception, "Invalid HTTP response") do
          Response.from_io?(IO::Memory.new("HTTP/1.0\n\nNot an HTTP response"))
        end
      end

      it "invalid status" do
        expect_raises(Exception, "Invalid HTTP status code: OK") do
          Response.from_io?(IO::Memory.new("HTTP/1.0 OK\n\nNot an HTTP response"))
        end
        expect_raises(Exception, "Invalid HTTP status code: 1000") do
          Response.from_io?(IO::Memory.new("HTTP/1.0 1000\n\nNot an HTTP response"))
        end
        expect_raises(Exception, "Invalid HTTP status code: -5") do
          Response.from_io?(IO::Memory.new("HTTP/1.0 -5\n\nNot an HTTP response"))
        end
        expect_raises(Exception, "Invalid HTTP status code: 42") do
          Response.from_io?(IO::Memory.new("HTTP/1.0 42\n\nNot an HTTP response"))
        end
      end
    end

    it "doesn't sets content length for 1xx, 204 or 304" do
      [HTTP::Status::CONTINUE, HTTP::Status::SWITCHING_PROTOCOLS, HTTP::Status::NO_CONTENT, HTTP::Status::NOT_MODIFIED].each do |status|
        response = Response.new(status)
        response.headers.size.should eq(0)
      end
    end

    it "raises when creating 1xx, 204 or 304 with body" do
      [HTTP::Status::CONTINUE, HTTP::Status::SWITCHING_PROTOCOLS, HTTP::Status::NO_CONTENT, HTTP::Status::NOT_MODIFIED].each do |status|
        expect_raises ArgumentError do
          Response.new(status, "hello")
        end
      end
    end

    it "serialize with body" do
      headers = HTTP::Headers.new
      headers["Content-Type"] = "text/plain"
      headers["Content-Length"] = "5"

      response = Response.new(:ok, "hello", headers)
      io = IO::Memory.new
      response.to_io(io)
      io.to_s.should eq("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\nhello")
    end

    it "serialize with body and cookies" do
      headers = HTTP::Headers.new
      headers["Content-Type"] = "text/plain"
      headers["Content-Length"] = "5"
      headers["Set-Cookie"] = "foo=bar; path=/"

      response = Response.new(:ok, "hello", headers)

      io = IO::Memory.new
      response.to_io(io)
      io.to_s.should eq("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\nSet-Cookie: foo=bar; path=/\r\n\r\nhello")

      response.cookies["foo"].value.should eq "bar" # Force lazy initialization

      io.clear
      response.to_io(io)
      io.to_s.should eq("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\nSet-Cookie: foo=bar; path=/\r\n\r\nhello")

      response.cookies["foo"] = "baz"
      response.cookies << Cookie.new("quux", "baz")

      io.clear
      response.to_io(io)
      io.to_s.should eq("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\nSet-Cookie: foo=baz\r\nSet-Cookie: quux=baz\r\n\r\nhello")
    end

    it "sets content length from body" do
      response = Response.new(:ok, "hello")
      io = IO::Memory.new
      response.to_io(io)
      io.to_s.should eq("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello")
    end

    it "sets content length even without body" do
      response = Response.new(:ok)
      io = IO::Memory.new
      response.to_io(io)
      io.to_s.should eq("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")
    end

    it "serialize as chunked with body_io" do
      response = Response.new(:ok, body_io: IO::Memory.new("hello"))
      io = IO::Memory.new
      response.to_io(io)
      io.to_s.should eq("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n0\r\n\r\n")
    end

    it "serialize as not chunked with body_io if HTTP/1.0" do
      response = Response.new(:ok, version: "HTTP/1.0", body_io: IO::Memory.new("hello"))
      io = IO::Memory.new
      response.to_io(io)
      io.to_s.should eq("HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\nhello")
    end

    it "returns no content_type when header is missing" do
      response = Response.new(:ok, "")
      response.content_type.should be_nil
      response.charset.should be_nil
    end

    it "returns content type and no charset" do
      response = Response.new(:ok, "", headers: HTTP::Headers{"Content-Type" => "application/octet-stream"})
      response.content_type.should eq("application/octet-stream")
      response.charset.should be_nil
    end

    it "returns content type and charset, removes semicolon" do
      response = Response.new(:ok, "", headers: HTTP::Headers{"Content-Type" => "text/plain ; charset=UTF-8"})
      response.content_type.should eq("text/plain")
      response.charset.should eq("UTF-8")
    end

    it "returns content type and charset, removes quotes" do
      response = Response.new(:ok, "", headers: HTTP::Headers{"Content-Type" => %(text/plain ; charset="UTF-8")})
      response.content_type.should eq("text/plain")
      response.charset.should eq("UTF-8")
    end

    it "returns content type and no charset, other parameter (#2520)" do
      response = Response.new(:ok, "", headers: HTTP::Headers{"Content-Type" => "application/octet-stream ; colenc=U"})
      response.content_type.should eq("application/octet-stream")
      response.charset.should be_nil
    end

    it "returns content type and charset, removes semicolon, with multiple parameters (#2520)" do
      response = Response.new(:ok, "", headers: HTTP::Headers{"Content-Type" => "text/plain ; colenc=U ; charset=UTF-8"})
      response.content_type.should eq("text/plain")
      response.charset.should eq("UTF-8")
    end

    it "returns content type as nil when empty (#8398)" do
      response = Response.new(:ok, "", headers: HTTP::Headers{"Content-Type" => ""})
      response.content_type.should be_nil
    end

    it "returns status_code" do
      response = Response.new(:created)
      response.status_code.should eq 201
    end

    it "creates Response with status code 204, no body and Content-Length == 0 (#2512)" do
      response = Response.new(204, version: "HTTP/1.0", body: "", headers: HTTP::Headers{"Content-Length" => "0"})
      response.status_code.should eq(204)
      response.body.should eq("")
    end

    it "deletes Content-Encoding and Content-Length headers after gzip decompression" do
      body = String.build do |io|
        Compress::Gzip::Writer.open(io, &.print("hello"))
      end
      response = Response.from_io(IO::Memory.new("HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}"))
      response.body.should eq("hello")
      response.headers["content-encoding"]?.should eq(nil)
      response.headers["content-length"]?.should eq(nil)
    end

    it "deletes Content-Encoding and Content-Length headers after deflate decompression" do
      body = String.build do |io|
        Compress::Deflate::Writer.open(io, &.print("hello"))
      end
      response = Response.from_io(IO::Memory.new("HTTP/1.1 200 OK\r\nContent-Encoding: deflate\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}"))
      response.body.should eq("hello")
      response.headers["content-encoding"]?.should eq(nil)
      response.headers["content-length"]?.should eq(nil)
    end

    describe "success?" do
      it "returns true for the 2xx" do
        response = Response.new(:ok)

        response.success?.should eq(true)
      end

      it "returns false for other ranges" do
        response = Response.new(500)

        response.success?.should eq(false)
      end
    end
  end
end