@tryghost/webhook-mock-receiver 0.1.2 → 0.2.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/README.md +1 -6
- package/lib/webhook-mock-receiver.js +40 -20
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
# Webhook Mock Receiver
|
|
2
2
|
|
|
3
|
-
|
|
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)
|
|
3
|
+
Up to date documentation about the usage of the module available in [End-to-end Testing Codex](https://ghost.notion.site/End-to-end-Testing-6a2ef073b1754b18aff42e24a632a007#827471b6314640758460b9c1e22b34c4).
|
|
9
4
|
|
|
10
5
|
## Install
|
|
11
6
|
|
|
@@ -1,22 +1,25 @@
|
|
|
1
1
|
const {AssertionError} = require('assert');
|
|
2
|
-
const util = require('util');
|
|
3
2
|
const {URL} = require('url');
|
|
4
3
|
const nock = require('nock');
|
|
5
|
-
const
|
|
4
|
+
const pWaitFor = require('p-wait-for');
|
|
6
5
|
|
|
7
6
|
class WebhookMockReceiver {
|
|
8
7
|
constructor({snapshotManager}) {
|
|
9
|
-
this.
|
|
10
|
-
this.
|
|
8
|
+
this.body;
|
|
9
|
+
this.headers;
|
|
10
|
+
this._receiver;
|
|
11
11
|
this.snapshotManager = snapshotManager;
|
|
12
|
-
this.
|
|
12
|
+
this.recordRequest = this.recordRequest.bind(this);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
this.
|
|
15
|
+
recordRequest(body, options) {
|
|
16
|
+
this.body = {body};
|
|
17
|
+
this.headers = {headers: options.headers};
|
|
18
|
+
}
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
async receivedRequest() {
|
|
21
|
+
// @NOTE: figure out a better waiting mechanism here, don't allow it to hang forever
|
|
22
|
+
await pWaitFor(() => this._receiver.isDone());
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
/**
|
|
@@ -26,9 +29,14 @@ class WebhookMockReceiver {
|
|
|
26
29
|
*/
|
|
27
30
|
mock(url) {
|
|
28
31
|
const parsedURL = new URL(url);
|
|
32
|
+
const recordRequest = this.recordRequest;
|
|
29
33
|
|
|
30
|
-
this.
|
|
31
|
-
.post(parsedURL.pathname,
|
|
34
|
+
this._receiver = nock(parsedURL.origin)
|
|
35
|
+
.post(parsedURL.pathname, function (body) {
|
|
36
|
+
recordRequest(body, this);
|
|
37
|
+
// let the nock continue with the response
|
|
38
|
+
return true;
|
|
39
|
+
})
|
|
32
40
|
.reply(200, {status: 'OK'});
|
|
33
41
|
|
|
34
42
|
return this;
|
|
@@ -36,16 +44,12 @@ class WebhookMockReceiver {
|
|
|
36
44
|
|
|
37
45
|
reset() {
|
|
38
46
|
nock.cleanAll();
|
|
39
|
-
this.
|
|
40
|
-
this.
|
|
47
|
+
this._receiver = undefined;
|
|
48
|
+
this.body = undefined;
|
|
49
|
+
this.headers = undefined;
|
|
41
50
|
}
|
|
42
51
|
|
|
43
|
-
|
|
44
|
-
// @TODO: figure out a better waiting mechanism here, don't allow it to hang forever
|
|
45
|
-
while (!this.receiver.isDone()) {
|
|
46
|
-
await setTimeoutPromise(10);
|
|
47
|
-
}
|
|
48
|
-
|
|
52
|
+
matchBodySnapshot(properties = {}) {
|
|
49
53
|
const error = new AssertionError({});
|
|
50
54
|
let assertion = {
|
|
51
55
|
properties: properties,
|
|
@@ -54,7 +58,23 @@ class WebhookMockReceiver {
|
|
|
54
58
|
error
|
|
55
59
|
};
|
|
56
60
|
|
|
57
|
-
this.snapshotManager.assertSnapshot(this.
|
|
61
|
+
this.snapshotManager.assertSnapshot(this.body, assertion);
|
|
62
|
+
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
matchHeaderSnapshot(properties = {}) {
|
|
67
|
+
const error = new AssertionError({});
|
|
68
|
+
let assertion = {
|
|
69
|
+
properties: properties,
|
|
70
|
+
field: 'headers',
|
|
71
|
+
type: 'header',
|
|
72
|
+
error
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
this.snapshotManager.assertSnapshot(this.headers, assertion);
|
|
76
|
+
|
|
77
|
+
return this;
|
|
58
78
|
}
|
|
59
79
|
}
|
|
60
80
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/webhook-mock-receiver",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"repository": "https://github.com/TryGhost/framework/tree/main/packages/webhook-mock-receiver",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,7 +24,10 @@
|
|
|
24
24
|
"c8": "7.12.0",
|
|
25
25
|
"got": "9.6.0",
|
|
26
26
|
"mocha": "10.0.0",
|
|
27
|
-
"sinon": "14.0.
|
|
27
|
+
"sinon": "14.0.1"
|
|
28
28
|
},
|
|
29
|
-
"
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"p-wait-for": "3.2.0"
|
|
31
|
+
},
|
|
32
|
+
"gitHead": "73748c86fd0a1416792974234dfe88e3f5a62f25"
|
|
30
33
|
}
|