-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathfind-page-middleware.ts
More file actions
162 lines (139 loc) · 4.59 KB
/
find-page-middleware.ts
File metadata and controls
162 lines (139 loc) · 4.59 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
import { fileURLToPath } from 'url'
import path from 'path'
import http from 'http'
import { Socket } from 'net'
import { describe, expect, test } from 'vitest'
import type { Response } from 'express'
import Page from '@/frame/lib/page'
import findPage from '@/frame/middleware/find-page'
import type { ExtendedRequest, Context } from '@/types'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
type TestResponse = Response & { _status?: number; _message?: string }
function makeRequestResponse(
url: string,
currentVersion = 'free-pro-team@latest',
): [ExtendedRequest, TestResponse] {
const req = new http.IncomingMessage(new Socket()) as ExtendedRequest
Object.defineProperty(req, 'path', {
value: url,
writable: true,
})
req.method = 'GET'
req.url = url
req.cookies = {}
req.headers = {}
const context: Context = {
currentVersion,
pages: {},
}
req.pagePath = url
req.context = context
const res = new http.ServerResponse(req) as TestResponse
res.status = function status(this: TestResponse, code: number) {
this._status = code
return Object.assign(this, {
send: (message: string) => {
this._message = message
return 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)
const page = await Page.init({
relativePath: 'page-with-redirects.md',
basePath: path.join(__dirname, '../../../src/fixtures/fixtures'),
languageCode: 'en',
})
if (page && req.context) {
req.context.pages = {
'/en/foo/bar': page,
}
}
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')
const page = await Page.init({
relativePath: 'page-with-redirects.md',
basePath: path.join(__dirname, '../../../src/fixtures/fixtures'),
languageCode: 'en',
})
if (page && req.context) {
req.context.pages = {
'/en/page-with-redirects': page,
}
}
await findPage(req, res, () => {}, {
isDev: true,
contentRoot: path.join(__dirname, '../../../src/fixtures/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')
const page = await Page.init({
relativePath: 'page-with-redirects.md',
basePath: path.join(__dirname, '../../../src/fixtures/fixtures'),
languageCode: 'en',
})
if (page && req.context) {
req.context.pages = {
'/en/page-with-redirects': page,
}
}
await findPage(req, res, () => {}, {
isDev: true,
contentRoot: path.join(__dirname, '../../../src/fixtures/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')
const page = await Page.init({
relativePath: 'page-with-redirects.md',
basePath: path.join(__dirname, '../../../src/fixtures/fixtures'),
languageCode: 'en',
})
if (page && req.context) {
req.context.pages = {
'/en/page-with-redirects': page,
}
}
await findPage(req, res, () => {}, {
isDev: true,
contentRoot: path.join(__dirname, '../../../src/fixtures/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, '../../../src/fixtures/fixtures'),
})
expect(req.context?.page).toBe(undefined)
})
})