@@ -2,12 +2,14 @@ import { fileURLToPath } from 'url'
2
2
import path from 'path'
3
3
import { isPlainObject } from 'lodash-es'
4
4
import supertest from 'supertest'
5
+ import { jest } from '@jest/globals'
6
+
5
7
import createApp from '../../lib/app.js'
6
8
import enterpriseServerReleases from '../../lib/enterprise-server-releases.js'
7
9
import Page from '../../lib/page.js'
8
10
import { get } from '../helpers/supertest.js'
9
11
import versionSatisfiesRange from '../../lib/version-satisfies-range.js'
10
- import { jest } from '@jest/globals '
12
+ import { PREFERRED_LOCALE_COOKIE_NAME } from '../../middleware/detect-language.js '
11
13
12
14
const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) )
13
15
@@ -132,6 +134,28 @@ describe('redirects', () => {
132
134
expect ( res . headers . location ) . toBe ( '/ja' )
133
135
expect ( res . headers [ 'cache-control' ] ) . toBe ( 'private, no-store' )
134
136
} )
137
+ test ( 'homepage redirects to preferred language by cookie' , async ( ) => {
138
+ const res = await get ( '/' , {
139
+ headers : {
140
+ Cookie : `${ PREFERRED_LOCALE_COOKIE_NAME } =ja` ,
141
+ 'Accept-Language' : 'es' , // note how this is going to be ignored
142
+ } ,
143
+ } )
144
+ expect ( res . statusCode ) . toBe ( 302 )
145
+ expect ( res . headers . location ) . toBe ( '/ja' )
146
+ expect ( res . headers [ 'cache-control' ] ) . toBe ( 'private, no-store' )
147
+ } )
148
+ test ( 'homepage redirects to preferred language by cookie if valid' , async ( ) => {
149
+ const res = await get ( '/' , {
150
+ headers : {
151
+ Cookie : `${ PREFERRED_LOCALE_COOKIE_NAME } =xy` ,
152
+ 'Accept-Language' : 'ja' , // note how this is going to be ignored
153
+ } ,
154
+ } )
155
+ expect ( res . statusCode ) . toBe ( 302 )
156
+ expect ( res . headers . location ) . toBe ( '/ja' )
157
+ expect ( res . headers [ 'cache-control' ] ) . toBe ( 'private, no-store' )
158
+ } )
135
159
} )
136
160
137
161
describe ( 'external redirects' , ( ) => {
@@ -149,14 +173,68 @@ describe('redirects', () => {
149
173
} )
150
174
151
175
describe ( 'localized redirects' , ( ) => {
176
+ const redirectFrom =
177
+ '/desktop/contributing-to-projects/changing-a-remote-s-url-from-github-desktop'
178
+ const redirectTo =
179
+ '/desktop/contributing-and-collaborating-using-github-desktop/working-with-your-remote-repository-on-github-or-github-enterprise/changing-a-remotes-url-from-github-desktop'
180
+
152
181
test ( 'redirect_from for renamed pages' , async ( ) => {
153
- const { res } = await get (
154
- '/ja/desktop/contributing-to-projects/changing-a-remote-s-url-from-github-desktop'
155
- )
182
+ const { res } = await get ( `/ja${ redirectFrom } ` )
156
183
expect ( res . statusCode ) . toBe ( 301 )
157
- const expected =
158
- '/ja/desktop/contributing-and-collaborating-using-github-desktop/working-with-your-remote-repository-on-github-or-github-enterprise/changing-a-remotes-url-from-github-desktop'
184
+ const expected = `/ja${ redirectTo } `
185
+ expect ( res . headers . location ) . toBe ( expected )
186
+ } )
187
+
188
+ test ( 'redirect_from for renamed pages by Accept-Language header' , async ( ) => {
189
+ const { res } = await get ( redirectFrom , {
190
+ headers : {
191
+ 'Accept-Language' : 'ja' ,
192
+ } ,
193
+ } )
194
+ expect ( res . statusCode ) . toBe ( 302 )
195
+ const expected = `/ja${ redirectTo } `
159
196
expect ( res . headers . location ) . toBe ( expected )
197
+ expect ( res . headers [ 'cache-control' ] ) . toBe ( 'private, no-store' )
198
+ } )
199
+
200
+ test ( 'redirect_from for renamed pages but ignore Accept-Language header if not recognized' , async ( ) => {
201
+ const { res } = await get ( redirectFrom , {
202
+ headers : {
203
+ // None of these are recognized
204
+ 'Accept-Language' : 'sv,fr,gr' ,
205
+ } ,
206
+ } )
207
+ expect ( res . statusCode ) . toBe ( 302 )
208
+ const expected = `/en${ redirectTo } `
209
+ expect ( res . headers . location ) . toBe ( expected )
210
+ expect ( res . headers [ 'cache-control' ] ) . toBe ( 'private, no-store' )
211
+ } )
212
+
213
+ test ( 'redirect_from for renamed pages but ignore unrecognized Accept-Language header values' , async ( ) => {
214
+ const { res } = await get ( redirectFrom , {
215
+ headers : {
216
+ // Only the last one is recognized
217
+ 'Accept-Language' : 'sv,ja' ,
218
+ } ,
219
+ } )
220
+ expect ( res . statusCode ) . toBe ( 302 )
221
+ const expected = `/ja${ redirectTo } `
222
+ expect ( res . headers . location ) . toBe ( expected )
223
+ expect ( res . headers [ 'cache-control' ] ) . toBe ( 'private, no-store' )
224
+ } )
225
+
226
+ test ( 'will inject the preferred language from cookie' , async ( ) => {
227
+ const { res } = await get ( redirectFrom , {
228
+ headers : {
229
+ Cookie : `${ PREFERRED_LOCALE_COOKIE_NAME } =ja` ,
230
+ 'Accept-Language' : 'es' , // note how this is going to be ignored
231
+ } ,
232
+ } )
233
+ // 302 because the redirect depended on cookie
234
+ expect ( res . statusCode ) . toBe ( 302 )
235
+ const expected = `/ja${ redirectTo } `
236
+ expect ( res . headers . location ) . toBe ( expected )
237
+ expect ( res . headers [ 'cache-control' ] ) . toBe ( 'private, no-store' )
160
238
} )
161
239
} )
162
240
0 commit comments