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

option to disable encoding slashes for url params #7940

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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.
Copy link
Contributor

Choose a reason for hiding this comment

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

this documentation section is weird. It was weird before, but it's still kinda awkward. I would replace the "where: " with something like "The following properties are considered:" or something.

*
* 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
Copy link
Contributor

Choose a reason for hiding this comment

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

to be honest this seems like a kind of weird way to implement this feature, but if it's what people want, then it's what people want.

* 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
Copy link
Contributor

Choose a reason for hiding this comment

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

no

// 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 () {
Copy link
Contributor

Choose a reason for hiding this comment

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

The tests look good to me, good work here

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