Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($route): correctly extract path params if path contains question mark or hash #16672

Closed
wants to merge 8 commits into from

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Aug 21, 2018

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Bug fix (and refactorings).

What is the current behavior? (You can also link to an open issue here)
The routeToRegExp() function, introduced by 840b5f0, cannot extract path params if the path contains question mark or hash. Although these characters would normally be encoded in the path, they are decoded by $location.path(), before being passed to the RegExp returned by routeToRegExp().

routeToRegExp() has to be able to deal with both encoded URL and decoded path, because it is being shared between ngRoute and ngMocks.

What is the new behavior (if this is a feature change)?
The RegExp returned by routeToRegExp() only deals with the path part and the callers are responsible for stripping off the query and hash parts (if any).

(This PR contains the code from #16670, plus some refactoring to simplify the implementation).

Does this PR introduce a breaking change?
No.

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Fix/Feature: Docs have been added/updated
  • Fix/Feature: Tests have been added; existing tests pass

Other information:
Closes #16670.

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

1 similar comment
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

@@ -77,5 +77,24 @@ describe('$routeParams', function() {
});
});

it('should correctly extract path params containing hashes and/or question marks', function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we test for $location.path('/foo/bar?baz#qux');?

And is it intended that the "params" have no values, i.e ?baz=val?

Should we test if multiple params are correctly extracted?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. This is not the primary focus of this test, but I'm never one to shy away from adding more testcases 😛

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated.

@@ -2145,14 +2154,18 @@ function MockHttpExpectation(method, url, data, headers, keys) {
};

this.params = function(u) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we give the params here (u, m) better names since we are already refactoring?

Copy link
Member Author

Choose a reason for hiding this comment

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

Suggestions?

Copy link
Contributor

Choose a reason for hiding this comment

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

u -> url
m -> match

😄

Copy link
Member Author

Choose a reason for hiding this comment

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

url is taken 😁

Copy link
Contributor

Choose a reason for hiding this comment

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

paramsUrl?

Copy link
Contributor

Choose a reason for hiding this comment

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

What's the difference between url and u anyway?

Copy link
Member Author

Choose a reason for hiding this comment

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

url is the URL of the definition (when calling $httpBackend.when(method, url)). u is the actual URL that $http was called with and we want to verify whether it matches the definition.
(Also note, url can be a string or a RegExp or a function.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Pretty big difference then! How about requestUrl instead of u?

Copy link
Member Author

Choose a reason for hiding this comment

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

I took a different approach and renamed the MockHttpExpectation() arguments to expectedXyz (see eaf7a5db1).
Let me know what you think.

susisu and others added 8 commits August 25, 2018 15:25
…mark or hash

The `routeToRegExp()` function, introduced by 840b5f0, could not extract
path params if the path contained question mark or hash. Although these
characters would normally be encoded in the path, they are decoded by
`$location.path()`, before being passed to the RegExp returned by
`routeToRegExp()`.

`routeToRegExp()` has to be able to deal with both encoded URL and
decoded path, because it is being shared between `ngRoute` and
`ngMocks`.

This commit fixes the issue, by introducing an `isUrl` option that
allows creating an appropriate RegExp for each usecase.
@gkalpak gkalpak force-pushed the fix-route-path-params branch from fd6ff1b to 9b9e25f Compare August 25, 2018 12:29
@gkalpak gkalpak closed this in a553735 Aug 25, 2018
@gkalpak gkalpak deleted the fix-route-path-params branch August 25, 2018 20:44
gkalpak added a commit that referenced this pull request Aug 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants