@@ -2,12 +2,14 @@ import { fileURLToPath } from 'url'
22import path from 'path'
33import { isPlainObject } from 'lodash-es'
44import supertest from 'supertest'
5+ import { jest } from '@jest/globals'
6+
57import createApp from '../../lib/app.js'
68import enterpriseServerReleases from '../../lib/enterprise-server-releases.js'
79import Page from '../../lib/page.js'
810import { get } from '../helpers/supertest.js'
911import versionSatisfiesRange from '../../lib/version-satisfies-range.js'
10- import { jest } from '@jest/globals '
12+ import { PREFERRED_LOCALE_COOKIE_NAME } from '../../middleware/detect-language.js '
1113
1214const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) )
1315
@@ -132,6 +134,28 @@ describe('redirects', () => {
132134 expect ( res . headers . location ) . toBe ( '/ja' )
133135 expect ( res . headers [ 'cache-control' ] ) . toBe ( 'private, no-store' )
134136 } )
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+ } )
135159 } )
136160
137161 describe ( 'external redirects' , ( ) => {
@@ -149,14 +173,68 @@ describe('redirects', () => {
149173 } )
150174
151175 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+
152181 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 } ` )
156183 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 } `
159196 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' )
160238 } )
161239 } )
162240
0 commit comments