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

wenming 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 1be5d2cc8 feat: mock plugin support adding headers (#9720)
1be5d2cc8 is described below

commit 1be5d2cc8fd8482c7dfc90ae02e2e0799f699f03
Author: Abhishek Choudhary <[email protected]>
AuthorDate: Thu Jul 13 09:57:15 2023 +0545

    feat: mock plugin support adding headers (#9720)
---
 apisix/plugins/mocking.lua        | 20 ++++++++++++++++++-
 docs/en/latest/plugins/mocking.md |  1 +
 docs/zh/latest/plugins/mocking.md |  1 +
 t/plugin/mocking.t                | 41 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/apisix/plugins/mocking.lua b/apisix/plugins/mocking.lua
index 134647f71..af5bc75ed 100644
--- a/apisix/plugins/mocking.lua
+++ b/apisix/plugins/mocking.lua
@@ -49,7 +49,19 @@ local schema = {
         -- specify response json schema, if response_example is not nil, this 
conf will be ignore.
         -- generate random response by json schema.
         response_schema = { type = "object" },
-        with_mock_header = { type = "boolean", default = true }
+        with_mock_header = { type = "boolean", default = true },
+        response_headers = {
+            type = "object",
+            minProperties = 1,
+            patternProperties = {
+                ["^[^:]+$"] = {
+                    oneOf = {
+                        { type = "string" },
+                        { type = "number" }
+                    }
+                }
+            },
+        }
     },
     anyOf = {
         { required = { "response_example" } },
@@ -215,6 +227,12 @@ function _M.access(conf, ctx)
         ngx.header["x-mock-by"] = "APISIX/" .. core.version.VERSION
     end
 
+    if conf.response_headers then
+        for key, value in pairs(conf.response_headers) do
+            core.response.add_header(key, value)
+        end
+    end
+
     if conf.delay > 0 then
         ngx.sleep(conf.delay)
     end
diff --git a/docs/en/latest/plugins/mocking.md 
b/docs/en/latest/plugins/mocking.md
index de9b20ad2..a46456bb1 100644
--- a/docs/en/latest/plugins/mocking.md
+++ b/docs/en/latest/plugins/mocking.md
@@ -41,6 +41,7 @@ The `mocking` Plugin is used for mocking an API. When 
executed, it returns rando
 | response_example | string  | False    |                  | Body of the 
response, support use variables, like `$remote_addr $consumer_name`.       |
 | response_schema  | object  | False    |                  | The JSON schema 
object for the response. Works when `response_example` is unspecified. |
 | with_mock_header | boolean | False    | true             | When set to 
`true`, adds a response header `x-mock-by: APISIX/{version}`.              |
+| response_headers | object  | false    |                  | Headers to be 
added in the mocked response. Example: `{"X-Foo": "bar", "X-Few": "baz"}`|
 
 The JSON schema supports the following types in their fields:
 
diff --git a/docs/zh/latest/plugins/mocking.md 
b/docs/zh/latest/plugins/mocking.md
index c6c944f31..842c4e757 100644
--- a/docs/zh/latest/plugins/mocking.md
+++ b/docs/zh/latest/plugins/mocking.md
@@ -41,6 +41,7 @@ description: 本文介绍了关于 Apache APISIX `mocking` 插件的基本信息
 | response_example| string | 否    |                  | 返回响应的 Body,支持使用变量,例如 
`$remote_addr $consumer_name`,与 `response_schema` 字段二选一。 |
 | response_schema | object | 否    |                  | 指定响应的 `jsonschema` 
对象,未指定 `response_example` 字段时生效。                        |
 | with_mock_header| boolean| 否    | true             | 当设置为 `true` 时,将添加响应头 
`x-mock-by: APISIX/{version}`。设置为 `false` 时则不添加该响应头。   |
+| response_headers| object | 否    |                  | 
要在模拟响应中添加的标头。示例:`{"X-Foo": "bar", "X-Few": "baz"}`                              
 |
 
 JSON Schema 在其字段中支持以下类型:
 
diff --git a/t/plugin/mocking.t b/t/plugin/mocking.t
index 644ee2cf3..46d82ef80 100644
--- a/t/plugin/mocking.t
+++ b/t/plugin/mocking.t
@@ -424,3 +424,44 @@ passed
 GET /hello
 --- response_body chomp
 empty_var:
+
+
+
+=== TEST 19: set route (return headers)
+--- 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,
+                    [[{
+                           "plugins": {
+                               "mocking": {
+                                   "response_example": "hello world",
+                                   "response_headers": {
+                                        "X-Apisix": "is, cool",
+                                        "X-Really": "yes"
+                                    }
+                               }
+                           },
+                           "uri": "/hello"
+                   }]]
+                   )
+
+               if code >= 300 then
+                   ngx.status = code
+               end
+               ngx.say(body)
+           }
+       }
+--- response_body
+passed
+
+
+
+=== TEST 20: hit route
+--- request
+GET /hello
+--- response_headers
+X-Apisix: is, cool
+X-Really: yes

Reply via email to