This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
option to disable encoding slashes for url params #7940
Open
connorbode
wants to merge
1
commit into
angular:master
Choose a base branch
from
connorbode:ngresource-encode-slashes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
@@ -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'}, | ||
|
@@ -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, '+'); | ||
|
@@ -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' : '+')); | ||
} | ||
|
||
|
@@ -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; | ||
}); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no |
||
// provider configuration | ||
resourceProvider.defaults.stripTrailingSlashes = false; | ||
|
||
|
@@ -304,6 +304,44 @@ describe("resource", function() { | |
R.get({a: 'foo'}); | ||
}); | ||
|
||
it('should support implicitly encode slashes in urls', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.