fix($resource): allow params in hostname (except for IPv6 addresses)

This commit reverts the revert 02f045be8d.
This commit is contained in:
Matias Niemelä
2017-02-06 16:25:40 -05:00
parent 424f4b8a13
commit 7f65bbb8de
2 changed files with 34 additions and 10 deletions
+5 -5
View File
@@ -427,7 +427,7 @@ function shallowClearAndCopy(src, dst) {
*/
angular.module('ngResource', ['ng']).
provider('$resource', function ResourceProvider() {
var PROTOCOL_AND_DOMAIN_REGEX = /^https?:\/\/[^/]*/;
var PROTOCOL_AND_IPV6_REGEX = /^https?:\/\/\[[^\]]*][^/]*/;
var provider = this;
@@ -575,7 +575,7 @@ angular.module('ngResource', ['ng']).
url = actionUrl || self.template,
val,
encodedVal,
protocolAndDomain = '';
protocolAndIpv6 = '';
var urlParams = self.urlParams = {};
forEach(url.split(/\W/), function(param) {
@@ -590,8 +590,8 @@ angular.module('ngResource', ['ng']).
}
});
url = url.replace(/\\:/g, ':');
url = url.replace(PROTOCOL_AND_DOMAIN_REGEX, function(match) {
protocolAndDomain = match;
url = url.replace(PROTOCOL_AND_IPV6_REGEX, function(match) {
protocolAndIpv6 = match;
return '';
});
@@ -628,7 +628,7 @@ angular.module('ngResource', ['ng']).
// E.g. `http://url.com/id./format?q=x` becomes `http://url.com/id.format?q=x`
url = url.replace(/\/\.(?=\w+($|\?))/, '.');
// replace escaped `/\.` with `/.`
config.url = protocolAndDomain + url.replace(/\/\\\./, '/.');
config.url = protocolAndIpv6 + url.replace(/\/\\\./, '/.');
// set params - delegate param encoding to $http
+29 -5
View File
@@ -300,11 +300,35 @@ describe('basic usage', function() {
});
it('should support IPv6 URLs', function() {
var R = $resource('http://[2620:0:861:ed1a::1]/:ed1a/', {}, {}, {stripTrailingSlashes: false});
$httpBackend.expect('GET', 'http://[2620:0:861:ed1a::1]/foo/').respond({});
$httpBackend.expect('GET', 'http://[2620:0:861:ed1a::1]/').respond({});
R.get({ed1a: 'foo'});
R.get({});
test('http://[2620:0:861:ed1a::1]', {ed1a: 'foo'}, 'http://[2620:0:861:ed1a::1]');
test('http://[2620:0:861:ed1a::1]/', {ed1a: 'foo'}, 'http://[2620:0:861:ed1a::1]/');
test('http://[2620:0:861:ed1a::1]/:ed1a', {ed1a: 'foo'}, 'http://[2620:0:861:ed1a::1]/foo');
test('http://[2620:0:861:ed1a::1]/:ed1a', {}, 'http://[2620:0:861:ed1a::1]/');
test('http://[2620:0:861:ed1a::1]/:ed1a/', {ed1a: 'foo'}, 'http://[2620:0:861:ed1a::1]/foo/');
test('http://[2620:0:861:ed1a::1]/:ed1a/', {}, 'http://[2620:0:861:ed1a::1]/');
// Helpers
function test(templateUrl, params, actualUrl) {
var R = $resource(templateUrl, null, null, {stripTrailingSlashes: false});
$httpBackend.expect('GET', actualUrl).respond(null);
R.get(params);
}
});
it('should support params in the `hostname` part of the URL', function() {
test('http://:hostname', {hostname: 'foo.com'}, 'http://foo.com');
test('http://:hostname/', {hostname: 'foo.com'}, 'http://foo.com/');
test('http://:l2Domain.:l1Domain', {l1Domain: 'com', l2Domain: 'bar'}, 'http://bar.com');
test('http://:l2Domain.:l1Domain/', {l1Domain: 'com', l2Domain: 'bar'}, 'http://bar.com/');
test('http://127.0.0.:octet', {octet: 42}, 'http://127.0.0.42');
test('http://127.0.0.:octet/', {octet: 42}, 'http://127.0.0.42/');
// Helpers
function test(templateUrl, params, actualUrl) {
var R = $resource(templateUrl, null, null, {stripTrailingSlashes: false});
$httpBackend.expect('GET', actualUrl).respond(null);
R.get(params);
}
});
it('should support overriding provider default trailing-slash stripping configuration', function() {