Emulate xhr.upload in FakeXMLHttpRequest #185

Closed
meleyal opened this Issue Oct 16, 2012 · 6 comments

Comments

Projects
None yet
4 participants
@meleyal
Contributor

meleyal commented Oct 16, 2012

It would be useful if the FakeXMLHttpRequest object could emulate stuff from the Level 2 spec such as upload, e.g. xhr.upload.addEventListener(...).

Thanks for sinon!

@fbuchinger

This comment has been minimized.

Show comment Hide comment
@fbuchinger

fbuchinger May 7, 2013

+1 for this request... I am writing unit tests for an html5 uploader and missing exactly this functionality

+1 for this request... I am writing unit tests for an html5 uploader and missing exactly this functionality

@meleyal

This comment has been minimized.

Show comment Hide comment
@meleyal

meleyal Jun 12, 2013

Contributor

Total hack, but this seems to work:

Helper:

xhrWithUpload: ->
  xhr = sinon.useFakeXMLHttpRequest()
  requests = []
  xhr.onCreate = (xhr) ->
    xhr.upload = $('<div/>')[0]
    requests.push(xhr)
  { requests }

Usage:

xhr = xhrWithUpload()
...
req = xhr.requests[0]
event = new ProgressEvent('progress', { loaded: 50, total: 100 })
req.upload.dispatchEvent(event)
...
Contributor

meleyal commented Jun 12, 2013

Total hack, but this seems to work:

Helper:

xhrWithUpload: ->
  xhr = sinon.useFakeXMLHttpRequest()
  requests = []
  xhr.onCreate = (xhr) ->
    xhr.upload = $('<div/>')[0]
    requests.push(xhr)
  { requests }

Usage:

xhr = xhrWithUpload()
...
req = xhr.requests[0]
event = new ProgressEvent('progress', { loaded: 50, total: 100 })
req.upload.dispatchEvent(event)
...
@AidasK

This comment has been minimized.

Show comment Hide comment
@AidasK

AidasK Aug 21, 2013

+1 for this too. Can't test upload library for now

AidasK commented Aug 21, 2013

+1 for this too. Can't test upload library for now

@AidasK

This comment has been minimized.

Show comment Hide comment
@AidasK

AidasK Sep 26, 2013

I could not launch Sinon.js tests on windows, so i have wrote my own implementation. It extends existing sinon.js functionality. Take a look at:
https://github.com/resumable2/resumable.js/blob/df46df6cb0540bb4dd5123cfc64236fbc535504f/test/FakeXMLHttpRequestUpload.js

FakeXMLHttpRequestUpload.js file must be loaded after sinon.js library.
Usage is easy, use same response object. In addition it has a helper method "progress".

request.progress(5, 10, true);
...
request.respond(200);

AidasK commented Sep 26, 2013

I could not launch Sinon.js tests on windows, so i have wrote my own implementation. It extends existing sinon.js functionality. Take a look at:
https://github.com/resumable2/resumable.js/blob/df46df6cb0540bb4dd5123cfc64236fbc535504f/test/FakeXMLHttpRequestUpload.js

FakeXMLHttpRequestUpload.js file must be loaded after sinon.js library.
Usage is easy, use same response object. In addition it has a helper method "progress".

request.progress(5, 10, true);
...
request.respond(200);
@meleyal

This comment has been minimized.

Show comment Hide comment
@meleyal

meleyal Sep 26, 2013

Contributor

@AidasK looks good, I will give it a try 👍

Contributor

meleyal commented Sep 26, 2013

@AidasK looks good, I will give it a try 👍

@mantoni

This comment has been minimized.

Show comment Hide comment
@mantoni

mantoni Jan 8, 2014

Owner

Closing since there is basic upload support now. If anything is missing, please open a new issue.

Owner

mantoni commented Jan 8, 2014

Closing since there is basic upload support now. If anything is missing, please open a new issue.

@mantoni mantoni closed this Jan 8, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment