diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 5c875a97ce55..f9f33670bd04 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -430,7 +430,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; @@ -541,7 +541,7 @@ angular.module('ngResource', ['ng']). url = actionUrl || self.template, val, encodedVal, - protocolAndDomain = ''; + protocolAndIpv6 = ''; var urlParams = self.urlParams = Object.create(null); forEach(url.split(/\W/), function(param) { @@ -556,8 +556,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 ''; }); @@ -594,7 +594,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 diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index 41dc071b37a7..3e59ccb11fea 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -321,11 +321,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() {