Skip to content

Commit

Permalink
fix(sailor): Path fixes on set_application_path and make_url.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Etiene committed Oct 11, 2015
1 parent 0d37dfc commit 7d4528d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/sailor.lua
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
-- License: MIT
Expand All @@ -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.
Expand Down Expand Up @@ -56,17 +56,20 @@ 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
-- Useful for posterior compatibility with other servers
-- 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 = {}, {}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions test/dev-app/tests/unit/sailor.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 7d4528d

Please sign in to comment.