/
members.ts
133 lines (126 loc) · 3.58 KB
/
members.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
// deno-lint-ignore-file
import type { Drash } from "../mod.ts";
import { BufReader, ServerRequest } from "../deps.ts";
import { Rhum } from "./deps.ts";
const decoder = new TextDecoder("utf-8");
/**
* Get a mocked request object.
*
* The reason there's a lot of `any`'s in this method is because
* it's our own testing method, and we cannot compile if we use
* the ServerRequest types. It isn't a problem though as
* it's purely for testing
*
* @param string url
* @param string method
* @param any
* {
* headers: { [key: string]: string }
* }
*
* @returns A server request object (ServerRequest)
*/
// deno-lint-ignore no-explicit-any
function mockRequest(url = "/", method = "get", options?: any): any {
// deno-lint-ignore no-explicit-any
let request: any = new ServerRequest(); // Type: ServerRequest, but can't type it so as we modify the request object (thus deferring from the original type)
request.url = url;
request.method = method;
request.headers = new Headers();
if (options) {
if (options.headers) {
for (let key in options.headers) {
request.headers.set(key, options.headers[key]);
}
}
if (options.body) {
try {
request.headers.set("Content-Length", options.body.length.toString());
} catch (err) {
// ... you *shall* pass
}
request.r = new BufReader(options.body);
}
}
//
// Stub `respond()` so we don't run into the following error:
//
// TypeError: Cannot read property 'write' of undefined
//
request.respond = function respond(output: Drash.Interfaces.ResponseOutput) {
output.send = function () {
if (
output.status === 301 ||
output.status === 302
) {
return output;
}
};
return output;
};
return request;
}
interface IMakeRequestOptions {
body?: any;
headers?: any;
credentials?: any;
}
// deno-lint-ignore no-explicit-any
function assertResponseJsonEquals(actual: any, expected: any) { // `any` because it's for testing, and the params could literally be anything
let response;
try {
response = Rhum.asserts.assertEquals(JSON.parse(actual), expected);
} catch (error) {
response = Rhum.asserts.assertEquals(actual, expected);
}
return response;
}
const makeRequest = {
get(url: string, options: IMakeRequestOptions = {}) {
options = Object.assign(options, {
method: "GET",
});
options.body = JSON.stringify(options.body);
return fetch(url, options);
},
post(url: string, options: IMakeRequestOptions = {}) {
options = Object.assign(options, {
method: "POST",
});
options.body = JSON.stringify(options.body);
return fetch(url, options);
},
put(url: string, options: IMakeRequestOptions = {}) {
options = Object.assign(options, {
method: "PUT",
});
options.body = JSON.stringify(options.body);
return fetch(url, options);
},
delete(url: string, options: IMakeRequestOptions = {}) {
options = Object.assign(options, {
method: "DELETE",
});
options.body = JSON.stringify(options.body);
return fetch(url, options);
},
patch(url: string, options: IMakeRequestOptions = {}) {
options = Object.assign(options, {
method: "PATCH",
});
options.body = JSON.stringify(options.body);
return fetch(url, options);
},
};
export default {
BufReader,
ServerRequest,
assertResponseJsonEquals,
currentTestSuite: "",
decoder,
fetch: makeRequest,
mockRequest,
responseBody: function (response: Drash.Interfaces.ResponseOutput) {
return decoder.decode(response.body as ArrayBuffer);
},
};