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

Respect PORT from URL without escaping #2652

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
*
* @param {string} url A parametrized URL template with parameters prefixed by `:` as in
* `/user/:username`. If you are using a URL with a port number (e.g.
* `http://example.com:8080/api`), you'll need to escape the colon character before the port
* number, like this: `$resource('http://example.com\\:8080/api')`.
* `http://example.com:8080/api`), it will be respected.
*
* @param {Object=} paramDefaults Default values for `url` parameters. These can be overridden in
* `actions` methods. If any of the parameter value is a function, it will be executed every time
Expand Down Expand Up @@ -331,7 +330,7 @@ angular.module('ngResource', ['ng']).

var urlParams = self.urlParams = {};
forEach(url.split(/\W/), function(param){
if (param && (new RegExp("(^|[^\\\\]):" + param + "(\\W|$)").test(url))) {
if (!(new RegExp("^\\d+","g").test(param)) && param && (new RegExp("(^|[^\\\\]):" + param + "(\\W|$)").test(url))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should the regex really be ^\d+$? I.E. Only matches if all the characters are digit, in which case you don't need "g" modifier?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

\ had to be escaped. The "g" modifier can be dropped, that's right.

Copy link
Contributor

Choose a reason for hiding this comment

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

The difference is not the \ but the lack of $ at the end. As it stands this regex will match "1abc", which is probably not valid, either way, as a port or a parameter!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed. I'll fix it shortly.

urlParams[param] = true;
}
});
Expand Down
7 changes: 7 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ describe("resource", function() {
R.get({a: 'foo', b: 'bar'});
});

it('should support por unescaped url', function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

typo : "por" -> "an"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad. This is to be fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it('should support unescaped port in url'), function() {

Is this better?

Choose a reason for hiding this comment

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

I think it should be "should support an unescaped port in url"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 @Nami-Doc

var R = $resource('http://localhost:8080/Path/:a');

$httpBackend.expect('GET', 'http://localhost:8080/Path/foo').respond();
R.get({a: 'foo'});
});


it('should correctly encode url params', function() {
var R = $resource('/Path/:a');
Expand Down