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

URL rewriting policy: Allow to modify query parameters #724

Merged
merged 14 commits into from
May 29, 2018

Conversation

davidor
Copy link
Contributor

@davidor davidor commented May 25, 2018

This PR implements part of #711
It adds the ability to modify query parameters in the URL rewriting policy.

"oneOf": [
{
"enum": ["add"],
"title": "Set an argument if it does not exist"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is different from what we have in the headers policy. There it is "append".

Query string arguments are quite similar and we have to have some "append" operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. I forgot about values with several arguments.

I've adapted the code to take this case into account. The big changes are in query_params.lua. The other changes that I needed to make were basically adapting the rest of modules to use the correct names of the operations.

assert.stub(ngx.req.set_uri_args).was_called_with({ an_arg = 'a_val' })
end)

it('can replace arguments from the query', function()
Copy link
Contributor

Choose a reason for hiding this comment

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

These tests duplicate tests of QueryParams.

I think we are over testing this a bit. We have 3 tests for the same code.
Not sure what is the best path forward though.

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 see your point. In particular, some of these tests check that ngx.req.set_uri_args was called with the proper arguments, but that shouldn't be a concern of url_rewriting. It should be of query_params, and it's being already tested on its specs.

However, it's a bit tricky to test this properly as QueryParams.new() is called from rewrite() and it does not make sense to inject an instance of QueryParams there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check the latest commit. It mocks QueryParams to avoid testing its internals.

@davidor davidor force-pushed the querystring-ops-url-rewriting-policy branch 2 times, most recently from 3bee4a7 to d25202e Compare May 28, 2018 16:05
}
}
local url_rewriting = URLRewriting.new(config_to_add_args)

url_rewriting:rewrite()

assert.stub(ngx.req.set_uri_args).was_called_with({ an_arg = { '1', '2' } })
assert.spy(spy_mocked_query_params_push).was_called_with(
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like all these tests are sharing one spy. Is that safe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. They need to be instantiated in the before_each to reset their calls.

@davidor davidor force-pushed the querystring-ops-url-rewriting-policy branch from d25202e to f83af88 Compare May 28, 2018 16:11
@davidor davidor force-pushed the querystring-ops-url-rewriting-policy branch from f83af88 to 73745e9 Compare May 28, 2018 16:16
-- 2) When the arg is set, replaces its value with the given one.
-- 3) Deletes an arg when the value is "".
function _M:set(arg, value)
if value == '' then
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmm. This might not be right. HTTP headers are not sent at all by nginx when the they are empty, but Query string args can be binary like ?foo&bar&search. We might need to have "delete" operation after all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. I forgot about this corner 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.

Fixed. Check the latest commit.

@codecov
Copy link

codecov bot commented May 28, 2018

Codecov Report

Merging #724 into master will increase coverage by 0.07%.
The diff coverage is 95.56%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #724      +/-   ##
==========================================
+ Coverage   88.06%   88.13%   +0.07%     
==========================================
  Files         177      179       +2     
  Lines        8460     8662     +202     
==========================================
+ Hits         7450     7634     +184     
- Misses       1010     1028      +18
Impacted Files Coverage Δ
spec/policy/url_rewriting/query_params_spec.lua 100% <100%> (ø)
.../src/apicast/policy/url_rewriting/query_params.lua 88.57% <88.57%> (ø)
...src/apicast/policy/url_rewriting/url_rewriting.lua 91.83% <90.47%> (-1.27%) ⬇️
spec/policy/url_rewriting/url_rewriting_spec.lua 97.67% <96.2%> (-2.33%) ⬇️
...way/src/apicast/configuration_loader/remote_v1.lua 57.74% <0%> (-12.68%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update af40ef6...bf094e5. Read the comment docs.

@davidor davidor force-pushed the querystring-ops-url-rewriting-policy branch from 73745e9 to bf094e5 Compare May 29, 2018 09:47
@davidor davidor requested a review from mikz May 29, 2018 09:52
Copy link
Contributor

@mikz mikz left a comment

Choose a reason for hiding this comment

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

👍

@davidor davidor merged commit 12bc55e into master May 29, 2018
@davidor davidor deleted the querystring-ops-url-rewriting-policy branch May 29, 2018 11:57
@davidor davidor added this to the 3.3 milestone Jun 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants