1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 01:14:36 +00:00

test(RequestClient): fix error handling

This commit is contained in:
Yury Shkoda
2021-12-21 11:40:59 +03:00
parent 2ebd6e11ba
commit 4197ad5aa8
3 changed files with 21 additions and 7 deletions

View File

@@ -215,6 +215,7 @@ export class RequestClient implements HttpClient {
.post<T>(url, data, { headers, withCredentials: true })
.then((response) => {
throwIfError(response)
return this.parseResponse<T>(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<T>(response: AxiosResponse<any>) {
@@ -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
}
}
}

View File

@@ -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. '
)
)
})
})

View File

@@ -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()