allow parsing of response body even if status was unauthorized (client-specific work...
authorJustin Wind <justin.wind+git@gmail.com>
Fri, 7 Oct 2022 18:46:01 +0000 (11:46 -0700)
committerJustin Wind <justin.wind+git@gmail.com>
Fri, 7 Oct 2022 18:46:01 +0000 (11:46 -0700)
lib/communication.js
test/lib/communication.js

index 0116548d6539eb70fe229df71aee35cf86305034..8f6110f6e3a9c3438a0855ead9af3db3eb59b0e9 100644 (file)
@@ -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;
index f544d80f67304ce8e47468623368b635e487e1a7..9781cd65b05088664a1d1dee04282d12ff31bf05 100644 (file)
@@ -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 () {