-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: mock plugin support adding headers #9720
Changes from 3 commits
13d1f99
bf261bb
f6d82af
9436a2f
fc50ac1
5477ef2
565c649
d9dfdf7
98ac8ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,11 @@ 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need a blank |
||
ngx.sleep(conf.delay) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if you set 2 identical response headers? Add a test case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by "identical response headers"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could test, I don't think you are right. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Well, you can give it a try on your local machine 😀 P.S.: I pushed a new commit that would demonstrate the addition of multiple values for a header. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I said this because you use the function
we will get
So it's ok for this. |
||
} | ||
} | ||
}, | ||
"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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ngx.header[key] = value
instead? keep the same as line 227There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
core.response
is more efficient.