You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/03/04 02:48:32 UTC
[apisix] branch master updated: test(http-logger): fetch request and response body test case (#6487)
This is an automated email from the ASF dual-hosted git repository.
spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 7fc2d8b test(http-logger): fetch request and response body test case (#6487)
7fc2d8b is described below
commit 7fc2d8b5cd64876da45d7f4be64c5722d10299dd
Author: 帅进超 <sh...@gmail.com>
AuthorDate: Fri Mar 4 10:48:26 2022 +0800
test(http-logger): fetch request and response body test case (#6487)
---
t/plugin/http-logger2.t | 112 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 112 insertions(+)
diff --git a/t/plugin/http-logger2.t b/t/plugin/http-logger2.t
index ae16f6b..688bc8b 100644
--- a/t/plugin/http-logger2.t
+++ b/t/plugin/http-logger2.t
@@ -32,6 +32,64 @@ add_block_preprocessor(sub {
$block->set_value("no_error_log", "[error]");
}
+ my $http_config = $block->http_config // <<_EOC_;
+ server {
+ listen 12001;
+
+ location /http-logger/test {
+ content_by_lua_block {
+ ngx.say("test-http-logger-response")
+ }
+ }
+
+ location /http-logger/center {
+ content_by_lua_block {
+ local function str_split(str, reps)
+ local str_list = {}
+ string.gsub(str, '[^' .. reps .. ']+', function(w)
+ table.insert(str_list, w)
+ end)
+ return str_list
+ end
+
+ local args = ngx.req.get_uri_args()
+ local query = args.query or nil
+ ngx.req.read_body()
+ local body = ngx.req.get_body_data()
+
+ if query then
+ if type(query) == "string" then
+ query = {query}
+ end
+
+ local data, err = require("cjson").decode(body)
+ if err then
+ ngx.log(ngx.WARN, "logs:", body)
+ end
+
+ for i = 1, #query do
+ local fields = str_split(query[i], ".")
+ local val
+ for j = 1, #fields do
+ local key = fields[j]
+ if j == 1 then
+ val = data[key]
+ else
+ val = val[key]
+ end
+ end
+ ngx.log(ngx.WARN ,query[i], ":", val)
+ end
+ else
+ ngx.log(ngx.WARN, "logs:", body)
+ end
+ }
+ }
+ }
+_EOC_
+
+ $block->set_value("http_config", $http_config);
+
my $extra_init_by_lua = <<_EOC_;
local bpm = require("apisix.utils.batch-processor-manager")
bpm.set_check_stale_interval(1)
@@ -141,3 +199,57 @@ GET /opentracing
--- no_error_log
removing batch processor stale object
--- wait: 1.5
+
+
+
+=== TEST 6: set fetch request body and response body route
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "methods": ["POST"],
+ "plugins": {
+ "http-logger": {
+ "uri": "http://127.0.0.1:12001/http-logger/center?query[]=request.body&query[]=response.body",
+ "batch_max_size": 1,
+ "max_retry_count": 1,
+ "retry_delay": 2,
+ "buffer_duration": 2,
+ "inactive_timeout": 2,
+ "include_req_body": true,
+ "include_resp_body": true
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:12001": 1
+ },
+ "type": "roundrobin"
+ },
+ "uri": "/http-logger/test"
+ }]])
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- response_body
+passed
+
+
+
+=== TEST 7: test fetch request body and response body route
+--- request
+POST /http-logger/test
+test-http-logger-request
+--- response_body
+test-http-logger-response
+--- error_log
+request.body:test-http-logger-request
+response.body:test-http-logger-response
+--- wait: 1.5