feature #171: support for responseType "blob"

This commit is contained in:
Sefa Ilkimen
2019-06-14 02:29:45 +02:00
parent 1fc3d6230c
commit 13bf4666b0
6 changed files with 46 additions and 7 deletions
+5
View File
@@ -1,5 +1,10 @@
# Changelog # Changelog
## 2.1.0
- Feature #216: Support for response type `arraybuffer`
- Feature #171: Support for response type `blob`
## 2.0.11 ## 2.0.11
- Fixed #221: headers not set on Android when request fails due to non-success status code - Fixed #221: headers not set on Android when request fails due to non-success status code
+3 -2
View File
@@ -203,8 +203,9 @@ The options object contains following keys:
* `params`: query params to be appended to the URL (only applicable on `get`, `head`, `delete`, `upload` or `download` methods) * `params`: query params to be appended to the URL (only applicable on `get`, `head`, `delete`, `upload` or `download` methods)
* `serializer`: data serializer to be used (only applicable on `post`, `put` or `patch` methods), defaults to global serializer value, see [setDataSerializer](#setDataSerializer) for supported values * `serializer`: data serializer to be used (only applicable on `post`, `put` or `patch` methods), defaults to global serializer value, see [setDataSerializer](#setDataSerializer) for supported values
* `responseType`: expected response type, defaults to `text`, needs to be one of the following values: * `responseType`: expected response type, defaults to `text`, needs to be one of the following values:
* `text` use this for all kind of text responses (e.g. JSON, XML, HTML, plain text, etc.) * `text`: data is returned as decoded string, use this for all kinds of string responses (e.g. JSON, XML, HTML, plain text, etc.)
* `arraybuffer` use this one for binary responses * `arraybuffer`: data is returned as [ArrayBuffer instance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
* `blob`: data is returned as [Blob instance](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
* `timeout`: timeout value for the request in seconds, defaults to global timeout value * `timeout`: timeout value for the request in seconds, defaults to global timeout value
* `followRedirect`: enable or disable automatically following redirects * `followRedirect`: enable or disable automatically following redirects
* `headers`: headers object (key value pair), will be merged with global values * `headers`: headers object (key value pair), will be merged with global values
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "cordova-plugin-advanced-http", "name": "cordova-plugin-advanced-http",
"version": "2.0.11", "version": "2.1.0",
"description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning", "description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning",
"scripts": { "scripts": {
"updatecert": "node ./scripts/update-e2e-server-cert.js && node ./scripts/update-e2e-client-cert.js", "updatecert": "node ./scripts/update-e2e-server-cert.js && node ./scripts/update-e2e-client-cert.js",
+1 -1
View File
@@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com https://self-signed.badssl.com http://httpbin.org http://www.columbia.edu 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;"> <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com https://self-signed.badssl.com http://httpbin.org http://www.columbia.edu 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content: blob:;">
<meta name="format-detection" content="telephone=no"> <meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no"> <meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
+25 -1
View File
@@ -697,12 +697,13 @@ const tests = [
}, },
{ {
description: 'should fetch binary correctly when response type is "arraybuffer"', description: 'should fetch binary correctly when response type is "arraybuffer"',
expected: 'resolved: {"hash":-1032603775,"byteLength":35588}', expected: 'resolved: {"isArrayBuffer:true,"hash":-1032603775,"byteLength":35588}',
func: function (resolve, reject) { func: function (resolve, reject) {
var url = 'https://httpbin.org/image/jpeg'; var url = 'https://httpbin.org/image/jpeg';
var options = { method: 'get', responseType: 'arraybuffer' }; var options = { method: 'get', responseType: 'arraybuffer' };
var success = function (response) { var success = function (response) {
resolve({ resolve({
isArrayBuffer: response.data.constructor === ArrayBuffer,
hash: helpers.hashArrayBuffer(response.data), hash: helpers.hashArrayBuffer(response.data),
byteLength: response.data.byteLength byteLength: response.data.byteLength
}); });
@@ -711,10 +712,33 @@ const tests = [
}, },
validationFunc: function (driver, result) { validationFunc: function (driver, result) {
result.type.should.be.equal('resolved'); result.type.should.be.equal('resolved');
result.data.isArrayBuffer.should.be.equal(true);
result.data.hash.should.be.equal(-1032603775); result.data.hash.should.be.equal(-1032603775);
result.data.byteLength.should.be.equal(35588); result.data.byteLength.should.be.equal(35588);
} }
}, },
{
description: 'should fetch binary correctly when response type is "blob"',
expected: 'resolved: {"isBlob":true,byteLength":35588}',
func: function (resolve, reject) {
var url = 'https://httpbin.org/image/jpeg';
var options = { method: 'get', responseType: 'blob' };
var success = function (response) {
resolve({
isBlob: response.data.constructor === Blob,
type: response.data.type,
byteLength: response.data.size
});
};
cordova.plugin.http.sendRequest(url, options, success, reject);
},
validationFunc: function (driver, result) {
result.type.should.be.equal('resolved');
result.data.isBlob.should.be.equal(true);
result.data.type.should.be.equal('image/jpeg');
result.data.byteLength.should.be.equal(35588);
}
},
{ {
description: 'should decode error body even if response type is "arraybuffer"', description: 'should decode error body even if response type is "arraybuffer"',
expected: 'rejected: {"status": 418, ...', expected: 'rejected: {"status": 418, ...',
+11 -2
View File
@@ -3,7 +3,7 @@ module.exports = function init(jsUtil, cookieHandler, messages, base64) {
var validCertModes = ['default', 'nocheck', 'pinned', 'legacy']; var validCertModes = ['default', 'nocheck', 'pinned', 'legacy'];
var validClientAuthModes = ['none', 'systemstore', 'buffer']; var validClientAuthModes = ['none', 'systemstore', 'buffer'];
var validHttpMethods = ['get', 'put', 'post', 'patch', 'head', 'delete', 'upload', 'download']; var validHttpMethods = ['get', 'put', 'post', 'patch', 'head', 'delete', 'upload', 'download'];
var validResponseTypes = ['text','arraybuffer']; var validResponseTypes = ['text','arraybuffer', 'blob'];
var interface = { var interface = {
b64EncodeUnicode: b64EncodeUnicode, b64EncodeUnicode: b64EncodeUnicode,
@@ -238,7 +238,16 @@ module.exports = function init(jsUtil, cookieHandler, messages, base64) {
return function (response) { return function (response) {
// arraybuffer // arraybuffer
if (responseType === validResponseTypes[1]) { if (responseType === validResponseTypes[1]) {
response.data = base64.toArrayBuffer(response.data); var buffer = base64.toArrayBuffer(response.data);
response.data = buffer;
}
// blob
if (responseType === validResponseTypes[2]) {
var buffer = base64.toArrayBuffer(response.data);
var type = response.headers['content-type'] || '';
var blob = new Blob([ buffer ], { type: type });
response.data = blob;
} }
cb(response); cb(response);