Skip to content

Commit 2fce789

Browse files
committed
tests(types): added tests for Response and Errors
1 parent 30706d8 commit 2fce789

File tree

1 file changed

+141
-25
lines changed

1 file changed

+141
-25
lines changed

index.test-d.ts

Lines changed: 141 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,143 @@
11
import {expectType} from 'tsd';
22
import express from 'express'
3-
import {OAuth2Server, Request, Response} from '.';
4-
import { Request as UndiciRequest } from 'undici';
5-
6-
expectType<Request>(new Request());
7-
expectType<Request>(new Request({}));
8-
9-
const req1 = new Request({ method: 'get', headers: { x: 'y'} });
10-
expectType<string>(req1.method);
11-
expectType<Record<string, string>>(req1.headers);
12-
13-
// check for express request compatibility
14-
const expressReq = new express.Request({ method: 'get', query: { moo: 'foo' }, headers: { x: 'y'} });
15-
const req2 = new Request(expressReq);
16-
expectType<string>(req2.method);
17-
expectType<Record<string, string>>(req2.headers);
18-
expectType<Record<string, string>>(req2.query);
19-
20-
// check for compatibility with fetch api Request
21-
// we use undici Request as a stand-in for fetch api Request
22-
// because we still support older versions of Node.js
23-
const undiciReq = new UndiciRequest('https://example.com', { method: 'get', headers: { x: 'y'} });
24-
const req3 = new Request(undiciReq);
25-
expectType<string>(req3.method);
26-
expectType<Record<string, string>>(req3.headers);
27-
expectType<Record<string, string>>(req3.query);
3+
import {
4+
OAuth2Server,
5+
OAuthError,
6+
Request,
7+
Response,
8+
AccessDeniedError,
9+
InsufficientScopeError,
10+
InvalidArgumentError,
11+
InvalidClientError,
12+
InvalidGrantError,
13+
InvalidRequestError,
14+
InvalidScopeError,
15+
InvalidTokenError,
16+
ServerError,
17+
UnauthorizedClientError,
18+
UnauthorizedRequestError,
19+
UnsupportedGrantTypeError,
20+
UnsupportedResponseTypeError
21+
} from '.';
22+
import {Request as UndiciRequest, Response as UndiciResponse} from 'undici';
23+
24+
// ----------------------------------------------------------------------------
25+
// REQUEST
26+
// ----------------------------------------------------------------------------
27+
function testRequest(req: Request): void {
28+
expectType<string>(req.method);
29+
expectType<Record<string, string>>(req.headers);
30+
expectType<Record<string, string>>(req.query);
31+
expectType<any>(req.body);
32+
}
33+
34+
const args = [
35+
undefined,
36+
{},
37+
{method: 'get'},
38+
{method: 'get', headers: {x: 'y'}},
39+
{method: 'get', query: {moo: 'foo'}},
40+
{method: 'get', headers: {x: 'y'}, query: {moo: 'foo'}},
41+
{method: 'get', headers: {x: 'y'}, query: {moo: 'foo', bar: 'baz'}},
42+
// check for express request compatibility
43+
new express.Request({
44+
method: 'get',
45+
query: {moo: 'foo'},
46+
headers: {x: 'y'}
47+
}),
48+
// check for compatibility with fetch api Request
49+
// we use undici Request as a stand-in for fetch api Request
50+
// because we still support older versions of Node.js
51+
new UndiciRequest(
52+
'https://example.com?moo=foo%2Cbar',
53+
{
54+
method: 'get', headers: {x: 'y'}
55+
})
56+
];
57+
58+
for (const arg of args) {
59+
testRequest(new Request(arg));
60+
}
61+
62+
63+
// ----------------------------------------------------------------------------
64+
// RESPONSE
65+
// ----------------------------------------------------------------------------
66+
function testResponse(res: Response): void {
67+
expectType<number>(res.status);
68+
expectType<Record<string, string>>(res.headers);
69+
expectType<any>(res.body);
70+
}
71+
72+
const resArgs = [
73+
undefined,
74+
{},
75+
{status: 200},
76+
{status: 200, headers: {x: 'y'}},
77+
{status: 200, body: 'foo'},
78+
{status: 200, headers: {x: 'y'}, body: 'foo'},
79+
{status: 200, headers: {x: 'y'}, body: 'foo', statusText: 'OK'},
80+
// check for express response compatibility
81+
new express.Response({
82+
status: 200,
83+
headers: {x: 'y'},
84+
body: 'foo'
85+
}),
86+
// check for compatibility with fetch api Response
87+
// we use undici Response as a stand-in for fetch api Response
88+
// because we still support older versions of Node.js
89+
new UndiciResponse(
90+
'foo',
91+
{
92+
status: 200,
93+
headers: {x: 'y'},
94+
statusText: 'OK'
95+
})
96+
];
97+
98+
for (const arg of resArgs) {
99+
testResponse(new Response(arg));
100+
}
101+
102+
// ----------------------------------------------------------------------------
103+
// ERRORS
104+
// ----------------------------------------------------------------------------
105+
function testError(err: OAuthError): void {
106+
expectType<string>(err.name);
107+
expectType<string>(err.message);
108+
expectType<number>(err.code);
109+
}
110+
111+
const errorTypes = [
112+
AccessDeniedError,
113+
InsufficientScopeError,
114+
InvalidArgumentError,
115+
InvalidClientError,
116+
InvalidGrantError,
117+
InvalidRequestError,
118+
InvalidScopeError,
119+
InvalidTokenError,
120+
ServerError,
121+
UnauthorizedClientError,
122+
UnauthorizedRequestError,
123+
UnsupportedGrantTypeError,
124+
UnsupportedResponseTypeError
125+
];
126+
127+
const errorArgs = [
128+
undefined,
129+
{},
130+
{message: 'foo'},
131+
{message: 'foo', code: 400},
132+
{message: 'foo', code: 400, data: {bar: 'baz'}},
133+
// check for express error compatibility
134+
new express.Error('foo'),
135+
// native error compatibility
136+
new Error('foo')
137+
];
138+
139+
for (const arg of errorArgs) {
140+
for (const ErrorType of errorTypes) {
141+
testError(new ErrorType(arg));
142+
}
143+
}

0 commit comments

Comments
 (0)