@tryghost/webhook-mock-receiver 2.3.17 → 2.3.18
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/WebhookMockReceiver.js +15 -2
- package/package.json +1 -1
|
@@ -6,6 +6,9 @@ const pWaitFor = require('p-wait-for').default;
|
|
|
6
6
|
class WebhookMockReceiver {
|
|
7
7
|
constructor({ snapshotManager }) {
|
|
8
8
|
this.snapshotManager = snapshotManager;
|
|
9
|
+
// Bumped by mock() and reset() so reply callbacks from a superseded
|
|
10
|
+
// mock can't record into the current one
|
|
11
|
+
this._mockGeneration = 0;
|
|
9
12
|
this.recordRequest = this.recordRequest.bind(this);
|
|
10
13
|
}
|
|
11
14
|
|
|
@@ -16,7 +19,9 @@ class WebhookMockReceiver {
|
|
|
16
19
|
|
|
17
20
|
async receivedRequest() {
|
|
18
21
|
// @NOTE: figure out a better waiting mechanism here, don't allow it to hang forever
|
|
19
|
-
|
|
22
|
+
// Nock marks the interceptor done when the request matches, before the
|
|
23
|
+
// async reply callback records it, so wait for both
|
|
24
|
+
await pWaitFor(() => this._receiver.isDone() && this.headers !== undefined);
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
/**
|
|
@@ -26,7 +31,14 @@ class WebhookMockReceiver {
|
|
|
26
31
|
*/
|
|
27
32
|
mock(url) {
|
|
28
33
|
const parsedURL = new URL(url);
|
|
29
|
-
const
|
|
34
|
+
const mockGeneration = ++this._mockGeneration;
|
|
35
|
+
const recordRequest = (...args) => {
|
|
36
|
+
if (mockGeneration === this._mockGeneration) {
|
|
37
|
+
this.recordRequest(...args);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
this.body = undefined;
|
|
41
|
+
this.headers = undefined;
|
|
30
42
|
|
|
31
43
|
this._receiver = nock(parsedURL.origin)
|
|
32
44
|
.post(parsedURL.pathname)
|
|
@@ -62,6 +74,7 @@ class WebhookMockReceiver {
|
|
|
62
74
|
|
|
63
75
|
reset() {
|
|
64
76
|
nock.cleanAll();
|
|
77
|
+
this._mockGeneration += 1;
|
|
65
78
|
this._receiver = undefined;
|
|
66
79
|
this.body = undefined;
|
|
67
80
|
this.headers = undefined;
|