Skip to content

Commit 95fb275

Browse files
committed
option to disable encoding slashes for url params
fixes angular#1388
1 parent 307e72e commit 95fb275

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/ngResource/resource.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,14 @@ function shallowClearAndCopy(src, dst) {
155155
* with `http response` object. See {@link ng.$http $http interceptors}.
156156
*
157157
* @param {Object} options Hash with custom settings that should extend the
158-
* default `$resourceProvider` behavior. The only supported option is
158+
* default `$resourceProvider` behavior.
159159
*
160160
* Where:
161161
*
162162
* - **`stripTrailingSlashes`** – {boolean} – If true then the trailing
163163
* slashes from any calculated URL will be stripped. (Defaults to true.)
164+
* - **`encodeSlashes`** - {boolean} - If true then slashes in URL parameters
165+
* will be encoded. (Defaults to true.)
164166
*
165167
* @returns {Object} A resource "class" object with methods for the default set of resource actions
166168
* optionally extended with custom `actions`. The default set contains these actions:
@@ -344,6 +346,9 @@ angular.module('ngResource', ['ng']).
344346
// Strip slashes by default
345347
stripTrailingSlashes: true,
346348

349+
// Encode slashes by default
350+
encodeSlashes: true,
351+
347352
// Default actions configuration
348353
actions: {
349354
'get': {method: 'GET'},
@@ -373,8 +378,8 @@ angular.module('ngResource', ['ng']).
373378
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
374379
* / "*" / "+" / "," / ";" / "="
375380
*/
376-
function encodeUriSegment(val) {
377-
return encodeUriQuery(val, true).
381+
function encodeUriSegment(val, encodeSlashes) {
382+
return encodeUriQuery(val, true, encodeSlashes).
378383
replace(/%26/gi, '&').
379384
replace(/%3D/gi, '=').
380385
replace(/%2B/gi, '+');
@@ -392,12 +397,13 @@ angular.module('ngResource', ['ng']).
392397
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
393398
* / "*" / "+" / "," / ";" / "="
394399
*/
395-
function encodeUriQuery(val, pctEncodeSpaces) {
400+
function encodeUriQuery(val, pctEncodeSpaces, encodeSlashes) {
396401
return encodeURIComponent(val).
397402
replace(/%40/gi, '@').
398403
replace(/%3A/gi, ':').
399404
replace(/%24/g, '$').
400405
replace(/%2C/gi, ',').
406+
replace(/%2F/g, (encodeSlashes ? '%2F' : '/')).
401407
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
402408
}
403409

@@ -430,7 +436,7 @@ angular.module('ngResource', ['ng']).
430436
forEach(self.urlParams, function (_, urlParam) {
431437
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam];
432438
if (angular.isDefined(val) && val !== null) {
433-
encodedVal = encodeUriSegment(val);
439+
encodedVal = encodeUriSegment(val, self.defaults.encodeSlashes);
434440
url = url.replace(new RegExp(":" + urlParam + "(\\W|$)", "g"), function (match, p1) {
435441
return encodedVal + p1;
436442
});

test/ngResource/resourceSpec.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ describe("resource", function() {
293293
});
294294

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

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

307+
it('should support implicitly encode slashes in urls', function () {
308+
var R = $resource('http://localhost:8080/:path');
309+
310+
$httpBackend.expect('GET', 'http://localhost:8080/Foo%2Fbar').respond();
311+
R.get({path: 'Foo/bar'});
312+
});
313+
314+
it('should support explicitly encoding slashes in urls', function () {
315+
var R = $resource('http://localhost:8080/:path', {}, {}, {encodeSlashes: true});
316+
317+
$httpBackend.expect('GET', 'http://localhost:8080/Foo%2Fbar').respond();
318+
R.get({path: 'Foo/bar'});
319+
});
320+
321+
it('should support explicitly preventing encoding slashes in urls', function () {
322+
var R = $resource('http://localhost:8080/:path', {}, {}, {encodeSlashes: false});
323+
324+
$httpBackend.expect('GET', 'http://localhost:8080/Foo/bar').respond();
325+
R.get({path: 'Foo/bar'});
326+
});
327+
328+
it('should support provider-level configuration to prevent encoding slashes in urls', function () {
329+
resourceProvider.defaults.encodeSlashes = false;
330+
331+
var R = $resource('http://localhost:8080/:path');
332+
333+
$httpBackend.expect('GET', 'http://localhost:8080/Foo/bar').respond();
334+
R.get({path: 'Foo/bar'});
335+
});
336+
337+
it('should support overriding provider default prevent encoding slashes configuration', function () {
338+
resourceProvider.defaults.encodeSlashes = false;
339+
340+
var R = $resource('http://localhost:8080/:path', {}, {}, {encodeSlashes: true});
341+
342+
$httpBackend.expect('GET', 'http://localhost:8080/Foo%2Fbar').respond();
343+
R.get({path: 'Foo/bar'});
344+
});
307345

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

0 commit comments

Comments
 (0)