diff --git a/doc/acid/apiutil.md b/doc/acid/apiutil.md new file mode 100644 index 0000000..8d0901e --- /dev/null +++ b/doc/acid/apiutil.md @@ -0,0 +1,74 @@ + + +# Table of Content + +- [Name](#name) +- [Status](#status) +- [Description](#description) +- [Synopsis](#synopsis) +- [Methods](#methods) + - [apiutil.output](#apiutiloutput) +- [Author](#author) +- [Copyright and License](#copyright-and-license) + + + +# Name + +acid.apiutil + +# Status + +This library is considered production ready. + +# Description + +It provides common ngx output functions. + +# Synopsis + +```lua +local apiutil = require("acid.apiutil") + +code = 200 +headers = {['Content-Type']='application/json'} +body = 'hello' + +apiutil.output(code,headers,body) +``` + +# Methods + + +## apiutil.output + +**syntax**: +`apiutil.output(code, headers, body)` + +**arguments**: + +- `code`: + is a integer. + +- `headers`: + is a table. + + like `{["X-My-Header"] = 'blah blah', ['Set-Cookie'] = {'a=32; path=/', 'b=4; path=/'}}`. + +- `body`: + can be str or nil or boolean or nested array table. + + See also [ngx.print](https://github.com/openresty/lua-nginx-module#ngxprint) + +**return**: +Nothing + +# Author + +Liu Tongwei(刘桐伟) + +# Copyright and License + +The MIT License (MIT) + +Copyright (c) 2017 Liu Tongwei(刘桐伟) diff --git a/lib/acid/apiutil.lua b/lib/acid/apiutil.lua new file mode 100644 index 0000000..0c131e5 --- /dev/null +++ b/lib/acid/apiutil.lua @@ -0,0 +1,19 @@ +local _M = {} + +local ngx = ngx + +function _M.output(code, headers, body) + + ngx.status = code + + for k, v in pairs(headers) do + ngx.header[k] = v + end + + ngx.print(body) + ngx.eof() + + ngx.exit(ngx.HTTP_OK) +end + +return _M diff --git a/t/apiutil.t b/t/apiutil.t new file mode 100644 index 0000000..d15ef29 --- /dev/null +++ b/t/apiutil.t @@ -0,0 +1,64 @@ +# vim:set ft=lua ts=4 sw=4 et ft=perl: + +################################################################################ +# DO NOT EDIT THIS FILE. # +# Use ./t/build_ngx_ut.sh to regenerate this wrapper file. # +################################################################################ + +use Test::Nginx::Socket "no_plan"; + +no_long_string(); + +# Env TEST_VERBOSE is set by command "prove" +# +# Only env var starting with TEST_NGINX_ will be evaluated in the "--- config" +# block. +$ENV{TEST_NGINX_ACID_UT_VERBOSE} = $ENV{TEST_VERBOSE} || 0; + +run_tests(); + +__DATA__ + +=== TEST 1: apiutil +--- http_config + lua_shared_dict shared_dict_lock 1m; + lua_shared_dict test_shared 10m; + lua_check_client_abort on; + + lua_package_path "./lib/?.lua;;"; + lua_package_cpath "./lib/?.so;;"; +--- config + location /t { + content_by_lua_block { + local apiutil = require("acid.apiutil") + local strutil = require("acid.strutil") + local args = ngx.req.get_uri_args() + code = args['code'] + body = args['body'] + headers_split = strutil.split(args['headers'],",") + headers = {} + for i, v in ipairs(headers_split) do + key, value = unpack(strutil.split(v,"=")) + headers[key] = value + end + ngx.log(ngx.ERROR,apiutil.output(code,headers,body)) + } + } +--- pipelined_requests eval +['GET /t?code=200&headers=Content-Type=application/json,Content-Encoding=gzip,a=b&body=123456', +'GET /t?code=500&headers=Content-Type=application/xml&body=12345aaa6', +'GET /t?code=404&headers=Content-Type=application/xml&body=123456'] +--- response_body eval +['123456', +'12345aaa6', +'123456'] +--- error_code eval +[200, +500, +404] +--- response_headers eval +['Content-Type: application/json +Content-Encoding: gzip +a=b', +'Content-Type: application/xml', +'Content-Type: application/xml']