File: request_processor_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 (352 lines) | stat: -rw-r--r-- 9,472 bytes parent folder | download
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
344
345
346
347
348
349
350
351
352
require "spec"
require "log/spec"
require "http/server/request_processor"
require "../../../support/io"

private def requestize(string)
  string.gsub('\n', "\r\n")
end

describe HTTP::Server::RequestProcessor do
  it "works" do
    processor = HTTP::Server::RequestProcessor.new do |context|
      context.response.content_type = "text/plain"
      context.response.print "Hello world"
    end

    input = IO::Memory.new("GET / HTTP/1.1\r\n\r\n")
    output = IO::Memory.new
    processor.process(input, output)
    output.rewind
    output.gets_to_end.should eq(requestize(<<-HTTP
      HTTP/1.1 200 OK
      Connection: keep-alive
      Content-Type: text/plain
      Content-Length: 11

      Hello world
      HTTP
    ))
  end

  describe "reads consecutive requests" do
    it "when body is consumed" do
      processor = HTTP::Server::RequestProcessor.new do |context|
        context.response.content_type = "text/plain"
        context.response << context.request.body.not_nil!.gets(chomp: true)
        context.response << "\r\n"
      end

      input = IO::Memory.new(requestize(<<-HTTP
        POST / HTTP/1.1
        Content-Length: 7

        hello
        POST / HTTP/1.1
        Content-Length: 7

        hello
        HTTP
      ))
      output = IO::Memory.new
      processor.process(input, output)
      output.rewind
      output.gets_to_end.should eq(requestize(<<-HTTP
        HTTP/1.1 200 OK
        Connection: keep-alive
        Content-Type: text/plain
        Content-Length: 7

        hello
        HTTP/1.1 200 OK
        Connection: keep-alive
        Content-Type: text/plain
        Content-Length: 7

        hello

        HTTP
      ))
    end

    it "with empty body" do
      processor = HTTP::Server::RequestProcessor.new do |context|
        context.response.content_type = "text/plain"
        context.response.puts "Hello world\r"
      end

      input = IO::Memory.new(requestize(<<-HTTP
        POST / HTTP/1.1

        POST / HTTP/1.1
        Content-Length: 7

        hello
        HTTP
      ))
      output = IO::Memory.new
      processor.process(input, output)
      output.rewind
      output.gets_to_end.should eq(requestize(<<-HTTP
        HTTP/1.1 200 OK
        Connection: keep-alive
        Content-Type: text/plain
        Content-Length: 13

        Hello world
        HTTP/1.1 200 OK
        Connection: keep-alive
        Content-Type: text/plain
        Content-Length: 13

        Hello world

        HTTP
      ))
    end

    it "fail if body is not consumed" do
      processor = HTTP::Server::RequestProcessor.new do |context|
        context.response.content_type = "text/plain"
        context.response.puts "Hello world\r"
      end

      input = IO::Memory.new(requestize(<<-HTTP
        POST / HTTP/1.1

        hello
        POST / HTTP/1.1
        Content-Length: 7

        hello
        HTTP
      ))
      output = IO::Memory.new
      processor.process(input, output)
      output.rewind
      output.gets_to_end.should eq(requestize(<<-HTTP
        HTTP/1.1 200 OK
        Connection: keep-alive
        Content-Type: text/plain
        Content-Length: 13

        Hello world
        HTTP/1.1 400 Bad Request
        Content-Type: text/plain
        Content-Length: 16

        400 Bad Request\\n
        HTTP
      ).gsub("\\n", "\n"))
    end

    it "closes connection when Connection: close" do
      processor = HTTP::Server::RequestProcessor.new do |context|
        context.response.headers["Connection"] = "close"
      end

      input = IO::Memory.new(requestize(<<-HTTP
        POST / HTTP/1.1
        Content-Length: 7

        hello
        POST / HTTP/1.1
        Content-Length: 7

        hello
        HTTP
      ))
      output = IO::Memory.new
      processor.process(input, output)
      output.rewind
      output.gets_to_end.should eq(requestize(<<-HTTP
        HTTP/1.1 200 OK
        Connection: close
        Content-Length: 0


        HTTP
      ))
    end

    it "closes connection when request body is not entirely consumed" do
      processor = HTTP::Server::RequestProcessor.new do |context|
      end

      input = IO::Memory.new(requestize(<<-HTTP
        POST / HTTP/1.1
        Content-Length: 4

        1
        POST / HTTP/1.1
        Content-Length: 7

        hello
        HTTP
      ))
      output = IO::Memory.new
      processor.process(input, output)
      output.rewind
      output.gets_to_end.should eq(requestize(<<-HTTP
        HTTP/1.1 200 OK
        Connection: keep-alive
        Content-Length: 0


        HTTP
      ))
    end

    it "continues when request body is entirely consumed" do
      processor = HTTP::Server::RequestProcessor.new do |context|
        io = context.request.body.not_nil!
        io.gets_to_end
      end

      input = IO::Memory.new(requestize(<<-HTTP
        POST / HTTP/1.1
        Content-Length: 16387

        #{"0" * 16_384}1
        POST / HTTP/1.1
        Content-Length: 7

        hello
        HTTP
      ))
      output = IO::Memory.new
      processor.process(input, output)
      output.rewind
      output.gets_to_end.should eq(requestize(<<-HTTP
        HTTP/1.1 200 OK
        Connection: keep-alive
        Content-Length: 0

        HTTP/1.1 200 OK
        Connection: keep-alive
        Content-Length: 0


        HTTP
      ))
    end
  end

  it "handles IO::Error while reading" do
    processor = HTTP::Server::RequestProcessor.new { }
    input = RaiseIOError.new
    output = IO::Memory.new
    processor.process(input, output)
    output.rewind.gets_to_end.should be_empty
  end

  it "handles IO::Error while writing" do
    processor = HTTP::Server::RequestProcessor.new do |context|
      context.response.content_type = "text/plain"
      context.response.print "Hello world"
      context.response.flush
    end
    input = IO::Memory.new("GET / HTTP/1.1\r\n\r\n")
    output = RaiseIOError.new(true)
    logs = Log.capture("http.server") do
      processor.process(input, output)
    end
    logs.check(:debug, "Error while writing data to the client")
    logs.entry.exception.should be_a(IO::Error)
  end

  it "handles IO::Error while flushing" do
    processor = HTTP::Server::RequestProcessor.new do |context|
      context.response.flush
    end
    input = IO::Memory.new("GET / HTTP/1.1\r\n\r\n")
    output = RaiseIOError.new(false)
    logs = Log.capture("http.server") do
      processor.process(input, output)
    end
    logs.check(:debug, "Error while flushing data to the client")
    logs.entry.exception.should be_a(IO::Error)
  end

  it "catches raised error on handler and retains context from handler" do
    exception = Exception.new "OH NO"
    processor = HTTP::Server::RequestProcessor.new { Log.context.set foo: "bar"; raise exception }
    input = IO::Memory.new("GET / HTTP/1.1\r\n\r\n")
    output = IO::Memory.new
    logs = Log.capture("http.server") do
      processor.process(input, output)
    end

    client_response = HTTP::Client::Response.from_io(output.rewind)
    client_response.status_code.should eq(500)
    client_response.status_message.should eq("Internal Server Error")
    client_response.headers["content-type"].should eq("text/plain")
    client_response.headers.has_key?("content-length").should be_true
    client_response.body.should eq("500 Internal Server Error\n")

    logs.check(:error, "Unhandled exception on HTTP::Handler")
    logs.entry.exception.should eq(exception)
    logs.entry.context[:foo].should eq "bar"
  end

  it "doesn't respond with error when headers were already sent" do
    processor = HTTP::Server::RequestProcessor.new do |context|
      context.response.content_type = "text/plain"
      context.response.print "Hello world"
      context.response.flush
      raise "OH NO"
    end
    input = IO::Memory.new("GET / HTTP/1.1\r\n\r\n")
    output = IO::Memory.new
    processor.process(input, output)

    client_response = HTTP::Client::Response.from_io(output.rewind)
    client_response.status_code.should eq(200)
    client_response.body.should eq("Hello world")
  end

  it "flushes output buffer when an error happens and some content was already sent" do
    processor = HTTP::Server::RequestProcessor.new do |context|
      context.response.content_type = "text/plain"
      context.response.print "Hello "
      context.response.flush
      context.response.print "world"
      raise "OH NO"
    end
    input = IO::Memory.new("GET / HTTP/1.1\r\n\r\n")
    output = IO::Memory.new
    processor.process(input, output)

    client_response = HTTP::Client::Response.from_io(output.rewind)
    client_response.status_code.should eq(200)
    client_response.body.should eq("Hello world")
  end

  it "does not bleed Log::Context between requests" do
    processor = HTTP::Server::RequestProcessor.new do |context|
      Log.info { "before" }
      Log.context.set foo: "bar"
      Log.info { "after" }

      context.response.content_type = "text/plain"
      context.response.print "Hello world"
    end

    logs = Log.capture do
      processor.process(
        IO::Memory.new("GET / HTTP/1.1\r\n\r\nGET / HTTP/1.1\r\n\r\n"),
        IO::Memory.new,
      )
    end

    logs.check :info, "before"
    logs.entry.context.should be_empty
    logs.check :info, "after"
    logs.entry.context[:foo].should eq "bar"

    logs.check :info, "before"
    logs.entry.context.should be_empty
    logs.check :info, "after"
    logs.entry.context[:foo].should eq "bar"
  end
end