From 7d4528d558c8d86d9ce66ce7e10d592ed5b954c1 Mon Sep 17 00:00:00 2001 From: Etiene Dalcol Date: Mon, 12 Oct 2015 01:06:22 +0200 Subject: [PATCH] fix(sailor): Path fixes on set_application_path and make_url. The application path was making openresty apps error because filename was nil. And make_url was giving bad results when apps were hosted on subfolders. New tests were also added to test make_url. --- src/sailor.lua | 20 ++++++--- test/dev-app/tests/unit/sailor.lua | 69 ++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 test/dev-app/tests/unit/sailor.lua diff --git a/src/sailor.lua b/src/sailor.lua index 2f0bb9b..fb96bf4 100755 --- a/src/sailor.lua +++ b/src/sailor.lua @@ -1,5 +1,5 @@ -------------------------------------------------------------------------------- --- sailor.lua, v0.4.12: core functionalities of the framework +-- sailor.lua, v0.4.13: core functionalities of the framework -- This file is a part of Sailor project -- Copyright (c) 2014 Etiene Dalcol -- License: MIT @@ -13,7 +13,7 @@ local sailor = { conf = conf.sailor, _COPYRIGHT = "Copyright (C) 2014-2015 Etiene Dalcol", _DESCRIPTION = "Sailor is a framework for creating MVC web applications.", - _VERSION = "Sailor 0.4.12", + _VERSION = "Sailor 0.4.13", } -- Loads Lua@client's settings from Sailor conf. @@ -56,10 +56,12 @@ end function sailor.set_application_path(r) if r.uri and r.filename then local filename = r.uri:match( "([^/]+)$") - sailor.path = r.filename:match("^@?(.-)/"..filename.."$") - else - sailor.path = lfs.currentdir() + if filename then + sailor.path = r.filename:match("^@?(.-)/"..filename.."$") + return + end end + sailor.path = lfs.currentdir() end -- Encapsulates request_rec functions inside the Page object @@ -67,6 +69,7 @@ end -- r: webserver's request object function sailor.init(r) sailor.set_application_path(r) + sailor.base_path = ((r.uri):match('^@?(.-)/index.lua$') or '') r.content_type = "text/html" local GET, GETMULTI = {}, {} @@ -94,7 +97,7 @@ function sailor.init(r) layout = conf.sailor.layout, title = conf.sailor.app_name, trace = {}, - base_path = ((r.uri):match('^@?(.-)/index.lua$') or '') + base_path = sailor.base_path } sailor.r = r lp.setoutfunc("page:print") @@ -347,11 +350,14 @@ end function sailor.make_url(route,params) params = params or {} local url = route - local base_path = ((sailor.r.uri):match('^@?(.-)/index.lua$') or '') + local base_path = sailor.base_path if base_path ~= '' then base_path = base_path..'/' end if conf.sailor.friendly_urls then + if base_path == '' then + base_path = '/' + end url = base_path..url for k,v in pairs(params) do url = url.."/"..k.."/"..v diff --git a/test/dev-app/tests/unit/sailor.lua b/test/dev-app/tests/unit/sailor.lua new file mode 100644 index 0000000..07f5bd9 --- /dev/null +++ b/test/dev-app/tests/unit/sailor.lua @@ -0,0 +1,69 @@ +local sailor = require "sailor" +local conf = require "conf.conf" + +describe("Testing Sailor core functions", function() + local base_path + setup(function() + + end) + + it("should create URLs accordingly without friendly urls", function() + conf.sailor.friendly_urls = false + + local url = sailor.make_url('test') + assert.is_equal('?r=test',url) + + url = sailor.make_url('test/etc') + assert.is_equal('?r=test/etc',url) + + url = sailor.make_url('test',{id = 5, name = 'a_test'}) + assert.is_equal('?r=test&id=5&name=a_test',url) + + url = sailor.make_url('test/etc',{id = 5, name = 'a_test'}) + assert.is_equal('?r=test/etc&id=5&name=a_test',url) + + base_path, sailor.base_path = sailor.base_path, '/sailor/test/dev-app' + url = sailor.make_url('test/etc') + assert.is_equal('/sailor/test/dev-app/?r=test/etc',url) + + url = sailor.make_url('test',{id = 5, name = 'a_test'}) + assert.is_equal('/sailor/test/dev-app/?r=test&id=5&name=a_test',url) + + url = sailor.make_url('test/etc',{id = 5, name = 'a_test'}) + assert.is_equal('/sailor/test/dev-app/?r=test/etc&id=5&name=a_test',url) + + url = sailor.make_url('test/etc',{id = 5, name = 'a_test'}) + assert.is_equal('/sailor/test/dev-app/?r=test/etc&id=5&name=a_test',url) + base_path, sailor.base_path = sailor.base_path, base_path + end) + + it("should create URLs accordingly with friendly urls", function() + conf.sailor.friendly_urls = true + + local url = sailor.make_url('test') + assert.is_equal('/test',url) + + url = sailor.make_url('test/etc') + assert.is_equal('/test/etc',url) + + url = sailor.make_url('test',{id = 5, name = 'a_test'}) + assert.is_equal('/test/id/5/name/a_test',url) + + url = sailor.make_url('test/etc',{id = 5, name = 'a_test'}) + assert.is_equal('/test/etc/id/5/name/a_test',url) + + base_path, sailor.base_path = sailor.base_path, '/sailor/test/dev-app' + url = sailor.make_url('test/etc') + assert.is_equal('/sailor/test/dev-app/test/etc',url) + + url = sailor.make_url('test',{id = 5, name = 'a_test'}) + assert.is_equal('/sailor/test/dev-app/test/id/5/name/a_test',url) + + url = sailor.make_url('test/etc',{id = 5, name = 'a_test'}) + assert.is_equal('/sailor/test/dev-app/test/etc/id/5/name/a_test',url) + + url = sailor.make_url('test/etc',{id = 5, name = 'a_test'}) + assert.is_equal('/sailor/test/dev-app/test/etc/id/5/name/a_test',url) + base_path, sailor.base_path = sailor.base_path, base_path + end) +end) \ No newline at end of file