Skip to content

Commit

Permalink
fix(ajax): properly encode body with form data that includes URLs (#3502
Browse files Browse the repository at this point in the history
)

closes #2399
  • Loading branch information
benlesh authored Mar 30, 2018
1 parent 5be4a37 commit 4455d21
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,39 @@ describe('Observable.ajax', () => {
expect(complete).to.be.true;
});

it('should 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({
'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/internal/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 4455d21

Please sign in to comment.