Skip to content

Commit

Permalink
option to disable encoding slashes for url params
Browse files Browse the repository at this point in the history
  • Loading branch information
connorbode committed Jun 23, 2014
1 parent 307e72e commit 95fb275
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ function shallowClearAndCopy(src, dst) {
* with `http response` object. See {@link ng.$http $http interceptors}.
*
* @param {Object} options Hash with custom settings that should extend the
* default `$resourceProvider` behavior. The only supported option is
* default `$resourceProvider` behavior.
*
* Where:
*
* - **`stripTrailingSlashes`** – {boolean} – If true then the trailing
* slashes from any calculated URL will be stripped. (Defaults to true.)
* - **`encodeSlashes`** - {boolean} - If true then slashes in URL parameters
* will be encoded. (Defaults to true.)
*
* @returns {Object} A resource "class" object with methods for the default set of resource actions
* optionally extended with custom `actions`. The default set contains these actions:
Expand Down Expand Up @@ -344,6 +346,9 @@ angular.module('ngResource', ['ng']).
// Strip slashes by default
stripTrailingSlashes: true,

// Encode slashes by default
encodeSlashes: true,

// Default actions configuration
actions: {
'get': {method: 'GET'},
Expand Down Expand Up @@ -373,8 +378,8 @@ angular.module('ngResource', ['ng']).
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
* / "*" / "+" / "," / ";" / "="
*/
function encodeUriSegment(val) {
return encodeUriQuery(val, true).
function encodeUriSegment(val, encodeSlashes) {
return encodeUriQuery(val, true, encodeSlashes).
replace(/%26/gi, '&').
replace(/%3D/gi, '=').
replace(/%2B/gi, '+');
Expand All @@ -392,12 +397,13 @@ angular.module('ngResource', ['ng']).
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
* / "*" / "+" / "," / ";" / "="
*/
function encodeUriQuery(val, pctEncodeSpaces) {
function encodeUriQuery(val, pctEncodeSpaces, encodeSlashes) {
return encodeURIComponent(val).
replace(/%40/gi, '@').
replace(/%3A/gi, ':').
replace(/%24/g, '$').
replace(/%2C/gi, ',').
replace(/%2F/g, (encodeSlashes ? '%2F' : '/')).
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
}

Expand Down Expand Up @@ -430,7 +436,7 @@ angular.module('ngResource', ['ng']).
forEach(self.urlParams, function (_, urlParam) {
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam];
if (angular.isDefined(val) && val !== null) {
encodedVal = encodeUriSegment(val);
encodedVal = encodeUriSegment(val, self.defaults.encodeSlashes);
url = url.replace(new RegExp(":" + urlParam + "(\\W|$)", "g"), function (match, p1) {
return encodedVal + p1;
});
Expand Down
40 changes: 39 additions & 1 deletion test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ describe("resource", function() {
});

it('should support overriding provider default trailing-slash stripping configuration', function() {
// Set the new behavior for all new resources created by overriding the
// Set the new behav ior for all new resources created by overriding the
// provider configuration
resourceProvider.defaults.stripTrailingSlashes = false;

Expand All @@ -304,6 +304,44 @@ describe("resource", function() {
R.get({a: 'foo'});
});

it('should support implicitly encode slashes in urls', function () {
var R = $resource('http://localhost:8080/:path');

$httpBackend.expect('GET', 'http://localhost:8080/Foo%2Fbar').respond();
R.get({path: 'Foo/bar'});
});

it('should support explicitly encoding slashes in urls', function () {
var R = $resource('http://localhost:8080/:path', {}, {}, {encodeSlashes: true});

$httpBackend.expect('GET', 'http://localhost:8080/Foo%2Fbar').respond();
R.get({path: 'Foo/bar'});
});

it('should support explicitly preventing encoding slashes in urls', function () {
var R = $resource('http://localhost:8080/:path', {}, {}, {encodeSlashes: false});

$httpBackend.expect('GET', 'http://localhost:8080/Foo/bar').respond();
R.get({path: 'Foo/bar'});
});

it('should support provider-level configuration to prevent encoding slashes in urls', function () {
resourceProvider.defaults.encodeSlashes = false;

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

$httpBackend.expect('GET', 'http://localhost:8080/Foo/bar').respond();
R.get({path: 'Foo/bar'});
});

it('should support overriding provider default prevent encoding slashes configuration', function () {
resourceProvider.defaults.encodeSlashes = false;

var R = $resource('http://localhost:8080/:path', {}, {}, {encodeSlashes: true});

$httpBackend.expect('GET', 'http://localhost:8080/Foo%2Fbar').respond();
R.get({path: 'Foo/bar'});
});

it('should allow relative paths in resource url', function () {
var R = $resource(':relativePath');
Expand Down

0 comments on commit 95fb275

Please sign in to comment.