Skip to content

Commit

Permalink
test(files): add tests for files plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Dec 27, 2023
1 parent 3825155 commit 1ac5fdb
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
127 changes: 127 additions & 0 deletions spec/unit/files_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
describe("Files plugin", function()

local Files = require "pegasus.plugins.files"



describe("instantiation", function()

local options = {}
local plugin = Files:new(options)

it("should return a table", function()
assert.is.table(plugin)
end)


it("should have a default location; '.'", function()
assert.is.equal(".", plugin.location)
end)


it("should have a default; '/index.html'", function()
assert.is.equal("/index.html", plugin.default)
end)

end)



describe("invocation", function()

local redirect_called, writeFile_called
local request = {}
local response = {
redirect = function(self, ...) redirect_called = {...} end,
writeFile = function(self, ...) writeFile_called = {...} return self end,
-- finish = function(self, ...) end,
-- setHeader = function(self, ...) end,
-- setStatusCode = function(self, ...) end,
}

before_each(function()
redirect_called = nil
writeFile_called = nil
end)


it("handles GET", function()
stub(request, "path", function() return "/some/file.html" end)
stub(request, "method", function() return "GET" end)
local stop = Files:new():newRequestResponse(request, response)
assert.is.True(stop)
assert.is.Nil(redirect_called)
assert.are.same({
"./some/file.html",
"text/html"
}, writeFile_called)
end)

it("handles HEAD", function()
stub(request, "path", function() return "/some/file.html" end)
stub(request, "method", function() return "HEAD" end)
local stop = Files:new():newRequestResponse(request, response)
assert.is.True(stop)
assert.is.Nil(redirect_called)
assert.are.same({
"./some/file.html",
"text/html"
}, writeFile_called)
end)

it("doesn't handle POST", function()
stub(request, "path", function() return "/some/file.html" end)
stub(request, "method", function() return "POST" end)
local stop = Files:new():newRequestResponse(request, response)
assert.is.False(stop)
assert.is.Nil(redirect_called)
assert.is.Nil(writeFile_called)
end)

it("doesn't handle PUT", function()
stub(request, "path", function() return "/some/file.html" end)
stub(request, "method", function() return "PUT" end)
local stop = Files:new():newRequestResponse(request, response)
assert.is.False(stop)
assert.is.Nil(redirect_called)
assert.is.Nil(writeFile_called)
end)

it("redirects GET /", function()
stub(request, "path", function() return "/" end)
stub(request, "method", function() return "GET" end)
local stop = Files:new():newRequestResponse(request, response)
assert.is.True(stop)
assert.are.same({
"/index.html"
}, redirect_called)
assert.is.Nil(writeFile_called)
end)

it("serves from specified location", function()
stub(request, "path", function() return "/some/file.html" end)
stub(request, "method", function() return "GET" end)
local stop = Files:new({ location = "./location" }):newRequestResponse(request, response)
assert.is.True(stop)
assert.is.Nil(redirect_called)
assert.are.same({
"./location/some/file.html",
"text/html"
}, writeFile_called)
end)

it("forces location to be relative", function()
stub(request, "path", function() return "/some/file.html" end)
stub(request, "method", function() return "GET" end)
local stop = Files:new({ location = "/location" }):newRequestResponse(request, response)
assert.is.True(stop)
assert.is.Nil(redirect_called)
assert.are.same({
"./location/some/file.html",
"text/html"
}, writeFile_called)
end)

end)

end)
1 change: 1 addition & 0 deletions src/pegasus/plugins/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function Files:new(options)

local location = options.location or ""
if location:sub(1,2) ~= "./" then
-- make sure it's a relative path, forcefully!
if location:sub(1,1) == "/" then
location = "." .. location
else
Expand Down

0 comments on commit 1ac5fdb

Please sign in to comment.