You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by me...@apache.org on 2020/02/18 07:19:17 UTC

[incubator-apisix] branch master updated: core: support to set response headers by table. (#1129)

This is an automated email from the ASF dual-hosted git repository.

membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 7bcf15d  core: support to set response headers by table. (#1129)
7bcf15d is described below

commit 7bcf15d45bef0051b49716a25e03a5a77c277c34
Author: YuanSheng Wang <me...@gmail.com>
AuthorDate: Tue Feb 18 15:19:12 2020 +0800

    core: support to set response headers by table. (#1129)
---
 lua/apisix/core/response.lua |  23 ++++++++--
 t/admin/plugins-reload.t     |   2 +-
 t/core/response.t            | 102 +++++++++++++++++++++++++++++++++++++++++++
 t/plugin/fault-injection.t   |  18 ++++----
 4 files changed, 131 insertions(+), 14 deletions(-)

diff --git a/lua/apisix/core/response.lua b/lua/apisix/core/response.lua
index ff8b1a9..385f9fe 100644
--- a/lua/apisix/core/response.lua
+++ b/lua/apisix/core/response.lua
@@ -16,7 +16,7 @@
 --
 local encode_json = require("cjson.safe").encode
 local ngx = ngx
-local ngx_say = ngx.say
+local ngx_print = ngx.print
 local ngx_header = ngx.header
 local error = error
 local select = select
@@ -26,6 +26,7 @@ local insert_tab = table.insert
 local concat_tab = table.concat
 local str_sub = string.sub
 local tonumber = tonumber
+local pairs = pairs
 
 local _M = {version = 0.1}
 
@@ -55,7 +56,7 @@ function resp_exit(code, ...)
                 error("failed to encode data: " .. err, -2)
             else
                 idx = idx + 1
-                insert_tab(t, idx, body)
+                insert_tab(t, idx, body .. "\n")
             end
 
         elseif v ~= nil then
@@ -65,7 +66,7 @@ function resp_exit(code, ...)
     end
 
     if idx > 0 then
-        ngx_say(concat_tab(t, "", 1, idx))
+        ngx_print(concat_tab(t, "", 1, idx))
     end
 
     if code then
@@ -87,7 +88,21 @@ function _M.set_header(...)
       error("headers have already been sent", 2)
     end
 
-    for i = 1, select('#', ...), 2 do
+    local count = select('#', ...)
+    if count == 1 then
+        local headers = select(1, ...)
+        if type(headers) ~= "table" then
+            error("should be a table if only one argument", 2)
+        end
+
+        for k, v in pairs(headers) do
+            ngx_header[k] = v
+        end
+
+        return
+    end
+
+    for i = 1, count, 2 do
         ngx_header[select(i, ...)] = select(i + 1, ...)
     end
 end
diff --git a/t/admin/plugins-reload.t b/t/admin/plugins-reload.t
index 1ca5563..bf84172 100644
--- a/t/admin/plugins-reload.t
+++ b/t/admin/plugins-reload.t
@@ -37,7 +37,7 @@ location /t {
                                     ngx.HTTP_PUT)
 
         ngx.status = code
-        ngx.print(org_body)
+        ngx.say(org_body)
         ngx.sleep(0.2)
     }
 }
diff --git a/t/core/response.t b/t/core/response.t
new file mode 100644
index 0000000..b8a3cb0
--- /dev/null
+++ b/t/core/response.t
@@ -0,0 +1,102 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+log_level("info");
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: exit with string
+--- config
+    location = /t {
+        access_by_lua_block {
+            local core = require("apisix.core")
+            core.response.exit(201, "done\n")
+        }
+    }
+--- request
+GET /t
+--- error_code: 201
+--- response_body
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: exit with table
+--- config
+    location = /t {
+        access_by_lua_block {
+            local core = require("apisix.core")
+            core.response.exit(201, {a = "a"})
+        }
+    }
+--- request
+GET /t
+--- error_code: 201
+--- response_body
+{"a":"a"}
+--- no_error_log
+[error]
+
+
+
+=== TEST 3: multiple reponse headers
+--- config
+    location = /t {
+        access_by_lua_block {
+            local core = require("apisix.core")
+            core.response.set_header("aaa", "bbb", "ccc", "ddd")
+            core.response.exit(200, "done\n")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- response_headers
+aaa: bbb
+ccc: ddd
+--- no_error_log
+[error]
+
+
+
+=== TEST 4: multiple reponse headers by table
+--- config
+    location = /t {
+        access_by_lua_block {
+            local core = require("apisix.core")
+            core.response.set_header({aaa = "bbb", ccc = "ddd"})
+            core.response.exit(200, "done\n")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- response_headers
+aaa: bbb
+ccc: ddd
+--- no_error_log
+[error]
diff --git a/t/plugin/fault-injection.t b/t/plugin/fault-injection.t
index 1038d14..9a44745 100644
--- a/t/plugin/fault-injection.t
+++ b/t/plugin/fault-injection.t
@@ -47,7 +47,7 @@ __DATA__
                                "fault-injection": {
                                    "abort": {
                                        "http_status": 100,
-                                       "body": "Fault Injection!"
+                                       "body": "Fault Injection!\n"
                                    }
                                },
                                "proxy-rewrite": {
@@ -357,7 +357,7 @@ hello world
 
 
 
-=== TEST 10: set route(abort with http status 200 and return "Fault Injection!")
+=== TEST 10: set route(abort with http status 200 and return "Fault Injection!\n")
 --- config
        location /t {
            content_by_lua_block {
@@ -369,7 +369,7 @@ hello world
                                "fault-injection": {
                                    "abort": {
                                       "http_status": 200,
-                                      "body": "Fault Injection!"
+                                      "body": "Fault Injection!\n"
                                    }
                                },
                                "proxy-rewrite": {
@@ -402,7 +402,7 @@ passed
 
 
 
-=== TEST 11: hit route(abort with http code 200 and return "Fault Injection!")
+=== TEST 11: hit route(abort with http code 200 and return "Fault Injection!\n")
 --- request
 GET /hello HTTP/1.1
 --- error_code: 200
@@ -413,7 +413,7 @@ Fault Injection!
 
 
 
-=== TEST 12: set route(abort with http status 405 and return "Fault Injection!")
+=== TEST 12: set route(abort with http status 405 and return "Fault Injection!\n")
 --- config
        location /t {
            content_by_lua_block {
@@ -425,7 +425,7 @@ Fault Injection!
                                "fault-injection": {
                                    "abort": {
                                       "http_status": 405,
-                                      "body": "Fault Injection!"
+                                      "body": "Fault Injection!\n"
                                    }
                                },
                                "proxy-rewrite": {
@@ -458,7 +458,7 @@ passed
 
 
 
-=== TEST 13: hit route(abort with http status 405 and return "Fault Injection!")
+=== TEST 13: hit route(abort with http status 405 and return "Fault Injection!\n")
 --- request
 GET /hello HTTP/1.1
 --- error_code: 405
@@ -481,7 +481,7 @@ Fault Injection!
                                "fault-injection": {
                                    "abort": {
                                       "http_status": 200,
-                                      "body": "Fault Injection!"
+                                      "body": "Fault Injection!\n"
                                    }
                                },
                                "redirect": {
@@ -515,7 +515,7 @@ passed
 
 
 
-=== TEST 15: hit route(abort with http status 200 and return "Fault Injection!")
+=== TEST 15: hit route(abort with http status 200 and return "Fault Injection!\n")
 --- request
 GET /hello HTTP/1.1
 --- error_code: 200