-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLocalWebhook.test.js
98 lines (89 loc) · 2.88 KB
/
LocalWebhook.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const LocalWebhook = require("./LocalWebhook");
const fetch = require("node-fetch");
describe("LocalWebhook with ngrok", () => {
beforeAll(async () => {
// Start LocalWebhook server.
await LocalWebhook.startServer({ service: "ngrok", region: "eu" });
});
afterAll(async () => {
// Stop LocalWebhook server.
await LocalWebhook.stopServer();
});
test("Promise API works", async () => {
// Create Promise.
const promise = LocalWebhook.getPromise();
expect(typeof promise.then).toBe("function");
expect(typeof promise.catch).toBe("function");
expect(typeof promise.getWebhookUrl).toBe("function");
expect(promise.getWebhookUrl()).toMatch("eu.ngrok.io");
// Trigger it.
let called = 0;
promise.then(({ res }) => {
++called;
res.end();
});
await fetch(promise.getWebhookUrl());
await promise; // Not really necessary.
expect(called).toBe(1);
});
test("Observable API works", async () => {
// Create Observable.
const observable = LocalWebhook.getObservable();
expect(typeof observable.subscribe).toBe("function");
expect(observable.getWebhookUrl()).toMatch("eu.ngrok.io");
// Trigger it twice.
let called = 0;
observable.subscribe(({ res }) => {
++called;
res.end();
});
await fetch(observable.getWebhookUrl());
expect(called).toBe(1);
await fetch(observable.getWebhookUrl());
expect(called).toBe(2);
});
});
describe("LocalWebhook with localhost.run", () => {
const subdomain = Date.now().toString();
beforeAll(async () => {
// Start LocalWebhook server.
await LocalWebhook.startServer({ subdomain });
});
afterAll(async () => {
// Stop LocalWebhook server.
await LocalWebhook.stopServer();
});
test("Promise API works", async () => {
// Create Promise.
const promise = LocalWebhook.getPromise("endpoint");
expect(typeof promise.then).toBe("function");
expect(typeof promise.catch).toBe("function");
expect(typeof promise.getWebhookUrl).toBe("function");
expect(promise.getWebhookUrl()).toMatch(subdomain + ".localhost.run/endpoint");
// Trigger it.
let called = 0;
promise.then(({ res }) => {
++called;
res.end();
});
await fetch(promise.getWebhookUrl());
await promise; // Not really necessary.
expect(called).toBe(1);
});
test("Observable API works", async () => {
// Create Observable.
const observable = LocalWebhook.getObservable("some-endpoint");
expect(typeof observable.subscribe).toBe("function");
expect(observable.getWebhookUrl()).toMatch(subdomain + ".localhost.run/some-endpoint");
// Trigger it twice.
let called = 0;
observable.subscribe(({ res }) => {
++called;
res.end();
});
await fetch(observable.getWebhookUrl());
expect(called).toBe(1);
await fetch(observable.getWebhookUrl());
expect(called).toBe(2);
});
});