Fixed bugs in blob handling of jquery

This commit is contained in:
Tino Fuhrmann 2020-05-24 21:08:31 +02:00
parent 277e4ed49d
commit 9b8e4c27c2
3 changed files with 16 additions and 18 deletions

View File

@ -21,14 +21,12 @@ export class JQueryHttpLibrary implements HttpLibrary {
data: body
};
/**
* Allow receiving binary data with jquery ajax
*
* Source: https://keyangxiang.com/2017/09/01/HTML5-XHR-download-binary-content-as-Blob/
*/
requestOptions.beforeSend = (jqXHR: any, settings: any) => {
settings.xhr().responseType = "blob";
};
// If we want a blob, we have to set the xhrFields' responseType AND add a
// custom converter to overwrite the default deserialization of JQuery...
requestOptions["xhrFields"] = { responseType: 'blob' };
requestOptions["converters"] = {}
requestOptions["converters"]["* blob"] = (result:any) => result;
requestOptions["dataType"] = "blob";
if (request.getHeaders()['Content-Type']) {
@ -51,6 +49,7 @@ export class JQueryHttpLibrary implements HttpLibrary {
if (body && body.constructor.name == "FormData") {
requestOptions.contentType = false;
}
const sentRequest = $.ajax(requestOptions);
const resultPromise = new Promise<ResponseContext>((resolve, reject) => {

View File

@ -21,14 +21,12 @@ export class JQueryHttpLibrary implements HttpLibrary {
data: body
};
/**
* Allow receiving binary data with jquery ajax
*
* Source: https://keyangxiang.com/2017/09/01/HTML5-XHR-download-binary-content-as-Blob/
*/
requestOptions.beforeSend = (jqXHR: any, settings: any) => {
settings.xhr().responseType = "blob";
};
// If we want a blob, we have to set the xhrFields' responseType AND add a
// custom converter to overwrite the default deserialization of JQuery...
requestOptions["xhrFields"] = { responseType: 'blob' };
requestOptions["converters"] = {}
requestOptions["converters"]["* blob"] = (result:any) => result;
requestOptions["dataType"] = "blob";
if (request.getHeaders()['Content-Type']) {
@ -51,6 +49,7 @@ export class JQueryHttpLibrary implements HttpLibrary {
if (body && body.constructor.name == "FormData") {
requestOptions.contentType = false;
}
const sentRequest = $.ajax(requestOptions);
const resultPromise = new Promise<ResponseContext>((resolve, reject) => {

View File

@ -17,7 +17,7 @@ for (let libName in libs) {
return new Promise((resolve, reject) => {
lib.send(requestContext).toPromise().then((resp: petstore.ResponseContext) => {
assert.ok(resp.httpStatusCode, 200, "Expected status code to be 200");
return resp.body.text()
return resp.body.text();
}).then((txtBody: string) => {
let body = JSON.parse(txtBody);
assert.ok(body["headers"]);
@ -46,7 +46,7 @@ for (let libName in libs) {
lib.send(requestContext).toPromise().then(
(resp: petstore.ResponseContext) => {
assert.ok(resp.httpStatusCode, 200, "Expected status code to be 200");
return resp.body.text()
return resp.body.text();
}).then((txtBody: any) => {
let body = JSON.parse(txtBody);
assert.ok(body["headers"]);