From: Justin Wind Date: Fri, 7 Oct 2022 18:46:01 +0000 (-0700) Subject: allow parsing of response body even if status was unauthorized (client-specific work... X-Git-Tag: v1.1.7~1 X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=dca6a761e6fe437a3fbeb24540bb6fa5d0b8eb12;p=squeep-indieauth-helper allow parsing of response body even if status was unauthorized (client-specific work-around) --- diff --git a/lib/communication.js b/lib/communication.js index 0116548..8f6110f 100644 --- a/lib/communication.js +++ b/lib/communication.js @@ -124,6 +124,17 @@ class Communication { } + /** + * Valid response statuses. + * Allow 401 as a workaround for one specific client which return such on + * its client identifier endpoint when not yet authenticated. + * @param {Number} status + * @returns {Boolean} + */ + static _validateStatus(status) { + return (status >= 200 && status < 300) || status == 401; + } + /** * A request config skeleton. * @param {String} method @@ -144,6 +155,8 @@ class Communication { responseType: 'text', // So force the matter by eliding all response transformations transformResponse: [ (res) => res ], + + validateStatus: Communication._validateStatus, }; Object.entries(params).map(([k, v]) => config.params.set(k, v)); return config; diff --git a/test/lib/communication.js b/test/lib/communication.js index f544d80..9781cd6 100644 --- a/test/lib/communication.js +++ b/test/lib/communication.js @@ -162,6 +162,7 @@ describe('Communication', function () { }, params: expectedUrlObj.searchParams, responseType: 'text', + validateStatus: Communication._validateStatus, }; const result = Communication._axiosConfig(method, urlObj, body, params, { 'Content-Type': contentType, @@ -179,6 +180,7 @@ describe('Communication', function () { headers: {}, params: expectedUrlObj.searchParams, responseType: 'text', + validateStatus: Communication._validateStatus, }; const result = Communication._axiosConfig(method, urlObj); delete result.transformResponse; @@ -196,17 +198,32 @@ describe('Communication', function () { headers: {}, params: urlObj.searchParams, responseType: 'text', + validateStatus: Communication._validateStatus, }; const result = Communication._axiosConfig(method, urlObj, body, params, {}); delete result.transformResponse; assert.deepStrictEqual(result, expected); - }); it('covers null response transform', function () { const urlObj = new URL(requestUrl); const result = Communication._axiosConfig('GET', urlObj, undefined, {}, {}); result.transformResponse[0](); }); + + describe('_validateStatus', function () { + it('allows normal valid', function () { + const result = Communication._validateStatus(200); + assert.strictEqual(result, true); + }); + it('allows unauthorized', function () { + const result = Communication._validateStatus(401); + assert.strictEqual(result, true); + }); + it('rejects invalid', function () { + const result = Communication._validateStatus(400); + assert.strictEqual(result, false); + }); + }); // _validateStatus }); // Axios Configurations describe('_baseUrlString', function () {