diff options
Diffstat (limited to 'modules/lua/test')
-rw-r--r-- | modules/lua/test/helpers.lua | 36 | ||||
-rw-r--r-- | modules/lua/test/htdocs/config_tests.lua | 37 | ||||
-rw-r--r-- | modules/lua/test/htdocs/filters.lua | 7 | ||||
-rw-r--r-- | modules/lua/test/htdocs/find_me.txt | 1 | ||||
-rw-r--r-- | modules/lua/test/htdocs/headers.lua | 6 | ||||
-rw-r--r-- | modules/lua/test/htdocs/hooks.lua | 29 | ||||
-rw-r--r-- | modules/lua/test/htdocs/other.lua | 21 | ||||
-rw-r--r-- | modules/lua/test/htdocs/simple.lua | 4 | ||||
-rw-r--r-- | modules/lua/test/htdocs/test.lua | 129 | ||||
-rw-r--r-- | modules/lua/test/lib/kangaroo.lua | 19 | ||||
-rw-r--r-- | modules/lua/test/moonunit.lua | 52 | ||||
-rwxr-xr-x | modules/lua/test/test.lua | 126 | ||||
-rw-r--r-- | modules/lua/test/test_httpd.conf | 31 |
13 files changed, 498 insertions, 0 deletions
diff --git a/modules/lua/test/helpers.lua b/modules/lua/test/helpers.lua new file mode 100644 index 0000000..79bd269 --- /dev/null +++ b/modules/lua/test/helpers.lua @@ -0,0 +1,36 @@ +module("helpers", package.seeall) + +local io = require("io") +local http = require("socket.http") +local string = require("string") + +base_url = "http://localhost" + +function get(uri) + return http.request(base_url .. uri) +end + +function post(uri, body) + local function do_it(body) + local flat + if (type(body) == "table") then + i = 1 + for k, v in pairs(body) do + if i == 1 then + flat = k .. "=" ..v + else + flat = flat .. "&" .. k .. "=" .. v + end + i = i + 1 + end + else + flat = body; + end + return http.request(base_url .. uri, flat) + end + if body then + return do_it(body) + else + return do_it + end +end
\ No newline at end of file diff --git a/modules/lua/test/htdocs/config_tests.lua b/modules/lua/test/htdocs/config_tests.lua new file mode 100644 index 0000000..698bedf --- /dev/null +++ b/modules/lua/test/htdocs/config_tests.lua @@ -0,0 +1,37 @@ +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +require 'string' + +local count = 0 + +function handle(r) + r:puts("success in handle " .. count) +end + +function handle_server_vm(r) + r:puts("hello from server scope " .. count) + count = count + 1 +end + +function handle_request_vm(r) + r:puts("hello from request scope " .. count) + count = count + 1 +end + +function handle_conn_vm(r) + r:puts("hello from request scope " .. count) + count = count + 1 +end
\ No newline at end of file diff --git a/modules/lua/test/htdocs/filters.lua b/modules/lua/test/htdocs/filters.lua new file mode 100644 index 0000000..cad0ca8 --- /dev/null +++ b/modules/lua/test/htdocs/filters.lua @@ -0,0 +1,7 @@ + +local s = require 'string' + +function handle_simple(r) + -- r:addoutputfilter("wombathood") + r:puts("added wombathood") +end
\ No newline at end of file diff --git a/modules/lua/test/htdocs/find_me.txt b/modules/lua/test/htdocs/find_me.txt new file mode 100644 index 0000000..cb96e9e --- /dev/null +++ b/modules/lua/test/htdocs/find_me.txt @@ -0,0 +1 @@ +please find me
\ No newline at end of file diff --git a/modules/lua/test/htdocs/headers.lua b/modules/lua/test/htdocs/headers.lua new file mode 100644 index 0000000..35938ea --- /dev/null +++ b/modules/lua/test/htdocs/headers.lua @@ -0,0 +1,6 @@ +function handle(r) + local host = r.headers_in['host'] + r:debug(host) + r:puts(host) + r.headers_out['wombat'] = 'lua' +end diff --git a/modules/lua/test/htdocs/hooks.lua b/modules/lua/test/htdocs/hooks.lua new file mode 100644 index 0000000..b8a8248 --- /dev/null +++ b/modules/lua/test/htdocs/hooks.lua @@ -0,0 +1,29 @@ +require 'string' +require 'apache2' + +function translate_name(r) + if r.uri == "/translate-name" then + r.uri = "/find_me.txt" + return apache2.DECLINED + end + return apache2.DECLINED +end + +function translate_name2(r) + if r.uri == "/translate-name2" then + r.uri = "/find_me.txt" + return apache2.DECLINED + end + return apache2.DECLINED +end + +function fixups_test(r) + -- r:err("KABAZ") + if r.uri == "/test_fixupstest" then + -- r:err("KABIZ") + r.status = 201 + return apache2.OK + end + -- r:err("ZIBAK") + return apache2.DECLINED +end
\ No newline at end of file diff --git a/modules/lua/test/htdocs/other.lua b/modules/lua/test/htdocs/other.lua new file mode 100644 index 0000000..90c6ed2 --- /dev/null +++ b/modules/lua/test/htdocs/other.lua @@ -0,0 +1,21 @@ +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +module("other") + +function doit(r) + r:debug("doing it...") + r:puts("Do It!\n") +end diff --git a/modules/lua/test/htdocs/simple.lua b/modules/lua/test/htdocs/simple.lua new file mode 100644 index 0000000..a3f8861 --- /dev/null +++ b/modules/lua/test/htdocs/simple.lua @@ -0,0 +1,4 @@ +function handle(r) + r.content_type = "text/plain" + r:puts("Hi there!") +end diff --git a/modules/lua/test/htdocs/test.lua b/modules/lua/test/htdocs/test.lua new file mode 100644 index 0000000..c0b96ab --- /dev/null +++ b/modules/lua/test/htdocs/test.lua @@ -0,0 +1,129 @@ +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +require 'string' + +function print_args(r, simple, complex) + local s = " %s: %s\n" + r:puts(" simple:\n") + for k, v in pairs(simple) do + r:puts(s:format(k, v)) + end + + s = " %s: " + r:puts(" complex:\n") + for k, ary in pairs(complex) do + r:puts(s:format(k)) + for i=1, #ary do + r:puts(ary[i]) + if i < #ary then r:puts(", ") end + end + r:puts("\n") + end +end + +function debug_stuff(r) + r:debug("This is a debug log message") + -- r:info("This is an info log message") + -- r:notice("This is an notice log message") + -- r:warn("This is an warn log message") + -- r:err("This is an err log message") + -- r:alert("This is an alert log message") + -- r:crit("This is an crit log message") + -- r:emerg("This is an emerg log message") +end + +function handle(r) + r:puts("hello Lua world\n") + r:puts("Query args:\n") + + print_args(r, r:parseargs()); + + debug_stuff(r) + + r:puts("HTTP Method:\n " .. r.method .. "\n") + + if r.method == 'POST' then + print_args(r, r:parsebody()) + end + + require("other") + r:puts("loaded relative to script:\n ") + other.doit(r) + + r:puts("loaded from LuaPackagePath:\n") + require("kangaroo"); + kangaroo.hop(r); +end + +function handle_foo(r) + r:puts("Handler FOO!\n") + r.status = 201 + r:debug("set status to 201") +end + + +function handle_attributes(r) + local function pf(name) + r:puts(("%s: %s\n"):format(name, tostring(r[name]))) + end + + pf("status") + r.status = 201 + pf("status") + r:puts("\n") + + pf("content_type") + r.content_type = "text/plain?charset=ascii" + pf("content_type") + r:puts("\n") + + pf("method") + pf("protocol") + pf("assbackwards") + pf("the_request") + pf("range") + pf("content_encoding") + pf("user") + pf("unparsed_uri") + pf("ap_auth_type") + pf("uri") + pf("filename") + pf("canonical_filename") + pf("path_info") + pf("args") + + r:puts("\n") +end + +function test_headers(r) + r:puts("test getting and setting headers here\n") +end + +function handle_quietly(r) + r:puts("hello!") +end + +function handle_regex(r) + r:puts("matched in handle_regex") +end + +function handle_serverversion(r) + r:puts(apache2.version) +end + +function handle_fixupstest(r) + r:puts("status is " .. r.status) +end
\ No newline at end of file diff --git a/modules/lua/test/lib/kangaroo.lua b/modules/lua/test/lib/kangaroo.lua new file mode 100644 index 0000000..00ba3e5 --- /dev/null +++ b/modules/lua/test/lib/kangaroo.lua @@ -0,0 +1,19 @@ +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +module("kangaroo") + +function hop(r) + r:puts(" hop hop!\n") +end diff --git a/modules/lua/test/moonunit.lua b/modules/lua/test/moonunit.lua new file mode 100644 index 0000000..8537a1e --- /dev/null +++ b/modules/lua/test/moonunit.lua @@ -0,0 +1,52 @@ +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +module("moonunit", package.seeall) + +TestCase = {} + +function TestCase:new(it) + it = it or {} + setmetatable(it, self) + self.__index = self + return it +end + +function TestCase:run(args) + args = args or arg + local function run_test(t, name) + local status, err = pcall(t, self) + if status then + print(("%-39s \27[32mpass\27[39m"):format("[" .. name .. "]")) + else + print(("%-39s \27[31mFAIL\27[39m %s"):format("[" .. name .. "]", err)) + end + end + + if (args and #args > 0) then + for _, v in ipairs(args) do + if type(self[v]) == "function" then + run_test(self[v], v) + else + print(("%-39s FAIL %s"):format("[" .. v .. "]", + "'" .. v .. "' doesn't appear to be a test function")) + end + end + else + for k, v in pairs(self) do + run_test(v, k) + end + end +end diff --git a/modules/lua/test/test.lua b/modules/lua/test/test.lua new file mode 100755 index 0000000..25da4d9 --- /dev/null +++ b/modules/lua/test/test.lua @@ -0,0 +1,126 @@ +#!/usr/bin/env lua + +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +local mu = require "moonunit" +local http = require "helpers" + +http.base_url = "http://localhost:8008" + +local test = mu.TestCase:new{} + +function test:document_root() + local b, c = http.get "/document_root.lua" + assert(200 == c, "expected status code 200, got " .. c) + assert(b:find("test"), "test not found in document root") +end + +function test:basic_get() + local b, c = http.get "/basic" + assert(200 == c, "expected status code 200, got " .. c) + assert(b:find("hello Lua world"), "'hello Lua world' not found in response") +end + +function test:quietly() + local b, c = http.get "/test_quietly" + assert(200 == c, "unexpected response code " .. c) + assert(b == 'hello!', "unexpected response body [" .. b .. "]") +end + +function test.basic_post() + local b, c = http.post "/basic" "hello=7&hello=1" + assert(200 == c, "expected status code 200, got " .. c) + assert(b:find("complex:%s+hello: 7, 1\n"), "didn't find complex post parsing") + assert(b:find("simple:%s+hello: 7\n"), "didn't find simple post parsing") +end + +function test.basic_post_alt() + local b, c = http.post("/test_foo", "hello=7&hello=1") + assert(201 == c, "expected status code 200, got " .. c) + assert(b:find("Handler FOO!"), "unexpected output!") +end + +function test.post_with_table() + local b, c = http.post "/basic" { hello = "7" } + assert(200 == c, "expected status code 200, got " .. c) + assert(b:find("hello: 7"), "didn't get expected post data [" .. b .."]") + + b, c = http.post "/basic" { hello = "7", goodbye = "8" } + + assert(200 == c, "expected status code 200, got " .. c) + assert(b:find("hello: 7"), "didn't get expected post data [" .. b .."]") + assert(b:find("goodbye: 8"), "didn't get expected post data [" .. b .."]") +end + +function test:simple_filter() + local b, c = http.get "/filter/simple" + assert(200 == c, "expected status code 200, got " .. c) +end + +function test:request_attributes() + local r, c = http.get "/test_attributes?yes=no" + assert(201 == c, "expected status code 201, got " .. c) + + assert(r:find("status: 200\nstatus: 201"), "changing status code failed") + assert(r:find("method: GET"), "method wasn't reported correctly") + assert(r:find("protocol: HTTP/1.1"), "protocol reported incorrectly") + assert(r:find("assbackwards: false"), "assbackwards reported incorrectly") + assert(r:find("args: yes=no"), "args not reported correctly") +end + +function test:map_regex() + local r, c = http.get "/test_regex" + assert(200 == c, "expected status code 200, got " .. c) + assert(r:find("matched in handle_regex"), "didn't find 'matched in handle_regex'") +end + +function test:map_regex2() + local r, c = http.get "/test_regex?a=8" + assert(200 == c, "expected status code 200, got " .. c) + assert(r:find("matched in handle_regex"), "didn't find 'matched in handle_regex'") +end + +function test:translate_name_hook() + local r, c = http.get "/translate-name" + assert(200 == c, "expected 200 got " .. c) + assert(r:find("please find me"), "didn't get expected static file :-(, instead got " .. r) +end + +function test:translate_name_hook2() + local r, c = http.get "/translate-name2" + assert(200 == c, "expected 200 got " .. c) + assert(r:find("please find me"), "didn't get expected static file :-(, instead got " .. r) +end + +function test:server_version() + local r, c = http.get "/test_serverversion" + assert(200 == c) + assert(r:find("Apache/2"), "version isn't Apache/2, but is " .. r) +end + +function test:fixups_hook() + local r, c = http.get "/test_fixupstest" + assert(201 == c, "incorrect status code returned, expected 201 got " .. c) + assert(r:find("status is 201"), "handler sees incorrect status") +end + +function test:simple() + local r, c = http.get "/simple.lua" + assert(200 == c, "incorrect status code returned, expected 200 got " .. c) + assert(r:find("Hi"), "Didn't find 'Hi'") +end + +test:run() diff --git a/modules/lua/test/test_httpd.conf b/modules/lua/test/test_httpd.conf new file mode 100644 index 0000000..27e3a50 --- /dev/null +++ b/modules/lua/test/test_httpd.conf @@ -0,0 +1,31 @@ +# Customize these two values for your Apache2 install +ServerRoot "/Users/brianm/.opt/httpd-2.2.3-worker-for-lua" +DocumentRoot "/Users/brianm/src/wombat/test/htdocs" + +# Customize this value to point to the top of mod_wombat's test dir +LuaRoot /Users/brianm/src/wombat/test + +Listen 8000 + +LoadModule lua_module modules/mod_lua.so + +AddHandler lua-script .lua + +#LuaConfig httpd_config.lua configure + +LuaMapHandler /basic /Users/brianm/src/wombat/test/htdocs/test.lua +LuaMapHandler /filter/simple /Users/brianm/src/wombat/test/htdocs/filters.lua handle_simple +LuaMapHandler ^/(\w+)_(\w+)$ /Users/brianm/src/wombat/test/htdocs/$1.lua handle_$2 + +LuaHookTranslateName htdocs/hooks.lua translate_name +LuaHookTranslateName htdocs/hooks.lua translate_name2 + +LuaHookFixups htdocs/hooks.lua fixups_test + +LuaPackagePath lib/?.lua + +# stat | forever | never +LuaCodeCache stat + +ErrorLog logs/error_log +LogLevel debug |