-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathfile_server_spec.rb
More file actions
206 lines (157 loc) · 5.93 KB
/
Copy pathfile_server_spec.rb
File metadata and controls
206 lines (157 loc) · 5.93 KB
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
# frozen_string_literal: true
require "http"
RSpec.describe "File server" do
before :all do
skip("skipping file server tests") unless ENV["ENABLE_EXTERNAL_TESTS"] == "true"
end
subject { http.get(url) }
let(:http) { HTTP }
let(:url) { "http://localhost:3000/test.txt" }
context "with file server disabled" do
before :all do
launch_server
end
after :all do
stop_server
end
it "doesn't allow to access public assets" do
expect(subject.code).to eq(404)
end
end
context "with file server enabled" do
before :all do
launch_server(env: { "ENABLE_FILE_SERVER" => "1" })
end
after :all do
stop_server
end
it "allows to access public assets" do
expect(subject.code).to eq(200)
expect(subject.to_s).to eq("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n")
end
it "returns correct headers" do
expect(subject.headers["content-length"]).to eq("27")
expect(subject.headers["content-type"]).to eq("text/plain")
expect(subject.headers["etag"]).not_to be_empty
expect(subject.headers["last-modified"]).not_to be_empty
end
it "fallbacks to application routes" do
response = HTTP.get("http://localhost:3000/get")
expect(response.code).to eq(200)
expect(response.to_s).to eq("i am a get response")
end
context "with valid range" do
let(:http) { HTTP.headers(range: "bytes=5-9") }
it "returns correct response" do
expect(subject.code).to eq(206)
expect(subject.to_s).to eq("FGHIJ")
end
it "returns correct headers" do
expect(subject.headers["content-length"]).to eq("5")
expect(subject.headers["content-range"]).to eq("bytes 5-9/27")
end
end
context "with invalid range" do
let(:http) { HTTP.headers(range: "bytes=5-100") }
it "returns correct response" do
expect(subject.code).to eq(416)
end
it "returns correct headers" do
expect(subject.headers["content-range"]).to eq("bytes */27")
end
end
context "with If-None-Match" do
let(:http) { HTTP.headers(if_none_match: etag) }
context "with valid etag" do
let(:etag) { HTTP.get(url).headers["etag"] }
it "returns correct response" do
expect(subject.code).to eq(304)
end
end
context "with invalid etag" do
let(:etag) { "invalid-etag" }
it "returns correct response" do
expect(subject.code).to eq(200)
expect(subject.to_s).to eq("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n")
end
end
end
context "with If-Range" do
let(:http) { HTTP.headers(range: "bytes=5-9", if_range: etag) }
context "with valid etag" do
let(:etag) { HTTP.get(url).headers["etag"] }
it "returns correct response" do
expect(subject.code).to eq(206)
expect(subject.to_s).to eq("FGHIJ")
end
end
context "with invalid etag" do
let(:etag) { "invalid-etag" }
it "returns correct status code" do
expect(subject.code).to eq(200)
expect(subject.to_s).to eq("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n")
end
end
end
context "with URL outside public directory" do
let(:url) { "http://localhost:3000/../Gemfile" }
it "returns correct status code" do
expect(subject.code).to eq(404)
end
end
context "with shadowing an upgrade endpoint" do
let(:url) { "http://localhost:3000/sse/stream" }
before do
FileUtils.mkdir_p("spec/integration/test_app/public/sse")
File.write("spec/integration/test_app/public/sse/stream", "static stream file")
end
after do
FileUtils.rm_r("spec/integration/test_app/public/sse")
end
it "returns correct response" do
expect(subject.code).to eq(200)
expect(subject.to_s).to eq("static stream file")
end
end
context "with x-sendfile" do
it "renders file" do
response = HTTP.get("http://localhost:3000/static", params: { file: "/index.html.erb" })
expect(response.code).to eq(200)
expect(response.to_s).to eq("<p>index page</p>\n")
end
it "strips service headers" do
response = HTTP.get("http://localhost:3000/static", params: { file: "/index.html.erb" })
expect(response.headers).not_to include("x-sendfile")
expect(response.headers).not_to include("x-sendfile-root")
end
it "allows to customize response headers" do
response = HTTP.get("http://localhost:3000/static", params: { file: "/index.html.erb" })
expect(response.headers["cache-control"]).to eq("max-age=604800")
expect(response.headers["rage-custom-header"]).to eq("qwerty")
end
it "renders file from nested folder" do
response = HTTP.get("http://localhost:3000/static", params: { file: "/pages/create.html.erb" })
expect(response.code).to eq(200)
expect(response.to_s).to eq("<p>create page</p>\n")
end
it "renders 404 for unknown files" do
response = HTTP.get("http://localhost:3000/static", params: { file: "/unknown.html.erb" })
expect(response.code).to eq(404)
end
it "strips custom headers for 404 responses" do
response = HTTP.get("http://localhost:3000/static", params: { file: "/unknown.html.erb" })
expect(response.headers["cache-control"]).to eq("no-cache, max-age=0")
expect(response.headers).not_to include("rage-custom-header")
end
it "renders 404 for directories" do
response = HTTP.get("http://localhost:3000/static", params: { file: "/pages" })
expect(response.code).to eq(404)
end
it "doesn't allow to access files outside x-sendfile-root" do
response = HTTP.get("http://localhost:3000/static", params: { file: "../controllers/application_controller.rb" })
expect(response.code).to eq(404)
expect(response.to_s).to eq("404 Not Found")
end
end
end
end