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
Prev Previous commit
Next Next commit
refactor(ngMocks): ignore query/hash when extracting path params for …
…`MockHttpExpectation`
  • Loading branch information
gkalpak committed Aug 25, 2018
commit db8394bc9bc933cb7c34bdc11e89659374b8d2dd
11 changes: 8 additions & 3 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,9 @@ function assertArgDefined(args, index, name) {
}
}

function stripQueryAndHash(url) {
return url.replace(/[?#].*$/, '');
}

function MockHttpExpectation(method, url, data, headers, keys) {

Expand Down Expand Up @@ -2146,15 +2149,17 @@ 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.

var queryStr = u.indexOf('?') === -1 ? '' : u.substring(u.indexOf('?') + 1);
var strippedUrl = stripQueryAndHash(u);

return angular.extend(parseQuery(queryStr), pathParams());
return angular.extend(parseQuery(queryStr), pathParams(strippedUrl));

function pathParams() {
function pathParams(strippedUrl) {
var keyObj = {};
if (!url || !angular.isFunction(url.test) || !keys || keys.length === 0) return keyObj;

var m = url.exec(u);
var m = url.exec(strippedUrl);
if (!m) return keyObj;

for (var i = 1, len = m.length; i < len; ++i) {
var key = keys[i - 1];
var val = m[i];
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy