Skip to content
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

Merged
merged 9 commits into from
Jul 13, 2023

Conversation

shreemaan-abhishek
Copy link
Contributor

Description

Fixes #9717

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@shreemaan-abhishek
Copy link
Contributor Author

Documentation changes are pending.

@lingsamuel lingsamuel changed the title feat: support adding headers feat: mock plugin support adding headers Jun 25, 2023
@@ -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 = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name headers and its placement just below with_mock_header can create confusion as to whether these two keys are related.

  • headers to response_headers?
  • add comment -- specify response headers.

if conf.headers then
for key, value in pairs(conf.headers) do
core.log.warn(key, value, "dibag")
core.response.add_header(key, value)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. remove the core.log.warn?
  2. use ngx.header[key] = value instead? keep the same as line 227

Copy link
Contributor Author

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.

@shreemaan-abhishek shreemaan-abhishek marked this pull request as ready for review June 27, 2023 02:27
for key, value in pairs(conf.response_headers) do
core.response.add_header(key, value)
end
end
if conf.delay > 0 then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a blank

Comment on lines 442 to 443
"X-Apisix": "is-cool",
"X-Really":"yes"
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "identical response headers"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

                                   "response_headers": {
                                        "X-Apisix": "is-cool",
                                        "X-Apisix": "yes"
                                    }
                               }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Only the last one is retained, the first one is lost. However, if someone needs multiple values for a header they can do "header-name": "val1, val2".

  2. Why is this test case needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could test, I don't think you are right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you are right.

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.

Copy link
Contributor

@monkeyDluffy6017 monkeyDluffy6017 Jul 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I said this because you use the function core.response.add_header to add the response header, the function will append the value to the same header name, but i tested locally, and found that the conf.response_headers won't pass the duplicated headers.
If we pass

                                   "response_headers": {
                                        "X-Apisix": "is-cool",
                                        "X-Apisix": "yes"
                                    }

we will get

                                   "response_headers": {
                                        "X-Apisix": "yes"
                                    }

So it's ok for this.
But i don't see you example

@monkeyDluffy6017
Copy link
Contributor

The doc should be updated

@monkeyDluffy6017 monkeyDluffy6017 added the wait for update wait for the author's response in this issue/PR label Jul 6, 2023
@monkeyDluffy6017 monkeyDluffy6017 added approved and removed wait for update wait for the author's response in this issue/PR labels Jul 10, 2023
@moonming moonming merged commit 1be5d2c into apache:master Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: plugin mocking support set_headers
4 participants