forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.js
More file actions
67 lines (59 loc) · 2.66 KB
/
webhooks.js
File metadata and controls
67 lines (59 loc) · 2.66 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
import { get } from '../helpers/e2etest.js'
import {
SURROGATE_ENUMS,
makeLanguageSurrogateKey,
} from '../../middleware/set-fastly-surrogate-key.js'
import { describe, expect } from '@jest/globals'
describe('webhooks v1 middleware', () => {
test('basic get webhook', async () => {
const sp = new URLSearchParams()
// Based on live data which isn't ideal but it should rarely change at least.
// Just check that we find the webhook and that the result has the `category`
// field which all webhook types should have.
sp.set('category', 'branch_protection_rule')
sp.set('version', 'free-pro-team@latest')
const res = await get('/api/webhooks/v1?' + sp)
expect(res.statusCode).toBe(200)
const results = JSON.parse(res.text)
const actionTypes = Object.keys(results)
expect(actionTypes.length).toBeGreaterThan(2)
expect(Object.keys(results[actionTypes[0]]).includes('category')).toBeTruthy()
// Check that it can be cached at the CDN
expect(res.headers['set-cookie']).toBeUndefined()
expect(res.headers['cache-control']).toContain('public')
expect(res.headers['cache-control']).toMatch(/max-age=[1-9]/)
expect(res.headers['surrogate-control']).toContain('public')
expect(res.headers['surrogate-control']).toMatch(/max-age=[1-9]/)
const surrogateKeySplit = res.headers['surrogate-key'].split(/\s/g)
expect(surrogateKeySplit.includes(SURROGATE_ENUMS.DEFAULT)).toBeTruthy()
expect(surrogateKeySplit.includes(makeLanguageSurrogateKey())).toBeTruthy()
})
test('get non-fpt version webhook', async () => {
const sp = new URLSearchParams()
sp.set('category', 'branch_protection_rule')
sp.set('version', 'enterprise-cloud@latest')
const res = await get('/api/webhooks/v1?' + sp)
expect(res.statusCode).toBe(200)
const results = JSON.parse(res.text)
const actionTypes = Object.keys(results)
expect(actionTypes.length).toBeGreaterThan(2)
expect(Object.keys(results[actionTypes[0]]).includes('category')).toBeTruthy()
expect(res.statusCode).toBe(200)
})
test('unknown webhook category', async () => {
const sp = new URLSearchParams()
sp.set('category', 'no-such-category')
sp.set('version', 'free-pro-team@latest')
const res = await get('/api/webhooks/v1?' + sp)
expect(res.statusCode).toBe(404)
expect(JSON.parse(res.text).error).toBeTruthy()
})
test('unknown version', async () => {
const sp = new URLSearchParams()
sp.set('category', 'branch_protection_rule')
sp.set('version', 'no-such-version')
const res = await get('/api/webhooks/v1?' + sp)
expect(res.statusCode).toBe(404)
expect(JSON.parse(res.text).error).toBeTruthy()
})
})