From 4197ad5aa88575a360f0323495effb4e545bc2a8 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Tue, 21 Dec 2021 11:40:59 +0300 Subject: [PATCH] test(RequestClient): fix error handling --- src/request/RequestClient.ts | 11 ++++++++--- src/test/RequestClient.spec.ts | 16 ++++++++++++---- src/test/SAS_server_app.ts | 1 + 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index 6594df5..3e18632 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -215,6 +215,7 @@ export class RequestClient implements HttpClient { .post(url, data, { headers, withCredentials: true }) .then((response) => { throwIfError(response) + return this.parseResponse(response) }) .catch(async (e) => { @@ -464,6 +465,8 @@ export class RequestClient implements HttpClient { if (e instanceof LoginRequiredError) { this.clearCsrfTokens() + + throw e } if (response?.status === 403 || response?.status === 449) { @@ -486,7 +489,8 @@ export class RequestClient implements HttpClient { else return } - throw prefixMessage(e, 'Error while handling error. ') + if (e.message) throw e + else throw prefixMessage(e, 'Error while handling error. ') } protected parseResponse(response: AxiosResponse) { @@ -538,8 +542,9 @@ export class RequestClient implements HttpClient { this.httpClient = createAxiosInstance(baseUrl, httpsAgent) - this.httpClient.defaults.validateStatus = (status) => - status >= 200 && status < 401 + this.httpClient.defaults.validateStatus = (status) => { + return status >= 200 && status <= 401 + } } } diff --git a/src/test/RequestClient.spec.ts b/src/test/RequestClient.spec.ts index 961043c..6a64e83 100644 --- a/src/test/RequestClient.spec.ts +++ b/src/test/RequestClient.spec.ts @@ -5,6 +5,8 @@ import { app, mockedAuthResponse } from './SAS_server_app' import { ServerType } from '@sasjs/utils' import SASjs from '../SASjs' import * as axiosModules from '../utils/createAxiosInstance' +import { LoginRequiredError } from '../types/errors' +import { prefixMessage } from '@sasjs/utils/error' const axiosActual = jest.requireActual('axios') @@ -55,8 +57,11 @@ describe('RequestClient', () => { it('should response the POST method with Unauthorized', async () => { await expect( adapter.getAccessToken('clientId', 'clientSecret', 'incorrect') - ).rejects.toThrow( - 'Error while getting access token. Request failed with status code 401' + ).rejects.toEqual( + prefixMessage( + new LoginRequiredError(), + 'Error while getting access token. ' + ) ) }) }) @@ -132,8 +137,11 @@ describe('RequestClient - Self Signed Server', () => { it('should response the POST method with Unauthorized', async () => { await expect( adapter.getAccessToken('clientId', 'clientSecret', 'incorrect') - ).rejects.toThrow( - 'Error while getting access token. Request failed with status code 401' + ).rejects.toEqual( + prefixMessage( + new LoginRequiredError(), + 'Error while getting access token. ' + ) ) }) }) diff --git a/src/test/SAS_server_app.ts b/src/test/SAS_server_app.ts index e4cff7a..aa8aa2c 100644 --- a/src/test/SAS_server_app.ts +++ b/src/test/SAS_server_app.ts @@ -18,6 +18,7 @@ app.get('/', function (req: any, res: any) { app.post('/SASLogon/oauth/token', function (req: any, res: any) { let valid = true + // capture the encoded form data req.on('data', (data: any) => { const resData = data.toString()