Mock. Js will modify responseType and cause the download file to be formatted incorrectly

There was something wrong with the last export interface of the new demand the day before yesterday. Because of the new query condition, and the query condition is more complex (object array), so we changed the GET request to POST, before the URL was directly downloaded in the A tag. After the change, the self-test found that the file could not be opened after downloading, but Swagger was normal, so the problem could be basically determined in the front end. All kinds of methods tried countless times, all failed, even in the group called a big man to long-distance two hours. Still no problem.

Only two things were wrong:

  1. ResponseType (blob); responseType (blob);

  1. The returned data is in string format

Later, we asked our leader to help us look at it. After looking at it all morning, we had thought about the realization of back-end transformation. Turns out there’s a blog that says mocks affect file downloads.

Then unmock the introduction and test that it works. [mock] [mock] [mock] [responseType

After this wave, I only feel that it is best to determine the key words when I encounter difficult and complicated diseases, and even change a few more keywords to search. The investigation should be careful, and the early investigation is actually very close to the answer.

// Initializes properties and methods associated with Response
Util.extend(MockXMLHttpRequest.prototype, {
   responseURL: ' '.status: MockXMLHttpRequest.UNSENT,
   statusText: ' '.// https://xhr.spec.whatwg.org/#the-getresponseheader()-method
   getResponseHeader: function(name) {
       / / native XHR
       if (!this.match) {
           return this.custom.xhr.getResponseHeader(name)
       }

       / / intercept XHR
       return this.custom.responseHeaders[name.toLowerCase()]
   },
   // https://xhr.spec.whatwg.org/#the-getallresponseheaders()-method
   // http://www.utf8-chartable.de/
   getAllResponseHeaders: function() {
       / / native XHR
       if (!this.match) {
           return this.custom.xhr.getAllResponseHeaders()
       }

       / / intercept XHR
       var responseHeaders = this.custom.responseHeaders
       var headers = ' '
       for (var h in responseHeaders) {
           if(! responseHeaders.hasOwnProperty(h))continue
           headers += h + ':' + responseHeaders[h] + '\r\n'
       }
       return headers
   },
   overrideMimeType: function( /*mime*/ ) {},
   responseType: ' '.// '', 'text', 'arraybuffer', 'blob', 'document', 'json'
   response: null.responseText: ' '.responseXML: null
})
Copy the code