-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwebsub.test.js
More file actions
188 lines (158 loc) · 5.8 KB
/
Copy pathwebsub.test.js
File metadata and controls
188 lines (158 loc) · 5.8 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const test = require('node:test');
const assert = require('node:assert/strict');
const { createWebSubClient, readVerification } = require('./websub');
function fakeFetch(status = 202, responseBody = '') {
const calls = [];
const fn = async(url, init) => {
calls.push({ url: String(url), init: init ?? {} });
return { status, text: async() => responseBody };
};
return { fn, calls };
}
function form(init) {
return new URLSearchParams(init.body);
}
test('subscribe posts the hub.* subscribe form to /websub', async() => {
const { fn, calls } = fakeFetch();
const client = createWebSubClient({
serverUrl: 'http://hub.example:5337',
fetch: fn
});
const res = await client.subscribe({
callbackUrl: 'http://sub.example:9000/websub-callback',
topicUrl: 'https://feed.example/rss'
});
assert.equal(calls[0].url, 'http://hub.example:5337/websub');
assert.equal(calls[0].init.method, 'POST');
assert.equal(
calls[0].init.headers['Content-Type'],
'application/x-www-form-urlencoded'
);
const body = form(calls[0].init);
assert.equal(body.get('hub.mode'), 'subscribe');
assert.equal(
body.get('hub.callback'),
'http://sub.example:9000/websub-callback'
);
assert.equal(body.get('hub.topic'), 'https://feed.example/rss');
assert.deepEqual(res, { status: 202, body: '' });
});
test('subscribe carries hub.lease_seconds and hub.secret when supplied', async() => {
const { fn, calls } = fakeFetch();
const client = createWebSubClient({
serverUrl: 'http://hub.example:5337',
fetch: fn
});
await client.subscribe({
callbackUrl: 'http://sub.example:9000/websub-callback',
topicUrl: 'https://feed.example/rss',
leaseSeconds: 3600,
secret: 's3cr3t'
});
const body = form(calls[0].init);
assert.equal(body.get('hub.lease_seconds'), '3600');
assert.equal(body.get('hub.secret'), 's3cr3t');
});
test('subscribe omits hub.lease_seconds and hub.secret when not supplied', async() => {
const { fn, calls } = fakeFetch();
const client = createWebSubClient({
serverUrl: 'http://hub.example:5337',
fetch: fn
});
await client.subscribe({
callbackUrl: 'http://sub.example:9000/websub-callback',
topicUrl: 'https://feed.example/rss'
});
const body = form(calls[0].init);
assert.equal(body.has('hub.lease_seconds'), false);
assert.equal(body.has('hub.secret'), false);
});
test('publish posts hub.mode=publish with the topic as hub.url', async() => {
const { fn, calls } = fakeFetch();
const client = createWebSubClient({
serverUrl: 'http://hub.example:5337',
fetch: fn
});
const res = await client.publish({ topicUrl: 'https://feed.example/rss' });
assert.equal(calls[0].url, 'http://hub.example:5337/websub');
assert.equal(calls[0].init.method, 'POST');
const body = form(calls[0].init);
assert.equal(body.get('hub.mode'), 'publish');
assert.equal(body.get('hub.url'), 'https://feed.example/rss');
assert.deepEqual(res, { status: 202, body: '' });
});
test('unsubscribe posts hub.mode=unsubscribe with callback and topic', async() => {
const { fn, calls } = fakeFetch();
const client = createWebSubClient({
serverUrl: 'http://hub.example:5337',
fetch: fn
});
await client.unsubscribe({
callbackUrl: 'http://sub.example:9000/websub-callback',
topicUrl: 'https://feed.example/rss'
});
const body = form(calls[0].init);
assert.equal(body.get('hub.mode'), 'unsubscribe');
assert.equal(
body.get('hub.callback'),
'http://sub.example:9000/websub-callback'
);
assert.equal(body.get('hub.topic'), 'https://feed.example/rss');
});
test('targets a configurable hub path', async() => {
const { fn, calls } = fakeFetch();
const client = createWebSubClient({
serverUrl: 'http://hub.example:5337',
path: '/hub',
fetch: fn
});
await client.publish({ topicUrl: 'https://feed.example/rss' });
assert.equal(calls[0].url, 'http://hub.example:5337/hub');
});
test('strips a trailing slash from the server URL', async() => {
const { fn, calls } = fakeFetch();
const client = createWebSubClient({
serverUrl: 'http://hub.example:5337/',
fetch: fn
});
await client.publish({ topicUrl: 'https://feed.example/rss' });
assert.equal(calls[0].url, 'http://hub.example:5337/websub');
});
test('defaults to the global fetch when none is injected', () => {
const client = createWebSubClient({
serverUrl: 'http://hub.example:5337'
});
assert.equal(typeof client.subscribe, 'function');
assert.equal(typeof client.unsubscribe, 'function');
assert.equal(typeof client.publish, 'function');
});
test('readVerification parses a subscribe verification GET', () => {
const parsed = readVerification({
'hub.mode': 'subscribe',
'hub.topic': 'https://feed.example/rss',
'hub.challenge': 'abc123',
'hub.lease_seconds': '3600'
});
assert.deepEqual(parsed, {
mode: 'subscribe',
topic: 'https://feed.example/rss',
challenge: 'abc123',
leaseSeconds: 3600
});
});
test('readVerification omits leaseSeconds for an unsubscribe verification', () => {
const parsed = readVerification({
'hub.mode': 'unsubscribe',
'hub.topic': 'https://feed.example/rss',
'hub.challenge': 'xyz789'
});
assert.deepEqual(parsed, {
mode: 'unsubscribe',
topic: 'https://feed.example/rss',
challenge: 'xyz789'
});
});
test('readVerification returns null when the query carries no challenge', () => {
assert.equal(readVerification({ 'hub.mode': 'subscribe' }), null);
assert.equal(readVerification({}), null);
});