generated from BrunnerLivio/deno-module-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.ts
130 lines (117 loc) · 2.57 KB
/
mod.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
import {
NextFunction,
OpineRequest as Request,
OpineResponse as Response,
} from "./deps.ts";
import { spy } from "./deps.ts";
export function _wrapSpy(func?: Function) {
return func ? spy(func as any) : spy();
}
/**
* Get a mock for opine NextFunction
* @returns mock for express next
*/
export function mockNextFunction(func: Function): NextFunction {
const next = _wrapSpy(func);
return next;
}
/**
* Get a Mock for opine request
* @param additional any properties that should be set explicitly
* @returns a mock for opine Requests
*/
export function mockRequest(additional?: Partial<Request>): Request {
//define properties and methods according to docs - http://expressjs.com/en/4x/api.html#req
const props: Partial<Request> = {
//app: {},
baseUrl: "",
body: {},
//cookies: {},
fresh: true,
//host: "",
hostname: "",
ip: "",
ips: [],
method: "",
originalUrl: "",
params: {},
path: "",
protocol: "",
query: {},
res: mockResponse({}),
route: {},
secure: true,
//signedCookies: {},
stale: true,
subdomains: [],
xhr: true,
headers: additional?.headers ? additional.headers : new Headers({}),
};
const methods: (keyof Request)[] = [
"accepts",
"acceptsCharsets",
"acceptsEncodings",
"acceptsLanguages",
"get",
"is",
"param",
"range",
];
//set properties
const req = {
...props,
...additional,
};
//set methods
methods.forEach((method) => {
req[method] = additional? _wrapSpy(additional[method]): _wrapSpy();
});
return req as Request;
}
/**
* Get a mock for opine Response
* @param additional any properties that should be set explicitly
* @returns a mock for opine Response
*/
export function mockResponse(additional?: Partial<Response>): Response {
//define properties and methods according to docs - http://expressjs.com/en/4x/api.html#res
const props: Partial<Response> = {
//app: {},
//headersSent: true,
status: 200,
locals: {},
};
const methods: (keyof Response)[] = [
"append",
"attachment",
"cookie",
"clearCookie",
"download",
"end",
"format",
"get",
"json",
"jsonp",
"links",
"location",
"redirect",
"render",
"send",
"sendFile",
"sendStatus",
"set",
"type",
"vary",
"setStatus"
];
//set properties
const res = {
...props,
...additional,
};
//set methods
methods.forEach((method) => {
res[method] = additional? _wrapSpy(additional[method]): _wrapSpy();
});
return res as Response;
}