File: static_file_handler_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 (290 lines) | stat: -rw-r--r-- 11,737 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
require "../../../spec_helper"
require "http/server/handler"
require "http/client/response"

private def handle(request, fallthrough = true, directory_listing = true, ignore_body = false, decompress = true)
  io = IO::Memory.new
  response = HTTP::Server::Response.new(io)
  context = HTTP::Server::Context.new(request, response)
  handler = HTTP::StaticFileHandler.new datapath("static_file_handler"), fallthrough, directory_listing
  handler.call context
  response.close
  io.rewind
  HTTP::Client::Response.from_io(io, ignore_body, decompress)
end

describe HTTP::StaticFileHandler do
  file_text = File.read datapath("static_file_handler", "test.txt")

  it "serves a file" do
    response = handle HTTP::Request.new("GET", "/test.txt"), ignore_body: false
    response.status_code.should eq(200)
    response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
  end

  it "handles forbidden characters in windows paths" do
    response = handle HTTP::Request.new("GET", "/foo\\bar.txt"), ignore_body: false
    response.status_code.should eq 404

    # This file can't be checkout out from git on Windows, thus we need to create it here.
    File.touch(Path[datapath("static_file_handler"), Path.posix("back\\slash.txt")])
    response = handle HTTP::Request.new("GET", "/back\\slash.txt"), ignore_body: false
    response.status_code.should eq 200
  end

  it "adds Etag header" do
    response = handle HTTP::Request.new("GET", "/test.txt")
    response.headers["Etag"].should match(/W\/"\d+"$/)
  end

  it "adds Last-Modified header" do
    response = handle HTTP::Request.new("GET", "/test.txt")
    modification_time = File.info(datapath("static_file_handler", "test.txt")).modification_time
    HTTP.parse_time(response.headers["Last-Modified"]).should eq(modification_time.at_beginning_of_second)
  end

  context "with If-Modified-Since header" do
    it "returns 304 Not Modified for equal to Last-Modified" do
      initial_response = handle HTTP::Request.new("GET", "/test.txt")

      headers = HTTP::Headers.new
      headers["If-Modified-Since"] = initial_response.headers["Last-Modified"]

      response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: true
      response.status_code.should eq(304)

      response.headers["Last-Modified"].should eq initial_response.headers["Last-Modified"]
      response.headers["Content-Type"]?.should be_nil
      response.body.should eq ""
    end

    it "returns 304 Not Modified for younger than Last-Modified" do
      initial_response = handle HTTP::Request.new("GET", "/test.txt")
      last_modified = HTTP.parse_time(initial_response.headers["Last-Modified"]).not_nil!

      headers = HTTP::Headers.new
      headers["If-Modified-Since"] = HTTP.format_time(last_modified + 1.hour)
      response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: true

      response.headers["Last-Modified"].should eq initial_response.headers["Last-Modified"]
      response.status_code.should eq(304)
      response.body.should eq ""
    end

    it "serves content for older than Last-Modified" do
      initial_response = handle HTTP::Request.new("GET", "/test.txt")
      last_modified = HTTP.parse_time(initial_response.headers["Last-Modified"]).not_nil!

      headers = HTTP::Headers.new
      headers["If-Modified-Since"] = HTTP.format_time(last_modified - 1.hour)
      response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: false

      response.headers["Last-Modified"].should eq initial_response.headers["Last-Modified"]
      response.status_code.should eq(200)
      response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
    end
  end

  context "with If-None-Match header" do
    it "returns 304 Not Modified if header matches etag" do
      initial_response = handle HTTP::Request.new("GET", "/test.txt")

      headers = HTTP::Headers.new
      headers["If-None-Match"] = initial_response.headers["Etag"]
      response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: true
      response.status_code.should eq(304)
    end

    it "serves file if header does not match etag" do
      headers = HTTP::Headers.new
      headers["If-None-Match"] = "some random etag"

      response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: false
      response.status_code.should eq(200)
      response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
    end

    it "returns 304 Not Modified if header is *" do
      headers = HTTP::Headers.new
      headers["If-None-Match"] = "*"
      response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: true
      response.status_code.should eq(304)
    end

    it "serves file if header is empty" do
      headers = HTTP::Headers.new
      headers["If-None-Match"] = ""

      response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: false
      response.status_code.should eq(200)
      response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
    end

    it "serves file if header does not contain valid etag" do
      headers = HTTP::Headers.new
      headers["If-None-Match"] = ", foo"

      response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: false
      response.status_code.should eq(200)
      response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
    end
  end

  context "with multiple If-None-Match header" do
    it "returns 304 Not Modified if at least one header matches etag" do
      initial_response = handle HTTP::Request.new("GET", "/test.txt")

      headers = HTTP::Headers.new
      headers["If-None-Match"] = %(,, ,W/"1234567"   , , #{initial_response.headers["Etag"]},"12345678",%)
      response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: true
      response.status_code.should eq(304)
    end

    it "serves file if no header matches etag" do
      headers = HTTP::Headers.new
      headers["If-None-Match"] = "some random etag, 1234567"

      response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: false
      response.status_code.should eq(200)
      response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
    end
  end

  context "with both If-None-Match and If-Modified-Since headers" do
    it "ignores If-Modified-Since as specified in RFC 7232" do
      initial_response = handle HTTP::Request.new("GET", "/test.txt")

      headers = HTTP::Headers.new
      headers["If-Modified-Since"] = HTTP.format_time(File.info(datapath("static_file_handler", "test.txt")).modification_time - 1.hour)
      headers["If-None-Match"] = initial_response.headers["Etag"]
      response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: true

      response.status_code.should eq(304)
    end

    it "serves a file if header does not match etag even If-Modified-Since is fresh" do
      initial_response = handle HTTP::Request.new("GET", "/test.txt")

      headers = HTTP::Headers.new
      headers["If-Modified-Since"] = initial_response.headers["Last-Modified"]
      headers["If-None-Match"] = "some random etag"
      response = handle HTTP::Request.new("GET", "/test.txt", headers), ignore_body: false

      response.status_code.should eq(200)
      response.body.should eq(File.read(datapath("static_file_handler", "test.txt")))
    end
  end

  it "lists directory's entries" do
    response = handle HTTP::Request.new("GET", "/")
    response.status_code.should eq(200)
    response.body.should match(/test.txt/)
  end

  it "does not list directory's entries when directory_listing is set to false" do
    response = handle HTTP::Request.new("GET", "/"), directory_listing: false
    response.status_code.should eq(404)
  end

  it "does not serve a not found file" do
    response = handle HTTP::Request.new("GET", "/not_found_file.txt")
    response.status_code.should eq(404)
  end

  it "does not serve a not found directory" do
    response = handle HTTP::Request.new("GET", "/not_found_dir/")
    response.status_code.should eq(404)
  end

  it "does not serve a file as directory" do
    response = handle HTTP::Request.new("GET", "/test.txt/")
    response.status_code.should eq(404)
  end

  it "handles only GET and HEAD method" do
    %w(GET HEAD).each do |method|
      response = handle HTTP::Request.new(method, "/test.txt")
      response.status_code.should eq(200)
    end

    %w(POST PUT DELETE).each do |method|
      response = handle HTTP::Request.new(method, "/test.txt")
      response.status_code.should eq(404)
      response = handle HTTP::Request.new(method, "/test.txt"), false
      response.status_code.should eq(405)
      response.headers["Allow"].should eq("GET, HEAD")
    end
  end

  it "expands a request path" do
    %w(../test.txt ../../test.txt test.txt/../test.txt a/./b/../c/../../test.txt).each do |path|
      response = handle HTTP::Request.new("GET", "/#{path}")
      response.status_code.should eq(302)
      response.headers["Location"].should eq("/test.txt")
    end

    # directory
    %w(.. ../ ../.. a/.. a/.././b/../).each do |path|
      response = handle HTTP::Request.new("GET", "/#{path}")
      response.status_code.should eq(302)
      response.headers["Location"].should eq("/")
    end
  end

  it "unescapes a request path" do
    %w(test%2Etxt %74%65%73%74%2E%74%78%74).each do |path|
      response = handle HTTP::Request.new("GET", "/#{path}")
      response.status_code.should eq(200)
      response.body.should eq(file_text)
    end

    %w(%2E%2E/test.txt found%2F%2E%2E%2Ftest%2Etxt).each do |path|
      response = handle HTTP::Request.new("GET", "/#{path}")
      response.status_code.should eq(302)
      response.headers["Location"].should eq("/test.txt")
    end
  end

  it "returns 400" do
    %w(%00 test.txt%00).each do |path|
      response = handle HTTP::Request.new("GET", "/#{path}")
      response.status_code.should eq(400)
    end
  end

  it "handles invalid redirect path" do
    response = handle HTTP::Request.new("GET", "test.txt%0A")
    response.status_code.should eq(302)
    response.headers["Location"].should eq "/test.txt%0A"

    response = handle HTTP::Request.new("GET", "/test.txt%0A")
    response.status_code.should eq(404)
  end

  it "serve compressed content" do
    modification_time = File.info(datapath("static_file_handler", "test.txt")).modification_time
    File.touch datapath("static_file_handler", "test.txt.gz"), modification_time + 1.second

    headers = HTTP::Headers{"Accept-Encoding" => "gzip"}
    response = handle HTTP::Request.new("GET", "/test.txt", headers), decompress: false
    response.headers["Content-Encoding"].should eq("gzip")
  end

  it "still serve compressed content when modification time is very close" do
    modification_time = File.info(datapath("static_file_handler", "test.txt")).modification_time
    File.touch datapath("static_file_handler", "test.txt.gz"), modification_time - 1.microsecond

    headers = HTTP::Headers{"Accept-Encoding" => "gzip"}
    response = handle HTTP::Request.new("GET", "/test.txt", headers), decompress: false
    response.headers["Content-Encoding"].should eq("gzip")
  end

  it "doesn't serve compressed content if older than raw file" do
    modification_time = File.info(datapath("static_file_handler", "test.txt")).modification_time
    File.touch datapath("static_file_handler", "test.txt.gz"), modification_time - 1.second

    headers = HTTP::Headers{"Accept-Encoding" => "gzip"}
    response = handle HTTP::Request.new("GET", "/test.txt", headers)
    response.headers["Content-Encoding"]?.should be_nil
  end
end