ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests
MIT License
3.44k stars 244 forks source link

Question: Handle response type blob #275

Closed qapla47 closed 3 years ago

qapla47 commented 4 years ago

I have a get request that downloads a file. It has a configuration of

const downloadFile = fileId => (dispatch, getState) => {
  const token = getState().auth.token
  let config = {
    responseType: 'blob',
    headers: { 'Content-Type': 'application/zip' }
  }
  return axios
    .get('download/endpoint`, config)
    .then(res =>
      let url = window.URL.createObjectURL(res.data)
      ... other stuff related to downloading files ...
    })
    .catch(err => { error block })

I mock my instance, and create a fake blob

let blob = new Blob([], {type: 'application/zip' })
let fakeFile = blob

and then create my request

mock
  .onGet('download/endpoint')
  .reply(config => {
    return [
      200,
      fakeFile,
      { Header Configuration }
    ]
  })

However, when my response is received, my data no longer contains a Blob.

res.data instanceof Blob

Checking the instance of returns false, while in my actual implementation, it returns true This prevents the

window.URL.createObjectURL(res.data)

from functioning as it is expecting a Blob, not an object.

How can I get this test to pass?

erykpiast commented 3 years ago

I have a similar issue. I think that's all because the case of Blob isn't correctly handled here. If we add a simple check like utils.isBlob(data) and pass it raw (instead of making a deep copy), everything should work as expected.

thefat32 commented 3 years ago

I have a similar issue. I think that's all because the case of Blob isn't correctly handled here. If we add a simple check like utils.isBlob(data) and pass it raw (instead of making a deep copy), everything should work as expected.

Please merge this fix!