-
Notifications
You must be signed in to change notification settings - Fork 66.7k
Expand file tree
/
Copy pathpageinfo.ts
More file actions
279 lines (256 loc) · 10.7 KB
/
pageinfo.ts
File metadata and controls
279 lines (256 loc) · 10.7 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { beforeAll, describe, expect, test } from 'vitest'
import { get } from '@/tests/helpers/e2etest'
import { SURROGATE_ENUMS } from '@/frame/middleware/set-fastly-surrogate-key'
import { latest } from '@/versions/lib/enterprise-server-releases'
const makeURL = (pathname: string): string =>
`/api/article/meta?${new URLSearchParams({ pathname })}`
interface PageMetadata {
product: string
title: string
intro: string
documentType: string | null
redirectedFrom?: string
}
interface ErrorResponse {
error: string
}
describe('pageinfo 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 pageinfo 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 pageinfo 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'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.product).toBe('Get started')
expect(meta.title).toBe('Start your journey')
expect(meta.intro).toBe(
'Get started using HubGit to manage Git repositories and collaborate with others.',
)
expect(meta.documentType).toBe('category')
// Canonical URLs should not have redirectedFrom
expect(meta.redirectedFrom).toBeUndefined()
// 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]/)
expect(res.headers['surrogate-key']).toBe(`${SURROGATE_ENUMS.DEFAULT} language:en`)
})
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) as ErrorResponse
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/meta')
expect(res.statusCode).toBe(400)
const { error } = JSON.parse(res.body) as ErrorResponse
expect(error).toBe("No 'pathname' query")
})
test("empty 'pathname' query string", async () => {
const res = await get('/api/article/meta?pathname=%20')
expect(res.statusCode).toBe(400)
const { error } = JSON.parse(res.body) as ErrorResponse
expect(error).toBe("'pathname' query empty")
})
test('repeated pathname query string key', async () => {
const res = await get('/api/article/meta?pathname=a&pathname=b')
expect(res.statusCode).toBe(400)
const { error } = JSON.parse(res.body) as ErrorResponse
expect(error).toBe("Multiple 'pathname' keys")
})
test('redirects correct the URL', async () => {
// Regular redirect from `redirect_from`
{
const res = await get(makeURL('/en/olden-days'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.title).toBe('HubGit.com Fixture Documentation')
expect(meta.redirectedFrom).toBe('/en/olden-days')
}
// Trailing slashes are always removed
{
const res = await get(makeURL('/en/olden-days/'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.title).toBe('HubGit.com Fixture Documentation')
expect(meta.redirectedFrom).toBe('/en/olden-days')
}
// Short code for latest version
{
const res = await get(makeURL('/en/enterprise-server@latest/get-started/liquid/ifversion'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.intro).toMatch(/\(not on fpt\)/)
}
// A URL that doesn't have fpt as an available version
{
const res = await get(makeURL('/en/get-started/versioning/only-ghec-and-ghes'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.title).toBe('Only in Enterprise Cloud and Enterprise Server')
}
})
test('a page that uses non-trivial Liquid to render', async () => {
// This page uses `{% ifversion not fpt %}` in the intro.
// First on the fpt version
{
const res = await get(makeURL('/en/get-started/liquid/ifversion'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.intro).toMatch(/\(on fpt\)/)
}
// Second on any other version
{
const res = await get(makeURL('/en/enterprise-server@latest/get-started/liquid/ifversion'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.intro).toMatch(/\(not on fpt\)/)
}
})
test('home pages', async () => {
// The home page with language specified
{
const res = await get(makeURL('/en'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.title).toMatch('HubGit.com Fixture Documentation')
}
// enterprise-server with language specified
// This is important because it tests that we check for a page
// before we bothering to see if it can be a redirect.
// That's how our middleware and Next router works. First we look
// for a page, if it can't be found, then we check if it's a redirect.
// This test proves something that caused a bug in production.
{
const res = await get(makeURL(`/en/enterprise-server@${latest}`))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.title).toMatch('HubGit Enterprise Server Fixture Documentation')
}
})
test('home pages (with redirects)', async () => {
// The home page for the default language *not* specified
{
const res = await get(makeURL('/'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.title).toMatch('HubGit.com Fixture Documentation')
}
// enterprise-server without language specified
{
const res = await get(makeURL('/enterprise-server@latest'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.title).toMatch('HubGit Enterprise Server Fixture Documentation')
}
})
test('documentType for different page types', async () => {
// Homepage
{
const res = await get(makeURL('/en'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.documentType).toBe('homepage')
}
// Product
{
const res = await get(makeURL('/en/get-started'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.documentType).toBe('product')
}
// Category
{
const res = await get(makeURL('/en/get-started/start-your-journey'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.documentType).toBe('category')
}
// Subcategory
{
const res = await get(makeURL('/en/actions/category/subcategory'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.documentType).toBe('subcategory')
}
// Article
{
const res = await get(makeURL('/en/get-started/start-your-journey/hello-world'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.documentType).toBe('article')
}
})
test('archived enterprise versions', async () => {
// For example /en/enterprise-server@3.8 is a valid Page in the
// site tree, but /en/enterprise-server@2.6 is not. Yet we can
// 200 OK and serve content for that. This needs to be reflected in
// page info too. Even if we have to "fabricate" the title a bit.
// At the time of writing, the latest archived version
{
const res = await get(makeURL('/en/enterprise-server@3.2'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.title).toMatch('GitHub Enterprise Server 3.2 Help Documentation')
expect(meta.documentType).toBeNull()
}
// The oldest known archived version that we proxy
{
const res = await get(makeURL('/en/enterprise/11.10.340'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.title).toMatch('GitHub Enterprise Server 11.10.340 Help Documentation')
}
})
test('pathname has to start with /', async () => {
const res = await get(makeURL('ip'))
expect(res.statusCode).toBe(400)
const { error } = JSON.parse(res.body) as ErrorResponse
expect(error).toBe("'pathname' has to start with /")
})
test("pathname can't contain spaces /", async () => {
const res = await get(makeURL('/en foo bar'))
expect(res.statusCode).toBe(400)
const { error } = JSON.parse(res.body) as ErrorResponse
expect(error).toBe("'pathname' cannot contain whitespace")
})
describe('translations', () => {
test('Japanese page', async () => {
const res = await get(makeURL('/ja/get-started/start-your-journey/hello-world'))
expect(res.statusCode).toBe(200)
const meta = JSON.parse(res.body) as PageMetadata
expect(meta.product).toBe('はじめに')
expect(meta.title).toBe('こんにちは World')
expect(meta.intro).toBe('この Hello World 演習に従って、HubGit の使用を開始します。')
})
test('falls back to English if translation is not present', async () => {
const enRes = await get(makeURL('/en/get-started/start-your-journey'))
expect(enRes.statusCode).toBe(200)
// This page doesn't have a Japanese translation. I.e. it doesn't
// even exist on disk. So it'll fall back to English.
const translationRes = await get(makeURL('/ja/get-started/start-your-journey'))
expect(translationRes.statusCode).toBe(200)
const en = JSON.parse(enRes.body) as PageMetadata
const translation = JSON.parse(translationRes.body) as PageMetadata
expect(en.title).toBe(translation.title)
expect(en.intro).toBe(translation.intro)
})
})
})