-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Fix ajax upload progress event is not set correctly #2200
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -652,6 +652,67 @@ describe('Observable.ajax', () => { | |
expect(complete).to.be.true; | ||
}); | ||
|
||
it('should emit progress event when progressSubscriber is specified', function() { | ||
const spy = sinon.spy(); | ||
const progressSubscriber = (<any>{ | ||
next: spy, | ||
error: () => { | ||
// noop | ||
}, | ||
complete: () => { | ||
// noop | ||
} | ||
}); | ||
|
||
Rx.Observable.ajax({ | ||
url: '/flibbertyJibbet', | ||
progressSubscriber | ||
}) | ||
.subscribe(); | ||
|
||
const request = MockXMLHttpRequest.mostRecent; | ||
|
||
request.respondWith({ | ||
'status': 200, | ||
'contentType': 'application/json', | ||
'responseText': JSON.stringify({}) | ||
}, 3); | ||
|
||
expect(spy).to.be.calledThrice; | ||
}); | ||
|
||
it('should emit progress event when progressSubscriber is specified in IE', function() { | ||
const spy = sinon.spy(); | ||
const progressSubscriber = (<any>{ | ||
next: spy, | ||
error: () => { | ||
// noop | ||
}, | ||
complete: () => { | ||
// noop | ||
} | ||
}); | ||
|
||
root.XMLHttpRequest = MockXMLHttpRequestInternetExplorer; | ||
root.XDomainRequest = MockXMLHttpRequestInternetExplorer; | ||
|
||
Rx.Observable.ajax({ | ||
url: '/flibbertyJibbet', | ||
progressSubscriber | ||
}) | ||
.subscribe(); | ||
|
||
const request = MockXMLHttpRequest.mostRecent; | ||
|
||
request.respondWith({ | ||
'status': 200, | ||
'contentType': 'application/json', | ||
'responseText': JSON.stringify({}) | ||
}, 3); | ||
|
||
expect(spy.callCount).to.equal(3); | ||
}); | ||
|
||
}); | ||
|
||
it('should work fine when XMLHttpRequest onreadystatechange property is monkey patched', function() { | ||
|
@@ -734,16 +795,6 @@ describe('Observable.ajax', () => { | |
configurable: true | ||
}); | ||
|
||
Object.defineProperty(root.XMLHttpRequest.prototype, 'upload', { | ||
get() { | ||
return true; | ||
}, | ||
configurable: true | ||
}); | ||
|
||
// mock for onprogress | ||
root.XDomainRequest = true; | ||
|
||
Rx.Observable.ajax({ | ||
url: '/flibbertyJibbet', | ||
progressSubscriber: (<any>{ | ||
|
@@ -763,12 +814,11 @@ describe('Observable.ajax', () => { | |
const request = MockXMLHttpRequest.mostRecent; | ||
|
||
expect(() => { | ||
request.onprogress((<any>'onprogress')); | ||
request.upload.onprogress((<any>'onprogress')); | ||
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. it seems like we should have to test both paths for this. It's tedious, but I want to make sure this works in every browser we can. 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. @Brooooooklyn @kwonoj thoughts? 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. On second thought. I think this can be remedied at a later time. This is fine for now. |
||
}).not.throw(); | ||
|
||
delete root.XMLHttpRequest.prototype.onprogress; | ||
delete root.XMLHttpRequest.prototype.upload; | ||
delete root.XDomainRequest; | ||
}); | ||
|
||
it('should work fine when XMLHttpRequest onerror property is monkey patched', function() { | ||
|
@@ -788,16 +838,6 @@ describe('Observable.ajax', () => { | |
configurable: true | ||
}); | ||
|
||
Object.defineProperty(root.XMLHttpRequest.prototype, 'upload', { | ||
get() { | ||
return true; | ||
}, | ||
configurable: true | ||
}); | ||
|
||
// mock for onprogress | ||
root.XDomainRequest = true; | ||
|
||
Rx.Observable.ajax({ | ||
url: '/flibbertyJibbet' | ||
}) | ||
|
@@ -813,6 +853,5 @@ describe('Observable.ajax', () => { | |
|
||
delete root.XMLHttpRequest.prototype.onerror; | ||
delete root.XMLHttpRequest.prototype.upload; | ||
delete root.XDomainRequest; | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,7 +224,12 @@ export class AjaxSubscriber<T> extends Subscriber<Event> { | |
} else { | ||
this.xhr = xhr; | ||
|
||
// open XHR first | ||
// set up the events before open XHR | ||
// https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest | ||
// You need to add the event listeners before calling open() on the request. | ||
// Otherwise the progress events will not fire. | ||
this.setupEvents(xhr, request); | ||
// open XHR | ||
let result: any; | ||
if (user) { | ||
result = tryCatch(xhr.open).call(xhr, method, url, async, user, password); | ||
|
@@ -244,9 +249,6 @@ export class AjaxSubscriber<T> extends Subscriber<Event> { | |
// set headers | ||
this.setHeaders(xhr, headers); | ||
|
||
// now set up the events | ||
this.setupEvents(xhr, request); | ||
|
||
// finally send the request | ||
result = body ? tryCatch(xhr.send).call(xhr, body) : tryCatch(xhr.send).call(xhr); | ||
if (result === errorObject) { | ||
|
@@ -304,14 +306,18 @@ export class AjaxSubscriber<T> extends Subscriber<Event> { | |
(<any>xhrTimeout).request = request; | ||
(<any>xhrTimeout).subscriber = this; | ||
(<any>xhrTimeout).progressSubscriber = progressSubscriber; | ||
if (xhr.upload && 'withCredentials' in xhr && root.XDomainRequest) { | ||
if (xhr.upload && 'withCredentials' in xhr) { | ||
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. is this passes browser test on IE9? Seems IE9 relies on 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. I don't know why 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. I think 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. I've checked the specs of XDomainRequest, and I'm sure the only difference between |
||
if (progressSubscriber) { | ||
let xhrProgress: (e: ProgressEvent) => void; | ||
xhrProgress = function(e: ProgressEvent) { | ||
const { progressSubscriber } = (<any>xhrProgress); | ||
progressSubscriber.next(e); | ||
}; | ||
xhr.onprogress = xhrProgress; | ||
if (root.XDomainRequest) { | ||
xhr.onprogress = xhrProgress; | ||
} else { | ||
xhr.upload.onprogress = xhrProgress; | ||
} | ||
(<any>xhrProgress).progressSubscriber = progressSubscriber; | ||
} | ||
let xhrError: (e: ErrorEvent) => void; | ||
|
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.
LOL... is this really a thing? "Thrice"? Hahaha... How about just
callCount(3)
so I don't snicker every time I read it? 😄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.
😂