Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ajax): default to opting into CORS #3442

Merged
merged 1 commit into from
Mar 16, 2018
Merged
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
23 changes: 17 additions & 6 deletions spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ describe('Observable.ajax', () => {

const obj: Rx.AjaxRequest = {
url: '/',
method: ''
method: '',
crossDomain: false,
};

Rx.Observable.ajax(obj).subscribe();
Expand Down Expand Up @@ -132,7 +133,21 @@ describe('Observable.ajax', () => {
'Content-Type': 'kenny/loggins',
'Fly-Into-The': 'Dangah Zone!',
'Take-A-Ride-Into-The': 'Danger ZoooOoone!',
'X-Requested-With': 'XMLHttpRequest'
});
});

it('should set the X-Requested-With if crossDomain is false', () => {
Rx.Observable.ajax({
url: '/test/monkey',
method: 'GET',
crossDomain: false,
})
.subscribe();

const request = MockXMLHttpRequest.mostRecent;

expect(request.requestHeaders).to.deep.equal({
'X-Requested-With': 'XMLHttpRequest',
});
});

Expand Down Expand Up @@ -433,7 +448,6 @@ describe('Observable.ajax', () => {
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
expect(MockXMLHttpRequest.mostRecent.data).to.deep.equal(body);
expect(MockXMLHttpRequest.mostRecent.requestHeaders).to.deep.equal({
'X-Requested-With': 'XMLHttpRequest',
});
});

Expand Down Expand Up @@ -624,7 +638,6 @@ describe('Observable.ajax', () => {
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'
});

Expand Down Expand Up @@ -657,7 +670,6 @@ describe('Observable.ajax', () => {
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'
});

Expand Down Expand Up @@ -692,7 +704,6 @@ describe('Observable.ajax', () => {
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'
});

Expand Down
6 changes: 3 additions & 3 deletions src/internal/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface AjaxRequest {
responseType?: string;
}

function getCORSRequest(this: AjaxRequest): XMLHttpRequest {
function getCORSRequest(): XMLHttpRequest {
if (root.XMLHttpRequest) {
return new root.XMLHttpRequest();
} else if (!!root.XDomainRequest) {
Expand Down Expand Up @@ -155,9 +155,9 @@ export class AjaxObservable<T> extends Observable<T> {
const request: AjaxRequest = {
async: true,
createXHR: function(this: AjaxRequest) {
return this.crossDomain ? getCORSRequest.call(this) : getXMLHttpRequest();
return this.crossDomain ? getCORSRequest() : getXMLHttpRequest();
},
crossDomain: false,
crossDomain: true,
withCredentials: false,
headers: {},
method: 'GET',
Expand Down