-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
82 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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,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 = {}, {} | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |