@tryghost/express-test 0.1.0
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/LICENSE +21 -0
- package/README.md +67 -0
- package/index.js +1 -0
- package/lib/agent.js +49 -0
- package/lib/expect-request.js +115 -0
- package/lib/request.js +132 -0
- package/lib/utils.js +5 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013-2022 Ghost Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Express Test
|
|
2
|
+
|
|
3
|
+
Rapid express testing without HTTP
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
`npm install @tryghost/express-test --save`
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
`yarn add @tryghost/express-test`
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
const TestAgent = require('@tryghost/express-test');
|
|
18
|
+
const agent = new TestAgent(app, {defaults});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
For the most up-to-date and clear usage info, there's a live working example of this library in action inside `tests/example-app.test.js`.
|
|
22
|
+
|
|
23
|
+
An instantiated agent can make HTTP-like calls, with a supertest-like chained API to set headers & body and to check status, headers and anything else.
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
const agent = new TestAgent(app)
|
|
28
|
+
|
|
29
|
+
return await agent
|
|
30
|
+
.post('/check/')
|
|
31
|
+
.header('x-check', true) // set a header
|
|
32
|
+
.body({foo: 'bar'}) // Set the body of the POST
|
|
33
|
+
.expectStatus(200) // Assert that the response status is 200
|
|
34
|
+
.expectHeader('x-checked', 'false') // Assert a header is set on the response
|
|
35
|
+
.expect(({body}) => {
|
|
36
|
+
// Make any further assertions
|
|
37
|
+
assert.deepEqual(body, {foo: 'bar'});
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
This is an initial version for review. More docs coming if it works :)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
## Develop
|
|
44
|
+
|
|
45
|
+
This is a mono repository, managed with [lerna](https://lernajs.io/).
|
|
46
|
+
|
|
47
|
+
Follow the instructions for the top-level repo.
|
|
48
|
+
1. `git clone` this repo & `cd` into it as usual
|
|
49
|
+
2. Run `yarn` to install top-level dependencies.
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
## Run
|
|
53
|
+
|
|
54
|
+
- `yarn dev`
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
## Test
|
|
58
|
+
|
|
59
|
+
- `yarn lint` run just eslint
|
|
60
|
+
- `yarn test` run lint and tests
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# Copyright & License
|
|
66
|
+
|
|
67
|
+
Copyright (c) 2013-2022 Ghost Foundation - Released under the [MIT license](LICENSE).
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib/agent');
|
package/lib/agent.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const {CookieJar} = require('cookiejar');
|
|
2
|
+
const ExpectRequest = require('./expect-request');
|
|
3
|
+
const {RequestOptions} = require('./request');
|
|
4
|
+
|
|
5
|
+
class Agent {
|
|
6
|
+
constructor(app, defaults = {}) {
|
|
7
|
+
this.app = app;
|
|
8
|
+
this.defaults = defaults;
|
|
9
|
+
|
|
10
|
+
this.jar = new CookieJar();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
_makeUrl(url) {
|
|
14
|
+
if (this.defaults.baseUrl) {
|
|
15
|
+
url = `/${this.defaults.baseUrl}/${url}`.replace(/(^|[^:])\/\/+/g, '$1/');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return url;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
_mergeOptions(method, url, options = {}) {
|
|
22
|
+
// It doesn't make sense to call this method without these properties
|
|
23
|
+
if (!method) {
|
|
24
|
+
throw new Error('_mergeOptions cannot be called without a method'); /* eslint-disable-line no-restricted-syntax */
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!url) {
|
|
28
|
+
throw new Error('_mergeOptions cannot be called without a url'); /* eslint-disable-line no-restricted-syntax */
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return new RequestOptions({
|
|
32
|
+
method,
|
|
33
|
+
url: this._makeUrl(url),
|
|
34
|
+
headers: Object.assign({}, this.defaults.headers, options.headers),
|
|
35
|
+
body: Object.assign({}, this.defaults.body, options.body)
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
['get', 'post', 'put', 'del'].forEach((method) => {
|
|
41
|
+
Agent.prototype[method] = function (url, options) {
|
|
42
|
+
if (!url) {
|
|
43
|
+
throw new Error('Cannot make a request without supplying a url'); /* eslint-disable-line no-restricted-syntax */
|
|
44
|
+
}
|
|
45
|
+
return new ExpectRequest(this.app, this.jar, this._mergeOptions(method.toUpperCase(), url, options));
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
module.exports = Agent;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const {Request, RequestOptions} = require('./request');
|
|
3
|
+
|
|
4
|
+
class ExpectRequest extends Request {
|
|
5
|
+
constructor(...args) {
|
|
6
|
+
super(...args);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
expect(callback) {
|
|
10
|
+
const wrapperFn = (response, assertion) => {
|
|
11
|
+
try {
|
|
12
|
+
callback(response);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
error.stack = assertion.error.stack.replace(assertion.error.message, error.message);
|
|
15
|
+
throw error;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const assertion = {
|
|
20
|
+
fn: wrapperFn
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
this._addAssertion(assertion);
|
|
24
|
+
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
expectStatus(expected) {
|
|
29
|
+
const assertion = {
|
|
30
|
+
fn: this._assertStatus,
|
|
31
|
+
expected
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
this._addAssertion(assertion);
|
|
35
|
+
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
expectHeader(expectedField, expectedValue) {
|
|
40
|
+
const assertion = {
|
|
41
|
+
fn: this._assertHeader,
|
|
42
|
+
expectedField: expectedField.toLowerCase(),
|
|
43
|
+
expectedValue
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
this._addAssertion(assertion);
|
|
47
|
+
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
finalize(callback) {
|
|
51
|
+
super.finalize((error, response) => {
|
|
52
|
+
if (error) {
|
|
53
|
+
return callback(error);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
if (this.assertions) {
|
|
58
|
+
this._assertAll(response);
|
|
59
|
+
}
|
|
60
|
+
return callback(null, response);
|
|
61
|
+
} catch (_error) {
|
|
62
|
+
return callback(_error);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
_assertAll(response) {
|
|
68
|
+
for (const assertion of this.assertions) {
|
|
69
|
+
assertion.fn(response, assertion);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
_addAssertion(assertion) {
|
|
74
|
+
let error = new assert.AssertionError({
|
|
75
|
+
message: 'Unexpected assertion error',
|
|
76
|
+
expected: assertion.expected,
|
|
77
|
+
stackStartFn: this._addAssertion
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
error.contextString = this.reqOptions.toString();
|
|
81
|
+
assertion.error = error;
|
|
82
|
+
|
|
83
|
+
this.assertions = this.assertions || [];
|
|
84
|
+
this.assertions.push(assertion);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
_assertStatus(response, assertion) {
|
|
88
|
+
const {error} = assertion;
|
|
89
|
+
|
|
90
|
+
error.message = `Expected statusCode ${assertion.expected}, got statusCode ${response.statusCode} ${error.contextString}`;
|
|
91
|
+
error.actual = response.statusCode;
|
|
92
|
+
|
|
93
|
+
assert.equal(response.statusCode, assertion.expected, error);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
_assertHeader(response, assertion) {
|
|
97
|
+
const {expectedField, expectedValue, error} = assertion;
|
|
98
|
+
const actual = response.headers[expectedField];
|
|
99
|
+
const expectedHeaderString = `${expectedField}: ${expectedValue}`;
|
|
100
|
+
const actualHeaderString = `${expectedField}: ${actual}`;
|
|
101
|
+
|
|
102
|
+
error.expected = expectedHeaderString;
|
|
103
|
+
error.actual = actualHeaderString;
|
|
104
|
+
|
|
105
|
+
error.message = `Expected header "${expectedHeaderString}" to exist but got ${JSON.stringify(response.headers)} ${error.contextString}`;
|
|
106
|
+
assert.notStrictEqual(actual, undefined, error);
|
|
107
|
+
|
|
108
|
+
error.message = `Expected header "${expectedHeaderString}", got ${actualHeaderString} ${error.contextString}`;
|
|
109
|
+
assert.equal(expectedHeaderString, actualHeaderString, error);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = ExpectRequest;
|
|
114
|
+
module.exports.ExpectRequest = ExpectRequest;
|
|
115
|
+
module.exports.RequestOptions = RequestOptions;
|
package/lib/request.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
const {default: reqresnext} = require('reqresnext');
|
|
2
|
+
const {CookieAccessInfo} = require('cookiejar');
|
|
3
|
+
const {parse} = require('url');
|
|
4
|
+
const {isJSON} = require('./utils');
|
|
5
|
+
|
|
6
|
+
class RequestOptions {
|
|
7
|
+
constructor({method, url, headers, body} = {}) {
|
|
8
|
+
this.method = method || 'GET';
|
|
9
|
+
this.url = url || '/';
|
|
10
|
+
this.headers = headers || {};
|
|
11
|
+
this.body = body;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
toString() {
|
|
15
|
+
return `${this.method} request on ${this.url}`;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
class Request {
|
|
20
|
+
constructor(app, cookieJar, reqOptions) {
|
|
21
|
+
this.app = app;
|
|
22
|
+
this.reqOptions = reqOptions instanceof RequestOptions ? reqOptions : new RequestOptions(reqOptions);
|
|
23
|
+
this.cookieJar = cookieJar;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
body(body) {
|
|
27
|
+
this.reqOptions.body = body;
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
header(name, value) {
|
|
32
|
+
this.reqOptions.headers[name] = value;
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
then(resolve, reject) {
|
|
37
|
+
this.finalize((error, response) => {
|
|
38
|
+
if (error) {
|
|
39
|
+
return reject(error);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return resolve(response);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/*
|
|
47
|
+
* This method exists to make it easy to extend this class with ExpectRequest
|
|
48
|
+
* We use callbacks so we don't need to introduce async/await here which may make things
|
|
49
|
+
* Difficult and/or confusing with the thennable
|
|
50
|
+
*/
|
|
51
|
+
finalize(callback) {
|
|
52
|
+
this._doRequest((error, response) => {
|
|
53
|
+
if (error) {
|
|
54
|
+
return callback(error);
|
|
55
|
+
}
|
|
56
|
+
return callback(null, response);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
_getReqRes() {
|
|
61
|
+
const {app, reqOptions} = this;
|
|
62
|
+
|
|
63
|
+
const {req, res} = reqresnext(Object.assign({}, reqOptions, {app}), {app});
|
|
64
|
+
|
|
65
|
+
// This is needed to make error handling work
|
|
66
|
+
// @TODO: submit this upstream in reqresnext
|
|
67
|
+
req.socket.destroy = () => { };
|
|
68
|
+
return {req, res};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
_buildResponse(res) {
|
|
72
|
+
const statusCode = res.statusCode;
|
|
73
|
+
const headers = Object.assign({}, res.getHeaders());
|
|
74
|
+
const text = res.body ? res.body.toString('utf8') : undefined;
|
|
75
|
+
let body = {};
|
|
76
|
+
|
|
77
|
+
if (isJSON(res.getHeader('Content-Type'))) {
|
|
78
|
+
body = text && JSON.parse(text);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {statusCode, headers, text, body, response: res};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
_doRequest(callback) {
|
|
85
|
+
try {
|
|
86
|
+
const {req, res} = this._getReqRes();
|
|
87
|
+
|
|
88
|
+
this._restoreCookies(req);
|
|
89
|
+
|
|
90
|
+
res.on('finish', () => {
|
|
91
|
+
const response = this._buildResponse(res);
|
|
92
|
+
|
|
93
|
+
this._saveCookies(res);
|
|
94
|
+
|
|
95
|
+
callback(null, response);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
this.app(req, res);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
callback(error);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
_getCookies(req) {
|
|
105
|
+
const url = parse(req.url);
|
|
106
|
+
|
|
107
|
+
const access = new CookieAccessInfo(
|
|
108
|
+
url.hostname,
|
|
109
|
+
url.pathname,
|
|
110
|
+
url.protocol === 'https:'
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
return this.cookieJar.getCookies(access).toValueString();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
_restoreCookies(req) {
|
|
117
|
+
req.headers.cookie = this._getCookies(req);
|
|
118
|
+
return req;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
_saveCookies(res) {
|
|
122
|
+
const cookies = res.getHeader('set-cookie');
|
|
123
|
+
|
|
124
|
+
if (cookies) {
|
|
125
|
+
this.cookieJar.setCookies(cookies);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports = Request;
|
|
131
|
+
module.exports.Request = Request;
|
|
132
|
+
module.exports.RequestOptions = RequestOptions;
|
package/lib/utils.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tryghost/express-test",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"repository": "https://github.com/TryGhost/framework/tree/main/packages/express-test",
|
|
5
|
+
"author": "Ghost Foundation",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"dev": "echo \"Implement me!\"",
|
|
10
|
+
"test": "NODE_ENV=testing c8 mocha './test/**/*.test.js'",
|
|
11
|
+
"lint": "eslint . --ext .js --cache",
|
|
12
|
+
"posttest": "yarn lint"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"lib"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"c8": "7.11.0",
|
|
23
|
+
"express": "^4.17.2",
|
|
24
|
+
"express-session": "^1.17.2",
|
|
25
|
+
"mocha": "9.2.0",
|
|
26
|
+
"sinon": "13.0.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"cookiejar": "^2.1.3",
|
|
30
|
+
"reqresnext": "^1.6.6"
|
|
31
|
+
},
|
|
32
|
+
"gitHead": "45777c4b5a010293f529f88aea19a81a896ba266"
|
|
33
|
+
}
|