@tryghost/webhook-mock-receiver 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 +46 -0
- package/index.js +1 -0
- package/lib/webhook-mock-receiver.js +58 -0
- package/package.json +30 -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,46 @@
|
|
|
1
|
+
# Webhook Mock Receiver
|
|
2
|
+
|
|
3
|
+
Webhook request testing.
|
|
4
|
+
|
|
5
|
+
It's a bare minimum implementation to be able to check webhook request payload. Few things that need to be done in the future:
|
|
6
|
+
- cleaner module API
|
|
7
|
+
- header snapshot recording/testing
|
|
8
|
+
- better request resolution waiting mechanism (see @TODO list)
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
`npm install @tryghost/webhook-mock-receiver --save`
|
|
13
|
+
|
|
14
|
+
or
|
|
15
|
+
|
|
16
|
+
`yarn add @tryghost/webhook-mock-receiver`
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Develop
|
|
23
|
+
|
|
24
|
+
This is a mono repository, managed with [lerna](https://lernajs.io/).
|
|
25
|
+
|
|
26
|
+
Follow the instructions for the top-level repo.
|
|
27
|
+
1. `git clone` this repo & `cd` into it as usual
|
|
28
|
+
2. Run `yarn` to install top-level dependencies.
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## Run
|
|
32
|
+
|
|
33
|
+
- `yarn dev`
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
## Test
|
|
37
|
+
|
|
38
|
+
- `yarn lint` run just eslint
|
|
39
|
+
- `yarn test` run lint and tests
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# Copyright & License
|
|
45
|
+
|
|
46
|
+
Copyright (c) 2013-2022 Ghost Foundation - Released under the [MIT license](LICENSE).
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib/webhook-mock-receiver');
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const util = require('util');
|
|
2
|
+
const {URL} = require('url');
|
|
3
|
+
const nock = require('nock');
|
|
4
|
+
const setTimeoutPromise = util.promisify(setTimeout);
|
|
5
|
+
|
|
6
|
+
class WebhookMockReceiver {
|
|
7
|
+
constructor({snapshotManager}) {
|
|
8
|
+
this.bodyResponse;
|
|
9
|
+
this.receiver;
|
|
10
|
+
this.snapshotManager = snapshotManager;
|
|
11
|
+
this.recordBodyResponse = this.recordBodyResponse.bind(this);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
recordBodyResponse(body) {
|
|
15
|
+
this.bodyResponse = {body};
|
|
16
|
+
|
|
17
|
+
// let the nock continue with the response
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
* @param {String} url endpoint URL to be mocked
|
|
24
|
+
* @returns WebhookMockReceiver
|
|
25
|
+
*/
|
|
26
|
+
mock(url) {
|
|
27
|
+
const parsedURL = new URL(url);
|
|
28
|
+
|
|
29
|
+
this.receiver = nock(parsedURL.origin)
|
|
30
|
+
.post(parsedURL.pathname, this.recordBodyResponse)
|
|
31
|
+
.reply(200, {status: 'OK'});
|
|
32
|
+
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
reset() {
|
|
37
|
+
nock.cleanAll();
|
|
38
|
+
this.receiver = undefined;
|
|
39
|
+
this.bodyResponse = undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async matchBodySnapshot(properties = {}) {
|
|
43
|
+
// @TODO: figure out a better waiting mechanism here, don't allow it to hang forever
|
|
44
|
+
while (!this.receiver.isDone()) {
|
|
45
|
+
await setTimeoutPromise(10);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let assertion = {
|
|
49
|
+
properties: properties,
|
|
50
|
+
field: 'body',
|
|
51
|
+
type: 'body'
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
this.snapshotManager.assertSnapshot(this.bodyResponse, assertion);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = WebhookMockReceiver;
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tryghost/webhook-mock-receiver",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"repository": "https://github.com/TryGhost/framework/tree/main/packages/webhook-mock-receiver",
|
|
5
|
+
"author": "Ghost Foundation",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"dev": "echo \"Implement me!\"",
|
|
10
|
+
"test": "NODE_ENV=testing c8 --all --check-coverage --reporter text --reporter cobertura mocha './test/**/*.test.js'",
|
|
11
|
+
"lint:code": "eslint *.js lib/ --ext .js --cache",
|
|
12
|
+
"lint": "yarn lint:code && yarn lint:test",
|
|
13
|
+
"lint:test": "eslint -c test/.eslintrc.js test/ --ext .js --cache",
|
|
14
|
+
"posttest": "yarn lint"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"index.js",
|
|
18
|
+
"lib"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"c8": "7.11.3",
|
|
25
|
+
"got": "9.6.0",
|
|
26
|
+
"mocha": "10.0.0",
|
|
27
|
+
"sinon": "14.0.0"
|
|
28
|
+
},
|
|
29
|
+
"gitHead": "0af921ce800c79f89685307914bd6d704302c043"
|
|
30
|
+
}
|