Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

redirects.js 16 KB

You have to be logged in to leave a comment. Sign In
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  1. const path = require('path')
  2. const { isPlainObject } = require('lodash')
  3. const supertest = require('supertest')
  4. const app = require('../../lib/app')
  5. const enterpriseServerReleases = require('../../lib/enterprise-server-releases')
  6. const nonEnterpriseDefaultVersion = require('../../lib/non-enterprise-default-version')
  7. const Page = require('../../lib/page')
  8. const { get } = require('../helpers/supertest')
  9. describe('redirects', () => {
  10. jest.setTimeout(5 * 60 * 1000)
  11. let redirects
  12. beforeAll(async (done) => {
  13. const res = await get('/en?json=redirects')
  14. redirects = JSON.parse(res.text)
  15. done()
  16. })
  17. test('page.redirects is an array', async () => {
  18. const page = await Page.init({
  19. relativePath: 'github/collaborating-with-issues-and-pull-requests/about-branches.md',
  20. basePath: path.join(__dirname, '../../content'),
  21. languageCode: 'en'
  22. })
  23. page.buildRedirects()
  24. expect(isPlainObject(page.redirects)).toBe(true)
  25. })
  26. test('dotcom homepage page.redirects', async () => {
  27. const page = await Page.init({
  28. relativePath: 'github/index.md',
  29. basePath: path.join(__dirname, '../../content'),
  30. languageCode: 'en'
  31. })
  32. page.buildRedirects()
  33. expect(page.redirects[`/en/${nonEnterpriseDefaultVersion}/github`]).toBe('/en/github')
  34. expect(page.redirects['/articles']).toBe('/en/github')
  35. expect(page.redirects['/en/articles']).toBe('/en/github')
  36. expect(page.redirects[`/en/${nonEnterpriseDefaultVersion}/articles`]).toBe('/en/github')
  37. expect(page.redirects['/common-issues-and-questions']).toBe('/en/github')
  38. expect(page.redirects['/en/common-issues-and-questions']).toBe('/en/github')
  39. expect(page.redirects[`/en/enterprise/${enterpriseServerReleases.latest}/user/articles`]).toBe(`/en/enterprise-server@${enterpriseServerReleases.latest}/github`)
  40. expect(page.redirects[`/en/enterprise/${enterpriseServerReleases.latest}/user/common-issues-and-questions`]).toBe(`/en/enterprise-server@${enterpriseServerReleases.latest}/github`)
  41. })
  42. test('converts single `redirect_from` strings values into arrays', async () => {
  43. const page = await Page.init({
  44. relativePath: 'github/collaborating-with-issues-and-pull-requests/about-conversations-on-github.md',
  45. basePath: path.join(__dirname, '../../content'),
  46. languageCode: 'en'
  47. })
  48. page.buildRedirects()
  49. const expected = '/en/github/collaborating-with-issues-and-pull-requests/about-conversations-on-github'
  50. expect(page.redirects['/en/articles/about-discussions-in-issues-and-pull-requests']).toBe(expected)
  51. })
  52. describe('query params', () => {
  53. test('are preserved in redirected URLs', async () => {
  54. const res = await get('/enterprise/admin?query=pulls')
  55. expect(res.statusCode).toBe(301)
  56. const expected = `/en/enterprise-server@${enterpriseServerReleases.latest}/admin?query=pulls`
  57. expect(res.headers.location).toBe(expected)
  58. })
  59. test('have q= converted to query=', async () => {
  60. const res = await get('/en/enterprise/admin?q=pulls')
  61. expect(res.statusCode).toBe(301)
  62. const expected = `/en/enterprise-server@${enterpriseServerReleases.latest}/admin?query=pulls`
  63. expect(res.headers.location).toBe(expected)
  64. })
  65. test('work with redirected search paths', async () => {
  66. const res = await get('/en/enterprise/admin/search?utf8=%E2%9C%93&query=pulls')
  67. expect(res.statusCode).toBe(301)
  68. const expected = `/en/enterprise-server@${enterpriseServerReleases.latest}/admin?utf8=%E2%9C%93&query=pulls`
  69. expect(res.headers.location).toBe(expected)
  70. })
  71. test('do not work on other paths that include "search"', async () => {
  72. const reqPath = `/en/enterprise-server@${enterpriseServerReleases.latest}/admin/configuration/enabling-unified-search-between-github-enterprise-server-and-githubcom`
  73. const res = await get(reqPath)
  74. expect(res.statusCode).toBe(200)
  75. })
  76. test('work on deprecated versions', async () => {
  77. const res = await get('/enterprise/2.12/admin/search?utf8=%E2%9C%93&q=pulls')
  78. expect(res.statusCode).toBe(200)
  79. })
  80. })
  81. describe('trailing slashes', () => {
  82. test('are absent from all redirected URLs', async () => {
  83. const keys = Object.keys(redirects)
  84. expect(keys.length).toBeGreaterThan(100)
  85. expect(keys.every(key => !key.endsWith('/') || key === '/')).toBe(true)
  86. })
  87. test('are absent from all destination URLs', async () => {
  88. const values = Object.values(redirects)
  89. expect(values.length).toBeGreaterThan(100)
  90. expect(values.every(value => !value.endsWith('/'))).toBe(true)
  91. })
  92. test('are redirected for HEAD requests (not just GET requests)', async () => {
  93. const res = await supertest(app).head('/articles/closing-issues-via-commit-messages/')
  94. expect(res.statusCode).toBe(301)
  95. expect(res.headers.location).toBe('/articles/closing-issues-via-commit-messages')
  96. })
  97. })
  98. describe('external redirects', () => {
  99. test('work for top-level request paths', async () => {
  100. const res = await get('/git-ready')
  101. expect(res.statusCode).toBe(301)
  102. expect(res.headers.location).toBe('http://gitready.com/')
  103. })
  104. test('work for article-level request paths', async () => {
  105. const res = await get('/articles/testing-webhooks')
  106. expect(res.statusCode).toBe(301)
  107. expect(res.headers.location).toBe('http://developer.github.com/webhooks/testing/')
  108. })
  109. })
  110. describe('localized redirects', () => {
  111. test('redirect_from for renamed pages', async () => {
  112. const { res } = await get('/ja/desktop/contributing-to-projects/changing-a-remote-s-url-from-github-desktop')
  113. expect(res.statusCode).toBe(301)
  114. const expected = '/ja/desktop/contributing-and-collaborating-using-github-desktop/changing-a-remotes-url-from-github-desktop'
  115. expect(res.headers.location).toBe(expected)
  116. })
  117. })
  118. describe('enterprise home page', () => {
  119. const enterpriseHome = `/en/enterprise-server@${enterpriseServerReleases.latest}`
  120. const japaneseEnterpriseHome = enterpriseHome.replace('/en/', '/ja/')
  121. test('/enterprise', async () => {
  122. const res = await get('/enterprise')
  123. expect(res.statusCode).toBe(301)
  124. expect(res.headers.location).toBe(enterpriseHome)
  125. })
  126. test('no language code redirects to english', async () => {
  127. const res = await get(`/enterprise/${enterpriseServerReleases.latest}`)
  128. expect(res.statusCode).toBe(301)
  129. expect(res.headers.location).toBe(enterpriseHome)
  130. })
  131. test('no version redirects to latest version', async () => {
  132. const res = await get('/en/enterprise')
  133. expect(res.statusCode).toBe(301)
  134. expect(res.headers.location).toBe(enterpriseHome)
  135. })
  136. test('no version redirects to latest version (japanese)', async () => {
  137. const res = await get('/ja/enterprise')
  138. expect(res.statusCode).toBe(301)
  139. expect(res.headers.location).toBe(japaneseEnterpriseHome)
  140. })
  141. test('hardcoded @latest redirects to latest version', async () => {
  142. const res = await get('/en/enterprise-server@latest')
  143. expect(res.statusCode).toBe(301)
  144. expect(res.headers.location).toBe(enterpriseHome)
  145. })
  146. })
  147. describe('2.13+ deprecated enterprise', () => {
  148. test('no language code redirects to english', async () => {
  149. const res = await get('/enterprise/2.13')
  150. expect(res.statusCode).toBe(301)
  151. expect(res.headers.location).toBe('/en/enterprise/2.13')
  152. })
  153. test('admin/guides redirects to admin', async () => {
  154. const res = await get('/en/enterprise/2.13/admin/guides')
  155. expect(res.statusCode).toBe(301)
  156. expect(res.headers.location).toBe('/en/enterprise/2.13/admin')
  157. })
  158. })
  159. describe('<2.12 deprecated enterprise', () => {
  160. test('english language code redirects to no language code', async () => {
  161. const res = await get('/en/enterprise/2.12')
  162. expect(res.statusCode).toBe(301)
  163. expect(res.headers.location).toBe('/enterprise/2.12')
  164. })
  165. test('frontmatter redirect', async () => {
  166. const res = await get('/enterprise/2.12/user/articles/github-flavored-markdown')
  167. expect(res.statusCode).toBe(301)
  168. expect(res.text).toContain('location=\'/enterprise/2.12/user/categories/writing-on-github/\'')
  169. })
  170. })
  171. describe('enterprise admin', () => {
  172. const enterpriseAdmin = `/en/enterprise-server@${enterpriseServerReleases.latest}/admin`
  173. const japaneseEnterpriseAdmin = enterpriseAdmin.replace('/en/', '/ja/')
  174. test('no language code redirects to english', async () => {
  175. const res = await get(`/enterprise/${enterpriseServerReleases.latest}/admin`)
  176. expect(res.statusCode).toBe(301)
  177. expect(res.headers.location).toBe(enterpriseAdmin)
  178. })
  179. test('no version redirects to latest version', async () => {
  180. const res = await get('/en/enterprise/admin')
  181. expect(res.statusCode).toBe(301)
  182. expect(res.headers.location).toBe(enterpriseAdmin)
  183. })
  184. test('admin/guides redirects to admin', async () => {
  185. const res = await get(`/en/enterprise/${enterpriseServerReleases.latest}/admin/guides`)
  186. expect(res.statusCode).toBe(301)
  187. expect(res.headers.location).toBe(enterpriseAdmin)
  188. })
  189. test('no version plus admin/guides redirects to admin on latest version', async () => {
  190. const res = await get('/en/enterprise/admin/guides')
  191. expect(res.statusCode).toBe(301)
  192. expect(res.headers.location).toBe(enterpriseAdmin)
  193. })
  194. test('admin/guides redirects to admin in redirects', async () => {
  195. const res = await get(`/en/enterprise/${enterpriseServerReleases.latest}/admin/guides/installation/upgrading-github-enterprise`)
  196. expect(res.statusCode).toBe(301)
  197. const redirectRes = await get(res.headers.location)
  198. expect(redirectRes.statusCode).toBe(200)
  199. const expected = `/en/enterprise-server@${enterpriseServerReleases.latest}/admin/enterprise-management/upgrading-github-enterprise-server`
  200. expect(res.headers.location).toBe(expected)
  201. })
  202. test('no version redirects to latest version (japanese)', async () => {
  203. const res = await get('/ja/enterprise/admin')
  204. expect(res.statusCode).toBe(301)
  205. expect(res.headers.location).toBe(japaneseEnterpriseAdmin)
  206. })
  207. test('admin/guides redirects to admin (japanese)', async () => {
  208. const res = await get(`/ja/enterprise/${enterpriseServerReleases.latest}/admin/guides`)
  209. expect(res.statusCode).toBe(301)
  210. expect(res.headers.location).toBe(japaneseEnterpriseAdmin)
  211. })
  212. })
  213. describe('enterprise user homepage', () => {
  214. const enterpriseUser = `/en/enterprise-server@${enterpriseServerReleases.latest}/github`
  215. const japaneseEnterpriseUser = enterpriseUser.replace('/en/', '/ja/')
  216. test('no product redirects to GitHub.com product', async () => {
  217. const res = await get(`/en/enterprise/${enterpriseServerReleases.latest}/user`)
  218. expect(res.statusCode).toBe(301)
  219. expect(res.headers.location).toBe(enterpriseUser)
  220. })
  221. test('no language code redirects to english', async () => {
  222. const res = await get(`/enterprise/${enterpriseServerReleases.latest}/user/github`)
  223. expect(res.statusCode).toBe(301)
  224. expect(res.headers.location).toBe(enterpriseUser)
  225. })
  226. test('no version redirects to latest version', async () => {
  227. const res = await get('/en/enterprise/user/github')
  228. expect(res.statusCode).toBe(301)
  229. expect(res.headers.location).toBe(enterpriseUser)
  230. })
  231. test('no version redirects to latest version (japanese)', async () => {
  232. const res = await get('/ja/enterprise/user/github')
  233. expect(res.statusCode).toBe(301)
  234. expect(res.headers.location).toBe(japaneseEnterpriseUser)
  235. })
  236. })
  237. describe('enterprise user article', () => {
  238. const userArticle = `/en/enterprise-server@${enterpriseServerReleases.latest}/github/getting-started-with-github/set-up-git`
  239. const japaneseUserArticle = userArticle.replace('/en/', '/ja/')
  240. test('no product redirects to GitHub.com product on the latest version', async () => {
  241. const res = await get(`/en/enterprise/${enterpriseServerReleases.latest}/user/articles/set-up-git`)
  242. expect(res.statusCode).toBe(301)
  243. expect(res.headers.location).toBe(userArticle)
  244. })
  245. // 2.16 was the first version where we moved /articles/foo to /github/<category>/foo
  246. test('no product does not redirect to GitHub.com product in <=2.15', async () => {
  247. const res = await get('/en/enterprise/2.15/user/articles/set-up-git')
  248. expect(res.statusCode).toBe(200)
  249. })
  250. test('no language code redirects to english', async () => {
  251. const res = await get(`/enterprise/${enterpriseServerReleases.latest}/user/github/getting-started-with-github/set-up-git`)
  252. expect(res.statusCode).toBe(301)
  253. expect(res.headers.location).toBe(userArticle)
  254. })
  255. test('no version redirects to latest version', async () => {
  256. const res = await get('/en/enterprise/user/github/getting-started-with-github/set-up-git')
  257. expect(res.statusCode).toBe(301)
  258. expect(res.headers.location).toBe(userArticle)
  259. })
  260. test('no version redirects to latest version (japanese)', async () => {
  261. const res = await get('/ja/enterprise/user/github/getting-started-with-github/set-up-git')
  262. expect(res.statusCode).toBe(301)
  263. expect(res.headers.location).toBe(japaneseUserArticle)
  264. })
  265. })
  266. describe('enterprise user article with frontmatter redirect', () => {
  267. const userArticle = `/en/enterprise-server@${enterpriseServerReleases.latest}/github/getting-started-with-github/access-permissions-on-github`
  268. const redirectFromPath = '/articles/what-are-the-different-access-permissions'
  269. const japaneseUserArticle = userArticle.replace('/en/', '/ja/')
  270. test('redirects to expected article', async () => {
  271. const res = await get(`/en/enterprise/${enterpriseServerReleases.latest}/user${redirectFromPath}`)
  272. expect(res.statusCode).toBe(301)
  273. expect(res.headers.location).toBe(userArticle)
  274. })
  275. test('no language code redirects to english', async () => {
  276. const res = await get(`/enterprise/${enterpriseServerReleases.latest}/user${redirectFromPath}`)
  277. expect(res.statusCode).toBe(301)
  278. expect(res.headers.location).toBe(userArticle)
  279. })
  280. test('no version redirects to latest version', async () => {
  281. const res = await get(`/en/enterprise/user${redirectFromPath}`)
  282. expect(res.statusCode).toBe(301)
  283. expect(res.headers.location).toBe(userArticle)
  284. })
  285. test('no version redirects to latest version (japanese)', async () => {
  286. const res = await get(`/ja/enterprise/user${redirectFromPath}`)
  287. expect(res.statusCode).toBe(301)
  288. expect(res.headers.location).toBe(japaneseUserArticle)
  289. })
  290. })
  291. describe('desktop guide', () => {
  292. const desktopGuide = '/en/desktop/contributing-and-collaborating-using-github-desktop/creating-an-issue-or-pull-request'
  293. const japaneseDesktopGuides = desktopGuide.replace('/en/', '/ja/')
  294. test('no language code redirects to english', async () => {
  295. const res = await get('/desktop/contributing-and-collaborating-using-github-desktop/creating-an-issue-or-pull-request')
  296. expect(res.statusCode).toBe(301)
  297. expect(res.headers.location).toBe(desktopGuide)
  298. })
  299. test('desktop/guides redirects to desktop', async () => {
  300. const res = await get('/en/desktop/guides/contributing-and-collaborating-using-github-desktop/creating-an-issue-or-pull-request')
  301. expect(res.statusCode).toBe(301)
  302. expect(res.headers.location).toBe(desktopGuide)
  303. })
  304. test('desktop/guides redirects to desktop (japanese)', async () => {
  305. const res = await get('/ja/desktop/guides/contributing-and-collaborating-using-github-desktop/creating-an-issue-or-pull-request')
  306. expect(res.statusCode).toBe(301)
  307. expect(res.headers.location).toBe(japaneseDesktopGuides)
  308. })
  309. })
  310. })
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...