From 13d1f99e8d3dbc61f1e8c9d7db1ff08b0bf01b5e Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Sun, 25 Jun 2023 15:54:16 +0545 Subject: [PATCH 1/9] feat: support adding headers --- apisix/plugins/mocking.lua | 20 ++++++++++++++++++- t/plugin/mocking.t | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/apisix/plugins/mocking.lua b/apisix/plugins/mocking.lua index 134647f71d9f..b703302b31f7 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 }, + 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.headers then + for key, value in pairs(conf.headers) do + core.log.warn(key, value, "dibag") + core.response.add_header(key, value) + end + end if conf.delay > 0 then ngx.sleep(conf.delay) end diff --git a/t/plugin/mocking.t b/t/plugin/mocking.t index 644ee2cf38df..3058c58f970f 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", + "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(return response example:"hello world") +--- request +GET /hello +--- response_headers +X-Apisix: is-cool +X-Really: yes From bf261bb5853c5ccc120584444f4593ca733bced9 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Tue, 27 Jun 2023 08:08:45 +0545 Subject: [PATCH 2/9] rename attribute --- apisix/plugins/mocking.lua | 6 +++--- t/plugin/mocking.t | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apisix/plugins/mocking.lua b/apisix/plugins/mocking.lua index b703302b31f7..e96048afda33 100644 --- a/apisix/plugins/mocking.lua +++ b/apisix/plugins/mocking.lua @@ -50,7 +50,7 @@ local schema = { -- generate random response by json schema. response_schema = { type = "object" }, with_mock_header = { type = "boolean", default = true }, - headers = { + response_headers = { type = "object", minProperties = 1, patternProperties = { @@ -227,8 +227,8 @@ function _M.access(conf, ctx) ngx.header["x-mock-by"] = "APISIX/" .. core.version.VERSION end - if conf.headers then - for key, value in pairs(conf.headers) do + if conf.response_headers then + for key, value in pairs(conf.response_headers) do core.log.warn(key, value, "dibag") core.response.add_header(key, value) end diff --git a/t/plugin/mocking.t b/t/plugin/mocking.t index 3058c58f970f..e1319693d5ef 100644 --- a/t/plugin/mocking.t +++ b/t/plugin/mocking.t @@ -438,7 +438,7 @@ empty_var: "plugins": { "mocking": { "response_example": "hello world", - "headers": { + "response_headers": { "X-Apisix": "is-cool", "X-Really":"yes" } From f6d82af9cebee7673b9a678bb4e8da514b0eafab Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Tue, 27 Jun 2023 08:09:13 +0545 Subject: [PATCH 3/9] clean --- apisix/plugins/mocking.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/apisix/plugins/mocking.lua b/apisix/plugins/mocking.lua index e96048afda33..0aa01d562665 100644 --- a/apisix/plugins/mocking.lua +++ b/apisix/plugins/mocking.lua @@ -229,7 +229,6 @@ function _M.access(conf, ctx) if conf.response_headers then for key, value in pairs(conf.response_headers) do - core.log.warn(key, value, "dibag") core.response.add_header(key, value) end end From 9436a2f8abae8d1d45287a96c671dadf2466e7af Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Fri, 7 Jul 2023 09:22:39 +0545 Subject: [PATCH 4/9] add blank line --- apisix/plugins/mocking.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/apisix/plugins/mocking.lua b/apisix/plugins/mocking.lua index 0aa01d562665..af5bc75edb2a 100644 --- a/apisix/plugins/mocking.lua +++ b/apisix/plugins/mocking.lua @@ -232,6 +232,7 @@ function _M.access(conf, ctx) core.response.add_header(key, value) end end + if conf.delay > 0 then ngx.sleep(conf.delay) end From fc50ac124d0d1c28c6d5ec6efac10905f1de3800 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Fri, 7 Jul 2023 09:22:47 +0545 Subject: [PATCH 5/9] add space --- t/plugin/mocking.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/plugin/mocking.t b/t/plugin/mocking.t index e1319693d5ef..8f25fb358e8c 100644 --- a/t/plugin/mocking.t +++ b/t/plugin/mocking.t @@ -440,7 +440,7 @@ empty_var: "response_example": "hello world", "response_headers": { "X-Apisix": "is-cool", - "X-Really":"yes" + "X-Really": "yes" } } }, From 5477ef214071b54c1ea6b2e87f576daf6216a462 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Fri, 7 Jul 2023 09:28:00 +0545 Subject: [PATCH 6/9] add space --- t/plugin/mocking.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/plugin/mocking.t b/t/plugin/mocking.t index 8f25fb358e8c..36a1f034719e 100644 --- a/t/plugin/mocking.t +++ b/t/plugin/mocking.t @@ -427,7 +427,7 @@ empty_var: -=== TEST 19: set route(return headers) +=== TEST 19: set route (return headers) --- config location /t { content_by_lua_block { @@ -459,7 +459,7 @@ passed -=== TEST 20: hit route(return response example:"hello world") +=== TEST 20: hit route (return response example:"hello world") --- request GET /hello --- response_headers From 565c6496b6956644b2237ee65c2b0a6e0c76540f Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Fri, 7 Jul 2023 13:10:49 +0545 Subject: [PATCH 7/9] add docs --- docs/en/latest/plugins/mocking.md | 1 + docs/zh/latest/plugins/mocking.md | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/en/latest/plugins/mocking.md b/docs/en/latest/plugins/mocking.md index de9b20ad22a9..a46456bb1b14 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 c6c944f31927..6281e7d94330 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 在其字段中支持以下类型: From d9dfdf74c0330b28a9865c2715efd3e5dc108e8b Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Mon, 10 Jul 2023 18:08:23 +0545 Subject: [PATCH 8/9] fix chinese copywriting lint error --- docs/zh/latest/plugins/mocking.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zh/latest/plugins/mocking.md b/docs/zh/latest/plugins/mocking.md index 6281e7d94330..842c4e757208 100644 --- a/docs/zh/latest/plugins/mocking.md +++ b/docs/zh/latest/plugins/mocking.md @@ -41,7 +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"}` | +| response_headers| object | 否 | | 要在模拟响应中添加的标头。示例:`{"X-Foo": "bar", "X-Few": "baz"}` | JSON Schema 在其字段中支持以下类型: From 98ac8ed0afcb3752068bd75ae03de4de15407c62 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Mon, 10 Jul 2023 20:25:35 +0545 Subject: [PATCH 9/9] improve tests --- t/plugin/mocking.t | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/t/plugin/mocking.t b/t/plugin/mocking.t index 36a1f034719e..46d82ef80a8f 100644 --- a/t/plugin/mocking.t +++ b/t/plugin/mocking.t @@ -439,7 +439,7 @@ empty_var: "mocking": { "response_example": "hello world", "response_headers": { - "X-Apisix": "is-cool", + "X-Apisix": "is, cool", "X-Really": "yes" } } @@ -459,9 +459,9 @@ passed -=== TEST 20: hit route (return response example:"hello world") +=== TEST 20: hit route --- request GET /hello --- response_headers -X-Apisix: is-cool +X-Apisix: is, cool X-Really: yes