forked from jamesshore/testing-without-mocks-complex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server_request.js
More file actions
140 lines (117 loc) · 3.5 KB
/
http_server_request.js
File metadata and controls
140 lines (117 loc) · 3.5 KB
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
// Copyright Titanium I.T. LLC.
import * as ensure from "util/ensure.js";
import EventEmitter from "node:events";
/** A request from a client to the HTTP server. */
export class HttpServerRequest {
/**
* Factory method. Wraps a Node.js HTTP request.
* @param nodeRequest the Node.js HTTP request
* @returns {HttpServerRequest} the wrapped request
*/
static create(nodeRequest) {
ensure.signature(arguments, [ Object ]);
return new HttpServerRequest(nodeRequest);
}
/**
* Factory method. Simulates an HTTP request.
* @param [options] details of the simulated HTTP request
* @param [options.url] path and optional querystring
* @param [options.method] method (GET, POST, etc.)
* @param [options.headers] headers
* @param [options.body] body
* @returns {HttpServerRequest} the simulated request
*/
static createNull(options) {
// signature checking in StubbedNodeRequest
return new HttpServerRequest(new StubbedNodeRequest(options));
}
/** @deprecated Use a factory method instead. */
constructor(nodeRequest) {
ensure.signature(arguments, [ Object ]);
this._request = nodeRequest;
}
/**
* @returns {string} URL path (excluding query)
*/
get urlPathname() {
const url = new URL(this._request.url, "http://unused.placeholder.host");
return decodeURIComponent(url.pathname);
}
/**
* @returns {string} method (GET, POST, etc.)
*/
get method() {
return this._request.method;
}
/**
* @returns {object} headers
*/
get headers() {
return { ...this._request.headers };
}
/**
* Determine whether the request matches a particular content-type header.
* @param expectedMediaType the media type to match (e.g., "application/json")
* @returns {boolean} true if the content-type header matches the media type
*/
hasContentType(expectedMediaType) {
ensure.signature(arguments, [ String ]);
const contentType = this.headers["content-type"];
if (contentType === undefined) return false;
const [ mediaType, ignoredParameters ] = contentType.split(";");
return mediaType.trim().toLowerCase() === expectedMediaType.trim().toLowerCase();
}
/**
* Read the request body. This may only be done once.
* @returns {Promise<string>} the body
*/
async readBodyAsync() {
return await new Promise((resolve, reject) => {
ensure.signature(arguments, []);
if (this._request.readableEnded) return reject(new Error("Can't read request body because it's already been read"));
let body = "";
this._request.on("error", reject); // this event is not tested
this._request.on("data", (chunk) => {
body += chunk;
});
this._request.on("end", () => {
resolve(body);
});
});
}
}
class StubbedNodeRequest extends EventEmitter {
constructor({
url = "/null-request-url",
method = "GET",
headers = {},
body = "",
} = {}) {
ensure.signature(arguments, [[ undefined, {
url: [ undefined, String ],
method: [ undefined, String ],
headers: [ undefined, Object ],
body: [ undefined, String ],
}]]);
super();
this.url = url;
this.method = method.toUpperCase();
this.headers = normalizeHeaders(headers);
this._body = body;
this.readableEnded = false;
}
on(event, fn) {
super.on(event, fn);
if (event === "end") {
setImmediate(() => {
this.emit("data", this._body);
this.emit("end");
this.readableEnded = true;
});
}
}
}
function normalizeHeaders(headers) {
const normalizedEntries = Object.entries(headers).map(([ name, value ]) => [ name.toLowerCase(), value ]);
return Object.fromEntries(normalizedEntries);
}