Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions doc/acid/apiutil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
# Table of Content

- [Name](#name)
- [Status](#status)
- [Description](#description)
- [Synopsis](#synopsis)
- [Methods](#methods)
- [apiutil.output](#apiutiloutput)
- [Author](#author)
- [Copyright and License](#copyright-and-license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

# 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(刘桐伟) <tongwei.liu@baishancloud.com>

# Copyright and License

The MIT License (MIT)

Copyright (c) 2017 Liu Tongwei(刘桐伟) <tongwei.liu@baishancloud.com>
19 changes: 19 additions & 0 deletions lib/acid/apiutil.lua
Original file line number Diff line number Diff line change
@@ -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
64 changes: 64 additions & 0 deletions t/apiutil.t
Original file line number Diff line number Diff line change
@@ -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']