/usr/share/lua/5.1/wsapi/mock.lua is in lua-wsapi 1.6.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | -----------------------------------------------------------------------------
-- Mock WSAPI handler for Unit testing
--
-- Author: Norman Clarke
-- Copyright (c) 2010 Kepler Project
--
-----------------------------------------------------------------------------
local _M = {}
local common = require "wsapi.common"
local request = require "wsapi.request"
-- Build a request that looks like something that would come from a real web
-- browser.
local function build_request(method, path, headers)
local req = {
GATEWAY_INTERFACE = "CGI/1.1",
HTTP_ACCEPT = "application/xml,application/xhtml+xml,text/html;q=0.9," ..
"text/plain;q=0.8,image/png,*/*;q=0.5",
HTTP_ACCEPT_CHARSET = "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
HTTP_ACCEPT_ENCODING = "gzip,deflate,sdch",
HTTP_ACCEPT_LANGUAGE = "en-US,en;q=0.8",
HTTP_CACHE_CONTROL = "max-age=0",
HTTP_CONNECTION = "keep-alive",
HTTP_HOST = "127.0.0.1:80",
HTTP_USER_AGENT = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X " ..
"10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) " ..
"Chrome/6.0.472.55",
HTTP_VERSION = "HTTP/1.1",
REMOTE_ADDR = "127.0.0.1",
REMOTE_HOST = "localhost",
SCRIPT_NAME = "wsapi_test",
SERVER_NAME = "localhost",
SERVER_PORT = "80",
SERVER_PROTOCOL = "HTTP/1.1"
}
req.PATH_INFO = path
req.REQUEST_METHOD = method:upper()
req.METHOD = req.REQUEST_METHOD
req.REQUEST_PATH = "/"
if req.PATH_INFO == "" then req.PATH_INFO = "/" end
for k, v in pairs(headers or {}) do req[k] = v end
-- allow case-insensitive table key access
setmetatable(req, {__index = function(t, k)
return rawget(t, string.upper(k))
end})
return req
end
-- Override common's output handler to avoid writing headers
-- in the reponse body.
function common.send_output(out, status, headers, res_iter, write_method,res_line)
common.send_content(out, res_iter, "write")
end
-- Mock IO objects
local function make_io_object(content)
local receiver = { buffer = { content }, bytes_read = 0 }
function receiver:write(content)
self.buffer[#self.buffer + 1] = content
return true
end
function receiver:read(len)
-- first read will turn the buffer into a string
if type(self.buffer) == "table" then
self.buffer = table.concat(self.buffer)
end
len = len or (#self.buffer - self.bytes_read)
if self.bytes_read >= #self.buffer then return nil end
local s = self.buffer:sub(self.bytes_read + 1, self.bytes_read + len)
self.bytes_read = self.bytes_read + len
if self.bytes_read > #self.buffer then self.bytes_read = #self.buffer end
return s
end
function receiver:clear()
self.buffer = {}
self.bytes_read = 0
end
function receiver:reset()
self.bytes_read = 0
end
return receiver
end
-- Build a GET request
local function build_get(path, params, headers)
local req = build_request("GET", path, headers)
req.QUERY_STRING = request.methods.qs_encode(nil, params)
req.REQUEST_URI = "http://" ..
req.HTTP_HOST ..
req.PATH_INFO ..
req.QUERY_STRING
return {
env = req,
input = make_io_object(),
output = make_io_object(),
error = make_io_object()
}
end
local function build_post(path, params, headers)
local req = build_request("POST", path, headers)
req.REQUEST_URI = "http://" .. req.HTTP_HOST .. req.PATH_INFO
local body
if headers["Content-Type"] then
body = params
else
body = request.methods.qs_encode(nil, params):gsub("^?", "")
req.CONTENT_TYPE = "x-www-form-urlencoded"
end
-- if the path includes a query string, store it in the appropiate header
local qs = path:match("?(.+)$")
if qs then
req.QUERY_STRING = qs
end
req.CONTENT_LENGTH = headers["Content-Length"] or #body
return {
env = req,
input = make_io_object(body),
output = make_io_object(),
error = make_io_object()
}
end
local function make_request(request_builder, app, path, params, headers)
local wsapi_env = request_builder(path, params, headers)
local response = {}
response.code, response.headers = common.run(app, wsapi_env)
response.body = wsapi_env.output:read()
response.wsapi_errors = wsapi_env.error:read()
return response, wsapi_env.env
end
local function get(self, path, params, headers)
return make_request(build_get, self.app, path, params, headers)
end
local function post(self, path, params, headers)
return make_request(build_post, self.app, path, params, headers)
end
--- Creates a WSAPI handler for testing.
-- @param app The WSAPI application you want to test.
function _M.make_handler(app)
return {
app = app,
get = get,
post = post
}
end
return _M
|