Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions spec/integration/file_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,61 @@
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
1 change: 1 addition & 0 deletions spec/integration/test_app/app/assets/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>index page</p>
1 change: 1 addition & 0 deletions spec/integration/test_app/app/assets/pages/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>create page</p>
11 changes: 11 additions & 0 deletions spec/integration/test_app/app/controllers/static_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class StaticController < RageController::API
def index
headers["x-sendfile"] = params[:file]
headers["x-sendfile-root"] = Rage.root.join("app/assets").to_s

headers["cache-control"] = "max-age=604800"
headers["rage-custom-header"] = "qwerty"

head :ok
end
end
2 changes: 2 additions & 0 deletions spec/integration/test_app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
get "renderers/erb_over_sse", to: "renderers#erb_over_sse"
get "renderers/json", to: "renderers#json"

get "static", to: "static#index"

mount ->(_) { [200, {}, ""] }, at: "/admin"

namespace :api do
Expand Down