-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhttp_exception.test.ts
142 lines (138 loc) · 3.59 KB
/
http_exception.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2020 Liam Tan. All rights reserved. MIT license.
import { STATUS_TEXT, Status, assertEquals } from "./deps.ts";
import {
HttpException,
BadRequestException,
UnauthorizedException,
PaymentRequiredException,
ForbiddenException,
NotFoundException,
MethodNotAllowedException,
RequestTimeoutException,
UnsupportedMediaTypeException,
TeapotException,
UnprocessableEntityException,
TooManyRequestsException,
RequestHeaderFieldsTooLargeException,
InternalServerErrorException,
NotImplementedException,
BadGatewayException,
ServiceUnavailableException,
GatewayTimeoutException,
} from "./http_exception.ts";
import { Newable } from "./types.ts";
Deno.test({
name: "HttpException.getError returns object literal of correct shape",
fn() {
const testStatus: Status = Status.Teapot;
const testMsg: string = "testMessage";
const testException: HttpException = new HttpException(testStatus, testMsg);
assertEquals(testException.getError(), { error: testMsg, status: testStatus });
},
});
/**
* Run tests for specific `HttpException` subclasses here.
*/
const testData: Array<{
name: string;
exception: Newable<HttpException>;
status: Status;
}> = [
{
name: "BadRequestException",
exception: BadRequestException,
status: Status.BadRequest,
},
{
name: "UnauthorizedException",
exception: UnauthorizedException,
status: Status.Unauthorized,
},
{
name: "PaymentRequiredException",
exception: PaymentRequiredException,
status: Status.PaymentRequired,
},
{
name: "ForbiddenException",
exception: ForbiddenException,
status: Status.Forbidden,
},
{
name: "NotFoundException",
exception: NotFoundException,
status: Status.NotFound,
},
{
name: "MethodNotAllowedException",
exception: MethodNotAllowedException,
status: Status.MethodNotAllowed,
},
{
name: "RequestTimeoutException",
exception: RequestTimeoutException,
status: Status.RequestTimeout,
},
{
name: "UnsupportedMediaTypeException",
exception: UnsupportedMediaTypeException,
status: Status.UnsupportedMediaType,
},
{
name: "TeapotException",
exception: TeapotException,
status: Status.Teapot,
},
{
name: "UnprocessableEntityException",
exception: UnprocessableEntityException,
status: Status.UnprocessableEntity,
},
{
name: "TooManyRequestsException",
exception: TooManyRequestsException,
status: Status.TooManyRequests,
},
{
name: "RequestHeaderFieldsTooLargeException",
exception: RequestHeaderFieldsTooLargeException,
status: Status.RequestHeaderFieldsTooLarge,
},
{
name: "InternalServerErrorException",
exception: InternalServerErrorException,
status: Status.InternalServerError,
},
{
name: "NotImplementedException",
exception: NotImplementedException,
status: Status.NotImplemented,
},
{
name: "BadGatewayException",
exception: BadGatewayException,
status: Status.BadGateway,
},
{
name: "ServiceUnavailableException",
exception: ServiceUnavailableException,
status: Status.ServiceUnavailable,
},
{
name: "GatewayTimeoutException",
exception: GatewayTimeoutException,
status: Status.GatewayTimeout,
},
];
for (const testDataItem of testData) {
Deno.test({
name: `${testDataItem.name} should return error with status ${testDataItem.status}`,
fn() {
const instance: HttpException = new testDataItem.exception();
assertEquals(instance.getError(), {
error: STATUS_TEXT.get(testDataItem.status),
status: testDataItem.status,
});
},
});
}