@tryghost/express-test 1.2.1 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Request.js +78 -10
- package/package.json +11 -11
package/lib/Request.js
CHANGED
|
@@ -1,8 +1,19 @@
|
|
|
1
|
-
const
|
|
1
|
+
const http = require('http');
|
|
2
|
+
const {Writable, Readable} = require('stream');
|
|
3
|
+
const express = require('express');
|
|
2
4
|
const {CookieAccessInfo} = require('cookiejar');
|
|
3
5
|
const {parse} = require('url');
|
|
4
6
|
const {isJSON, attachFile} = require('./utils');
|
|
5
|
-
|
|
7
|
+
|
|
8
|
+
class MockSocket extends Writable {
|
|
9
|
+
constructor() {
|
|
10
|
+
super();
|
|
11
|
+
this.remoteAddress = '127.0.0.1';
|
|
12
|
+
}
|
|
13
|
+
_write(chunk, encoding, callback) {
|
|
14
|
+
callback();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
6
17
|
|
|
7
18
|
class RequestOptions {
|
|
8
19
|
constructor({method, url, headers, body} = {}) {
|
|
@@ -70,11 +81,8 @@ class Request {
|
|
|
70
81
|
stream(readableStream) {
|
|
71
82
|
this.reqOptions.body = undefined;
|
|
72
83
|
|
|
73
|
-
//
|
|
74
|
-
//
|
|
75
|
-
// It is that stream that is eventually read by middlewares and converted as JSON, as text, as form data etc.
|
|
76
|
-
// Currently the Express middlewares (and multer for file uploads) use the pipe method and/or the event listeners, so we don't need to override other methods
|
|
77
|
-
// If we need other methods, we only need to map them to the readable stream.
|
|
84
|
+
// Override stream methods on the request to delegate to the provided readable stream.
|
|
85
|
+
// Express middlewares (and multer for file uploads) use pipe/event listeners to consume the body.
|
|
78
86
|
this.reqOptions.methodOverrides = {
|
|
79
87
|
pipe: (destination) => {
|
|
80
88
|
readableStream.pipe(destination);
|
|
@@ -91,9 +99,15 @@ class Request {
|
|
|
91
99
|
get readable() {
|
|
92
100
|
return readableStream.readable;
|
|
93
101
|
},
|
|
102
|
+
get readableEnded() {
|
|
103
|
+
return readableStream.readableEnded;
|
|
104
|
+
},
|
|
94
105
|
pause: () => {
|
|
95
106
|
readableStream.pause();
|
|
96
107
|
},
|
|
108
|
+
resume: () => {
|
|
109
|
+
readableStream.resume();
|
|
110
|
+
},
|
|
97
111
|
read: () => {
|
|
98
112
|
return readableStream.read(...arguments);
|
|
99
113
|
}
|
|
@@ -137,10 +151,64 @@ class Request {
|
|
|
137
151
|
_getReqRes() {
|
|
138
152
|
const {app, reqOptions} = this;
|
|
139
153
|
|
|
140
|
-
|
|
154
|
+
// Create proper Node.js req/res objects using built-in http module.
|
|
155
|
+
// MockSocket provides a writable stream so that res.end() properly emits 'finish'.
|
|
156
|
+
const socket = new MockSocket();
|
|
157
|
+
const hasStreamOverrides = !!this.reqOptions.methodOverrides;
|
|
158
|
+
|
|
159
|
+
// When streaming body data, the socket must appear readable so that
|
|
160
|
+
// body-parser's on-finished check doesn't skip the request as "already finished".
|
|
161
|
+
if (hasStreamOverrides) {
|
|
162
|
+
Object.defineProperty(socket, 'readable', {value: true});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const req = new http.IncomingMessage(socket);
|
|
166
|
+
req.method = reqOptions.method;
|
|
167
|
+
req.url = reqOptions.url;
|
|
168
|
+
req.headers = {};
|
|
169
|
+
for (const key of Object.keys(reqOptions.headers)) {
|
|
170
|
+
req.headers[key.toLowerCase()] = reqOptions.headers[key];
|
|
171
|
+
}
|
|
172
|
+
req.headers.host = req.headers.host || 'localhost';
|
|
173
|
+
req.body = reqOptions.body;
|
|
174
|
+
req.app = app;
|
|
175
|
+
|
|
176
|
+
// When body is pre-parsed (e.g. JSON object), mark it so body-parser skips parsing
|
|
177
|
+
if (reqOptions.body !== undefined) {
|
|
178
|
+
req._body = true;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const res = new http.ServerResponse(req);
|
|
182
|
+
res.assignSocket(socket);
|
|
183
|
+
|
|
184
|
+
// Apply Express prototypes so res.send(), res.json(), etc. are available
|
|
185
|
+
Object.setPrototypeOf(req, express.request);
|
|
186
|
+
Object.setPrototypeOf(res, express.response);
|
|
187
|
+
|
|
188
|
+
res.req = req;
|
|
189
|
+
req.res = res;
|
|
190
|
+
res.app = app;
|
|
191
|
+
|
|
192
|
+
// Track written body data for _buildResponse.
|
|
193
|
+
// Express calls res.write() then res.end() separately, so we must capture both.
|
|
194
|
+
let bodyChunks = [];
|
|
195
|
+
const originalWrite = res.write.bind(res);
|
|
196
|
+
res.write = function (chunk, encoding, cb) {
|
|
197
|
+
if (chunk !== null && chunk !== undefined) {
|
|
198
|
+
bodyChunks.push(Buffer.from(chunk, encoding));
|
|
199
|
+
}
|
|
200
|
+
return originalWrite(chunk, encoding, cb);
|
|
201
|
+
};
|
|
202
|
+
const originalEnd = res.end.bind(res);
|
|
203
|
+
res.end = function (chunk, encoding, cb) {
|
|
204
|
+
if (chunk !== null && chunk !== undefined) {
|
|
205
|
+
bodyChunks.push(Buffer.from(chunk, encoding));
|
|
206
|
+
}
|
|
207
|
+
res.body = Buffer.concat(bodyChunks);
|
|
208
|
+
return originalEnd(chunk, encoding, cb);
|
|
209
|
+
};
|
|
141
210
|
|
|
142
|
-
if (
|
|
143
|
-
// Copies all properties from original to copy, including getters and setters
|
|
211
|
+
if (hasStreamOverrides) {
|
|
144
212
|
const props = Object.keys(this.reqOptions.methodOverrides);
|
|
145
213
|
for (const prop of props) {
|
|
146
214
|
const descriptor = Object.getOwnPropertyDescriptor(this.reqOptions.methodOverrides, prop);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/express-test",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/TryGhost/framework.git",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"main": "index.js",
|
|
12
12
|
"scripts": {
|
|
13
13
|
"dev": "echo \"Implement me!\"",
|
|
14
|
-
"test": "NODE_ENV=testing
|
|
14
|
+
"test": "NODE_ENV=testing vitest run --coverage",
|
|
15
15
|
"lint": "eslint . --ext .js --cache",
|
|
16
16
|
"posttest": "yarn lint"
|
|
17
17
|
},
|
|
@@ -23,18 +23,18 @@
|
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"c8": "10.1.3",
|
|
27
26
|
"express": "5.2.1",
|
|
28
27
|
"express-session": "1.19.0",
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
|
|
28
|
+
"multer": "2.1.1",
|
|
29
|
+
"sinon": "21.0.2"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"express": "^4.0.0 || ^5.0.0"
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"@tryghost/jest-snapshot": "^
|
|
35
|
-
"cookiejar": "
|
|
36
|
-
"form-data": "
|
|
37
|
-
"mime-types": "
|
|
38
|
-
"reqresnext": "^1.7.0"
|
|
35
|
+
"@tryghost/jest-snapshot": "^2.0.1",
|
|
36
|
+
"cookiejar": "2.1.4",
|
|
37
|
+
"form-data": "4.0.5",
|
|
38
|
+
"mime-types": "3.0.2"
|
|
39
39
|
}
|
|
40
40
|
}
|