This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marcel Kloubert
committed
Jan 7, 2024
1 parent
e5f0382
commit 11fc9a3
Showing
19 changed files
with
541 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
src/__tests__/utils/throwOnUnexpectedApiResponse.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import createServer from "@egomobile/http-server"; | ||
import type { AxiosResponse } from "axios"; | ||
import assert from "node:assert"; | ||
import request from "supertest"; | ||
import { apiResponse } from "../../responses"; | ||
import { UnexpectedApiResponseError } from "../../types"; | ||
import { ApiResponseValidator, throwOnUnexpectedApiResponse } from "../../utils"; | ||
import { binaryParser } from "../_utils"; | ||
|
||
const NO_ERROR = Symbol("NO_ERROR"); | ||
|
||
describe("throwOnUnexpectedApiResponse()", () => { | ||
it("should throw UnexpectedApiResponseError using single status code", async () => { | ||
const app = createServer(); | ||
|
||
app.get("/", async (request, response) => { | ||
apiResponse(request, response) | ||
.noSuccess() | ||
.withStatus(400) | ||
.addMessage({ | ||
"code": 40000, | ||
"message": "Invalid input data" | ||
}) | ||
.send(); | ||
}); | ||
|
||
const response = await request(app).get("/") | ||
.send() | ||
.parse(binaryParser); | ||
|
||
const data = response.body; | ||
expect(Buffer.isBuffer(data)).toBe(true); | ||
|
||
const dataObj = JSON.parse( | ||
data.toString("utf8") | ||
); | ||
|
||
const axiosResponse = { | ||
"config": undefined!, | ||
"data": dataObj, | ||
"headers": response.headers, | ||
"status": response.statusCode, | ||
"statusText": response.text | ||
} as AxiosResponse; | ||
|
||
let lastError: any = NO_ERROR; | ||
|
||
try { | ||
await throwOnUnexpectedApiResponse(axiosResponse, 200); | ||
} | ||
catch (error) { | ||
lastError = error; | ||
} | ||
|
||
assert.strictEqual(typeof lastError, "object"); | ||
assert.strictEqual(lastError instanceof UnexpectedApiResponseError, true); | ||
}); | ||
|
||
it("should throw UnexpectedApiResponseError using list of status codes", async () => { | ||
const app = createServer(); | ||
|
||
app.get("/", async (request, response) => { | ||
apiResponse(request, response) | ||
.noSuccess() | ||
.withStatus(400) | ||
.addMessage({ | ||
"code": 40000, | ||
"message": "Invalid input data" | ||
}) | ||
.send(); | ||
}); | ||
|
||
const response = await request(app).get("/") | ||
.send() | ||
.parse(binaryParser); | ||
|
||
const data = response.body; | ||
expect(Buffer.isBuffer(data)).toBe(true); | ||
|
||
const dataObj = JSON.parse( | ||
data.toString("utf8") | ||
); | ||
|
||
const axiosResponse = { | ||
"config": undefined!, | ||
"data": dataObj, | ||
"headers": response.headers, | ||
"status": response.statusCode, | ||
"statusText": response.text | ||
} as AxiosResponse; | ||
|
||
let lastError: any = NO_ERROR; | ||
|
||
try { | ||
await throwOnUnexpectedApiResponse(axiosResponse, [200, 201]); | ||
} | ||
catch (error) { | ||
lastError = error; | ||
} | ||
|
||
assert.strictEqual(lastError instanceof UnexpectedApiResponseError, true); | ||
}); | ||
|
||
it("should throw UnexpectedApiResponseError using validation functions", async () => { | ||
const app = createServer(); | ||
|
||
app.get("/", async (request, response) => { | ||
apiResponse(request, response) | ||
.noSuccess() | ||
.withStatus(400) | ||
.addMessage({ | ||
"code": 40000, | ||
"message": "Invalid input data" | ||
}) | ||
.send(); | ||
}); | ||
|
||
const response = await request(app).get("/") | ||
.send() | ||
.parse(binaryParser); | ||
|
||
const data = response.body; | ||
expect(Buffer.isBuffer(data)).toBe(true); | ||
|
||
const dataObj = JSON.parse( | ||
data.toString("utf8") | ||
); | ||
|
||
const axiosResponse = { | ||
"config": undefined!, | ||
"data": dataObj, | ||
"headers": response.headers, | ||
"status": response.statusCode, | ||
"statusText": response.text | ||
} as AxiosResponse; | ||
|
||
const validatorFunctions: ApiResponseValidator[] = [ | ||
(context) => { | ||
return context.response.status === 200; | ||
}, | ||
async (context) => { | ||
return context.response.status === 200; | ||
} | ||
]; | ||
|
||
for (const validator of validatorFunctions) { | ||
let lastError: any = NO_ERROR; | ||
|
||
try { | ||
await throwOnUnexpectedApiResponse(axiosResponse, validator); | ||
} | ||
catch (error) { | ||
lastError = error; | ||
} | ||
|
||
assert.strictEqual(lastError instanceof UnexpectedApiResponseError, true); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// This file is part of the @egomobile/api-utils distribution. | ||
// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/) | ||
// | ||
// @egomobile/api-utils is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as | ||
// published by the Free Software Foundation, version 3. | ||
// | ||
// @egomobile/api-utils is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
export * from "./unexpectedApiResponseError"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// This file is part of the @egomobile/api-utils distribution. | ||
// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/) | ||
// | ||
// @egomobile/api-utils is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as | ||
// published by the Free Software Foundation, version 3. | ||
// | ||
// @egomobile/api-utils is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import type { AxiosResponse } from "axios"; | ||
|
||
/** | ||
* Is thrown on an unexpected API response. | ||
*/ | ||
export class UnexpectedApiResponseError extends Error { | ||
/** | ||
* Initializes a new instance of that class. | ||
* | ||
* @param {AxiosResponse} response The response context. | ||
* @param {string} [message] The optional message. | ||
*/ | ||
public constructor( | ||
public readonly response: AxiosResponse, | ||
message?: string, | ||
) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,3 +68,5 @@ export interface IApiResponseBuilderFactoryOptions { | |
*/ | ||
response: ServerResponse; | ||
} | ||
|
||
export * from "./errors"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.