Update tests

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2021-11-18 14:56:32 +01:00
parent 4d09c60256
commit 4d97ec07fa
2 changed files with 35 additions and 28 deletions
+1
View File
@@ -3,3 +3,4 @@
---
Deprecate `parseErrorResponse` in favour of `parseErrorResponseBody`. Deprecate `data` field inside `ErrorResponse` in favour of `body`.
Rename the error name for unknown errors from `unknown` to `error`.
@@ -14,11 +14,11 @@
* limitations under the License.
*/
import { parseErrorResponse, ErrorResponse } from './response';
import { parseErrorResponseBody, ErrorResponseBody } from './response';
describe('parseErrorResponse', () => {
describe('parseErrorResponseBody', () => {
it('handles the happy path', async () => {
const body: ErrorResponse = {
const body: ErrorResponseBody = {
error: { name: 'Fours', message: 'Expected fives', stack: 'lines' },
request: { method: 'GET', url: '/' },
response: { statusCode: 444 },
@@ -31,13 +31,13 @@ describe('parseErrorResponse', () => {
headers: new Headers({ 'Content-Type': 'application/json' }),
};
await expect(parseErrorResponse(response as Response)).resolves.toEqual(
await expect(parseErrorResponseBody(response as Response)).resolves.toEqual(
body,
);
});
it('uses request header and text body when wrong content type, even if parsable', async () => {
const body: ErrorResponse = {
const body: ErrorResponseBody = {
error: { name: 'Threes', message: 'Expected twos' },
request: { method: 'GET', url: '/' },
response: { statusCode: 333 },
@@ -50,19 +50,21 @@ describe('parseErrorResponse', () => {
headers: new Headers({ 'Content-Type': 'not-application/not-json' }),
};
await expect(parseErrorResponse(response as Response)).resolves.toEqual({
error: {
name: 'Unknown',
message: `Request failed with status 444 Fours, ${JSON.stringify(
body,
)}`,
await expect(parseErrorResponseBody(response as Response)).resolves.toEqual(
{
error: {
name: 'Error',
message: `Request failed with status 444 Fours, ${JSON.stringify(
body,
)}`,
},
response: { statusCode: 444 },
},
response: { statusCode: 444 },
});
);
});
it('uses request header and text body when not parsable', async () => {
const body: ErrorResponse = {
const body: ErrorResponseBody = {
error: { name: 'Threes', message: 'Expected twos' },
request: { method: 'GET', url: '/' },
response: { statusCode: 333 },
@@ -75,15 +77,17 @@ describe('parseErrorResponse', () => {
headers: new Headers({ 'Content-Type': 'application/json' }),
};
await expect(parseErrorResponse(response as Response)).resolves.toEqual({
error: {
name: 'Unknown',
message: `Request failed with status 444 Fours, ${JSON.stringify(
body,
).substring(1)}`,
await expect(parseErrorResponseBody(response as Response)).resolves.toEqual(
{
error: {
name: 'Error',
message: `Request failed with status 444 Fours, ${JSON.stringify(
body,
).substring(1)}`,
},
response: { statusCode: 444 },
},
response: { statusCode: 444 },
});
);
});
it('uses request header when failing to get body', async () => {
@@ -96,12 +100,14 @@ describe('parseErrorResponse', () => {
headers: new Headers({ 'Content-Type': 'application/json' }),
};
await expect(parseErrorResponse(response as Response)).resolves.toEqual({
error: {
name: 'Unknown',
message: `Request failed with status 444 Fours`,
await expect(parseErrorResponseBody(response as Response)).resolves.toEqual(
{
error: {
name: 'Error',
message: `Request failed with status 444 Fours`,
},
response: { statusCode: 444 },
},
response: { statusCode: 444 },
});
);
});
});