Skip to content

Commit

Permalink
fix(AjaxObservable): switch to encodeURIComponent to properly encode …
Browse files Browse the repository at this point in the history
…body form data (#3832)

Switch to encodeURIComponent so characters like & and + are properly encoded. This fix is in RxJS 6
but is not in RxJS 5.

Fixes #3824
  • Loading branch information
NickGeek authored and benlesh committed Jun 18, 2018
1 parent 1ed8563 commit 09e4359
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,40 @@ describe('Observable.ajax', () => {
expect(complete).to.be.true;
});

it('properly encode full URLs passed', () => {
const expected = { test: 'https://google.com/search?q=encodeURI+vs+encodeURIComponent' };
let result: Rx.AjaxResponse;
let complete = false;

Rx.Observable
.ajax.post('/flibbertyJibbet', expected)
.subscribe(x => {
result = x;
}, null, () => {
complete = true;
});

const request = MockXMLHttpRequest.mostRecent;

expect(request.method).to.equal('POST');
expect(request.url).to.equal('/flibbertyJibbet');
expect(request.requestHeaders).to.deep.equal({
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
});

request.respondWith({
'status': 200,
'contentType': 'application/json',
'responseText': JSON.stringify(expected)
});

expect(request.data)
.to.equal('test=https%3A%2F%2Fgoogle.com%2Fsearch%3Fq%3DencodeURI%2Bvs%2BencodeURIComponent');
expect(result.response).to.deep.equal(expected);
expect(complete).to.be.true;
});

it('should succeed on 204 No Content', () => {
const expected = null;
let result: Rx.AjaxResponse;
Expand Down
2 changes: 1 addition & 1 deletion src/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {

switch (contentType) {
case 'application/x-www-form-urlencoded':
return Object.keys(body).map(key => `${encodeURI(key)}=${encodeURI(body[key])}`).join('&');
return Object.keys(body).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(body[key])}`).join('&');
case 'application/json':
return JSON.stringify(body);
default:
Expand Down

0 comments on commit 09e4359

Please sign in to comment.