From 13bf4666b071dfc542ce58a80fac23e3afd6832a Mon Sep 17 00:00:00 2001 From: Sefa Ilkimen Date: Fri, 14 Jun 2019 02:29:45 +0200 Subject: [PATCH] feature #171: support for responseType "blob" --- CHANGELOG.md | 5 +++++ README.md | 5 +++-- package.json | 2 +- test/e2e-app-template/www/index.html | 2 +- test/e2e-specs.js | 26 +++++++++++++++++++++++++- www/helpers.js | 13 +++++++++++-- 6 files changed, 46 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a08430b..238b217 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 2.1.0 + +- Feature #216: Support for response type `arraybuffer` +- Feature #171: Support for response type `blob` + ## 2.0.11 - Fixed #221: headers not set on Android when request fails due to non-success status code diff --git a/README.md b/README.md index 6364ec4..23b4f3c 100644 --- a/README.md +++ b/README.md @@ -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) * `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: - * `text` use this for all kind of text responses (e.g. JSON, XML, HTML, plain text, etc.) - * `arraybuffer` use this one for binary responses + * `text`: data is returned as decoded string, use this for all kinds of string responses (e.g. JSON, XML, HTML, plain text, etc.) + * `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 * `followRedirect`: enable or disable automatically following redirects * `headers`: headers object (key value pair), will be merged with global values diff --git a/package.json b/package.json index 1ab34fc..e5c55ef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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", "scripts": { "updatecert": "node ./scripts/update-e2e-server-cert.js && node ./scripts/update-e2e-client-cert.js", diff --git a/test/e2e-app-template/www/index.html b/test/e2e-app-template/www/index.html index 3247ed6..8f5da01 100644 --- a/test/e2e-app-template/www/index.html +++ b/test/e2e-app-template/www/index.html @@ -1,7 +1,7 @@ - + diff --git a/test/e2e-specs.js b/test/e2e-specs.js index 17c46d1..c37aad8 100644 --- a/test/e2e-specs.js +++ b/test/e2e-specs.js @@ -697,12 +697,13 @@ const tests = [ }, { 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) { var url = 'https://httpbin.org/image/jpeg'; var options = { method: 'get', responseType: 'arraybuffer' }; var success = function (response) { resolve({ + isArrayBuffer: response.data.constructor === ArrayBuffer, hash: helpers.hashArrayBuffer(response.data), byteLength: response.data.byteLength }); @@ -711,10 +712,33 @@ const tests = [ }, validationFunc: function (driver, result) { result.type.should.be.equal('resolved'); + result.data.isArrayBuffer.should.be.equal(true); result.data.hash.should.be.equal(-1032603775); 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"', expected: 'rejected: {"status": 418, ...', diff --git a/www/helpers.js b/www/helpers.js index e6180d5..18d8ae4 100644 --- a/www/helpers.js +++ b/www/helpers.js @@ -3,7 +3,7 @@ module.exports = function init(jsUtil, cookieHandler, messages, base64) { var validCertModes = ['default', 'nocheck', 'pinned', 'legacy']; var validClientAuthModes = ['none', 'systemstore', 'buffer']; var validHttpMethods = ['get', 'put', 'post', 'patch', 'head', 'delete', 'upload', 'download']; - var validResponseTypes = ['text','arraybuffer']; + var validResponseTypes = ['text','arraybuffer', 'blob']; var interface = { b64EncodeUnicode: b64EncodeUnicode, @@ -238,7 +238,16 @@ module.exports = function init(jsUtil, cookieHandler, messages, base64) { return function (response) { // arraybuffer 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);