-
Notifications
You must be signed in to change notification settings - Fork 66.8k
Expand file tree
/
Copy pathapi-article-body.ts
More file actions
77 lines (66 loc) · 2.97 KB
/
api-article-body.ts
File metadata and controls
77 lines (66 loc) · 2.97 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
import { beforeAll, describe, expect, test } from 'vitest'
import { get } from '#src/tests/helpers/e2etest.js'
const makeURL = (pathname: string) => `/api/article/body?${new URLSearchParams({ pathname })}`
describe('article body api', () => {
beforeAll(() => {
// If you didn't set the `ROOT` variable, the tests will fail rather
// cryptically. So as a warning for engineers running these tests,
// alert in case it was accidentally forgotten.
if (!process.env.ROOT) {
console.warn(
'WARNING: The articlebody tests require the ROOT environment variable to be set to the fixture root',
)
}
// Ditto for fixture-based translations to work
if (!process.env.TRANSLATIONS_FIXTURE_ROOT) {
console.warn(
'WARNING: The articlebody tests require the TRANSLATIONS_FIXTURE_ROOT environment variable to be set',
)
}
})
test('happy path', async () => {
const res = await get(makeURL('/en/get-started/start-your-journey/api-article-body-test-page'))
expect(res.statusCode).toBe(200)
expect(res.headers['content-type']).toContain('text/markdown')
expect(res.body).toContain('## About GitHub')
expect(res.body).toContain('## About Git')
expect(res.body).toMatch(/^#+\s+\w+/m) // Check for any markdown heading pattern
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]/)
})
test('a pathname that does not exist', async () => {
const res = await get(makeURL('/en/never/heard/of'))
expect(res.statusCode).toBe(404)
const { error } = JSON.parse(res.body)
expect(error).toBe("No page found for '/en/never/heard/of'")
})
test("no 'pathname' query string at all", async () => {
const res = await get('/api/article/body')
expect(res.statusCode).toBe(400)
const { error } = JSON.parse(res.body)
expect(error).toBe("No 'pathname' query")
})
test('has proper markdown structure with frontmatter removed', async () => {
const res = await get(makeURL('/en/get-started/start-your-journey/api-article-body-test-page'))
expect(res.statusCode).toBe(200)
// Should not contain frontmatter
expect(res.body).not.toMatch(/^---/)
// Should have at least one heading
expect(res.body).toMatch(/^#{1,6}\s+\w+/m)
})
test("empty 'pathname' query string", async () => {
const res = await get('/api/article/body?pathname=%20')
expect(res.statusCode).toBe(400)
const { error } = JSON.parse(res.body)
expect(error).toBe("'pathname' query empty")
})
test('repeated pathname query string key', async () => {
const res = await get('/api/article/body?pathname=a&pathname=b')
expect(res.statusCode).toBe(400)
const { error } = JSON.parse(res.body)
expect(error).toBe("Multiple 'pathname' keys")
})
})