-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathredirects.test.ts
175 lines (143 loc) · 4.67 KB
/
redirects.test.ts
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
import { Url, strToQueryParams } from "@ourworldindata/utils"
import { formatUrls } from "../site/formatting.js"
import * as redirects from "./redirects.js"
import { resolveInternalRedirect } from "./redirects.js"
import { jest } from "@jest/globals"
import { KnexReadonlyTransaction } from "../db/db.js"
type ArrayForMap = [string, string][]
const getGrapherAndWordpressRedirectsMap = jest.spyOn(
redirects,
"getGrapherAndWordpressRedirectsMap"
)
const getFormattedUrl = (url: string): Url => {
return Url.fromURL(formatUrls(url))
}
it("resolves pathnames", async () => {
const src = "/hello"
const target = "/world"
const redirectsArr: ArrayForMap = [[src, target]]
const urlToResolve = getFormattedUrl(src)
const resolvedUrl = getFormattedUrl(target)
getGrapherAndWordpressRedirectsMap.mockImplementation(() =>
Promise.resolve(new Map(redirectsArr))
)
expect(
await resolveInternalRedirect(
urlToResolve,
{} as KnexReadonlyTransaction
)
).toEqual(resolvedUrl)
})
it("does not support query string in redirects map", async () => {
const src = "/hello?q=1"
const target = "/world"
const redirectsArr: ArrayForMap = [[src, target]]
const urlToResolve = getFormattedUrl(src)
getGrapherAndWordpressRedirectsMap.mockImplementation(() =>
Promise.resolve(new Map(redirectsArr))
)
expect(
await resolveInternalRedirect(
urlToResolve,
{} as KnexReadonlyTransaction
)
).toEqual(urlToResolve)
})
it("passes query string params when resolving", async () => {
const src = "/hello"
const target = "/world"
const redirectsArr: ArrayForMap = [[src, target]]
const queryString = "?q=1"
const urlToResolve = getFormattedUrl(src).setQueryParams(
strToQueryParams(queryString)
)
const resolvedUrl = getFormattedUrl(target).setQueryParams(
strToQueryParams(queryString)
)
getGrapherAndWordpressRedirectsMap.mockImplementation(() =>
Promise.resolve(new Map(redirectsArr))
)
expect(
await resolveInternalRedirect(
urlToResolve,
{} as KnexReadonlyTransaction
)
).toEqual(resolvedUrl)
})
it("does not pass query string params when some present on the target", async () => {
const src = "/hello"
const target = "/world?q=1"
const redirectsArr: ArrayForMap = [[src, target]]
const urlToResolve = getFormattedUrl(src).setQueryParams(
strToQueryParams("?z=2")
)
const resolvedUrl = getFormattedUrl(target)
getGrapherAndWordpressRedirectsMap.mockImplementation(() =>
Promise.resolve(new Map(redirectsArr))
)
expect(
await resolveInternalRedirect(
urlToResolve,
{} as KnexReadonlyTransaction
)
).toEqual(resolvedUrl)
})
it("resolves self-redirects", async () => {
const src = "/hello"
const target = "/hello"
const redirectsArr: ArrayForMap = [[src, target]]
const urlToResolve = getFormattedUrl(src)
getGrapherAndWordpressRedirectsMap.mockImplementation(() =>
Promise.resolve(new Map(redirectsArr))
)
expect(
await resolveInternalRedirect(
urlToResolve,
{} as KnexReadonlyTransaction
)
).toEqual(urlToResolve)
})
it("does not support query params in self-redirects", async () => {
const src = "/hello"
const target = "/hello?q=1"
const redirectsArr: ArrayForMap = [[src, target]]
const urlToResolve = getFormattedUrl(src)
const urlToResolveWithQueryParam = getFormattedUrl(src).setQueryParams(
strToQueryParams("?z=2")
)
getGrapherAndWordpressRedirectsMap.mockImplementation(() =>
Promise.resolve(new Map(redirectsArr))
)
expect(
await resolveInternalRedirect(
urlToResolve,
{} as KnexReadonlyTransaction
)
).toEqual(urlToResolve)
expect(
await resolveInternalRedirect(
urlToResolveWithQueryParam,
{} as KnexReadonlyTransaction
)
).toEqual(urlToResolveWithQueryParam)
})
it("resolves circular redirects", async () => {
const theKing = "/the-king"
const isDead = "/is-dead"
const longLive = "/long-live"
const redirectsArr: ArrayForMap = [
[theKing, isDead],
[isDead, longLive],
[longLive, theKing],
]
const urlToResolve = getFormattedUrl(theKing)
getGrapherAndWordpressRedirectsMap.mockImplementation(() =>
Promise.resolve(new Map(redirectsArr))
)
expect(
await resolveInternalRedirect(
urlToResolve,
{} as KnexReadonlyTransaction
)
).toEqual(urlToResolve)
})