Skip to content

Commit 4455d21

Browse files
authored
fix(ajax): properly encode body with form data that includes URLs (#3502)
closes #2399
1 parent 5be4a37 commit 4455d21

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

spec/observables/dom/ajax-spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,39 @@ describe('Observable.ajax', () => {
652652
expect(complete).to.be.true;
653653
});
654654

655+
it('should properly encode full URLs passed', () => {
656+
const expected = { test: 'https://google.com/search?q=encodeURI+vs+encodeURIComponent' };
657+
let result: Rx.AjaxResponse;
658+
let complete = false;
659+
660+
Rx.Observable
661+
.ajax.post('/flibbertyJibbet', expected)
662+
.subscribe(x => {
663+
result = x;
664+
}, null, () => {
665+
complete = true;
666+
});
667+
668+
const request = MockXMLHttpRequest.mostRecent;
669+
670+
expect(request.method).to.equal('POST');
671+
expect(request.url).to.equal('/flibbertyJibbet');
672+
expect(request.requestHeaders).to.deep.equal({
673+
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
674+
});
675+
676+
request.respondWith({
677+
'status': 200,
678+
'contentType': 'application/json',
679+
'responseText': JSON.stringify(expected)
680+
});
681+
682+
expect(request.data)
683+
.to.equal('test=https%3A%2F%2Fgoogle.com%2Fsearch%3Fq%3DencodeURI%2Bvs%2BencodeURIComponent');
684+
expect(result.response).to.deep.equal(expected);
685+
expect(complete).to.be.true;
686+
});
687+
655688
it('should succeed on 204 No Content', () => {
656689
const expected = null;
657690
let result: Rx.AjaxResponse;

src/internal/observable/dom/AjaxObservable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
292292

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

0 commit comments

Comments
 (0)