From 1ac5fdbe0688d86da7eaf143bec46b7ddb022ea8 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Wed, 27 Dec 2023 22:55:29 +0100 Subject: [PATCH] test(files): add tests for files plugin --- spec/unit/files_spec.lua | 127 ++++++++++++++++++++++++++++++++++ src/pegasus/plugins/files.lua | 1 + 2 files changed, 128 insertions(+) create mode 100644 spec/unit/files_spec.lua diff --git a/spec/unit/files_spec.lua b/spec/unit/files_spec.lua new file mode 100644 index 0000000..2489c2b --- /dev/null +++ b/spec/unit/files_spec.lua @@ -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) diff --git a/src/pegasus/plugins/files.lua b/src/pegasus/plugins/files.lua index daeb7b7..c0a3eb2 100644 --- a/src/pegasus/plugins/files.lua +++ b/src/pegasus/plugins/files.lua @@ -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