forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-page-middleware.js
133 lines (115 loc) · 3.84 KB
/
find-page-middleware.js
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
import { fileURLToPath } from 'url'
import path from 'path'
import http from 'http'
import { expect, describe, test } from '@jest/globals'
import Page from '../../lib/page.js'
import findPage from '../../middleware/find-page.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
function makeRequestResponse(url, currentVersion = 'free-pro-team@latest') {
const req = new http.IncomingMessage(null)
req.method = 'GET'
req.url = url
req.path = url
req.cookies = {}
req.headers = {}
// Custom keys on the request
req.pagePath = url
req.context = {}
req.context.currentVersion = currentVersion
req.context.pages = {}
const res = new http.ServerResponse(req)
res.status = function (code) {
this._status = code
return {
send: function (message) {
this._message = message
}.bind(this),
}
}
return [req, res]
}
describe('find page middleware', () => {
test('attaches page on req.context', async () => {
const url = '/en/foo/bar'
const [req, res] = makeRequestResponse(url)
req.context.pages = {
'/en/foo/bar': await Page.init({
relativePath: 'page-with-redirects.md',
basePath: path.join(__dirname, '../fixtures'),
languageCode: 'en',
}),
}
let nextCalls = 0
await findPage(req, res, () => {
nextCalls++
})
expect(nextCalls).toBe(1)
expect(req.context.page).toBeInstanceOf(Page)
})
test('does not attach page on req.context if not found', async () => {
const [req, res] = makeRequestResponse('/en/foo/bar')
let nextCalls = 0
await findPage(req, res, () => {
nextCalls++
})
expect(nextCalls).toBe(1)
expect(req.context.page).toBe(undefined)
})
test('re-reads from disk if in development mode', async () => {
const [req, res] = makeRequestResponse('/en/page-with-redirects')
req.context.pages = {
'/en/page-with-redirects': await Page.init({
relativePath: 'page-with-redirects.md',
basePath: path.join(__dirname, '../fixtures'),
languageCode: 'en',
}),
}
await findPage(req, res, () => {}, {
isDev: true,
contentRoot: path.join(__dirname, '../fixtures'),
})
expect(req.context.page).toBeInstanceOf(Page)
})
test('finds it for non-fpt version URLS', async () => {
const [req, res] = makeRequestResponse('/en/page-with-redirects', 'enterprise-cloud@latest')
req.context.pages = {
'/en/page-with-redirects': await Page.init({
relativePath: 'page-with-redirects.md',
basePath: path.join(__dirname, '../fixtures'),
languageCode: 'en',
}),
}
await findPage(req, res, () => {}, {
isDev: true,
contentRoot: path.join(__dirname, '../fixtures'),
})
expect(req.context.page).toBeInstanceOf(Page)
})
test("will 404 if the request version doesn't match the page", async () => {
// The 'versions:' frontmatter on 'page-with-redirects.md' does
// not include ghes. So this'll eventually 404.
const [req, res] = makeRequestResponse('/en/page-with-redirects', 'enterprise-server@latest')
req.context.pages = {
'/en/page-with-redirects': await Page.init({
relativePath: 'page-with-redirects.md',
basePath: path.join(__dirname, '../fixtures'),
languageCode: 'en',
}),
}
await findPage(req, res, () => {}, {
isDev: true,
contentRoot: path.join(__dirname, '../fixtures'),
})
expect(res._status).toBe(404)
expect(res._message).toMatch('')
expect(req.context.page).toBeUndefined()
})
test('re-reads from disk if in development mode and finds nothing', async () => {
const [req, res] = makeRequestResponse('/en/never/heard/of')
await findPage(req, res, () => {}, {
isDev: true,
contentRoot: path.join(__dirname, '../fixtures'),
})
expect(req.context.page).toBe(undefined)
})
})