@@ -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,13 +173,63 @@ 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 } `
196+ expect ( res . headers . location ) . toBe ( expected )
197+ } )
198+
199+ test ( 'redirect_from for renamed pages but ignore Accept-Language header if not recognized' , async ( ) => {
200+ const { res } = await get ( redirectFrom , {
201+ headers : {
202+ // None of these are recognized
203+ 'Accept-Language' : 'sv,fr,gr' ,
204+ } ,
205+ } )
206+ expect ( res . statusCode ) . toBe ( 302 )
207+ const expected = `/en${ redirectTo } `
208+ expect ( res . headers . location ) . toBe ( expected )
209+ } )
210+
211+ test ( 'redirect_from for renamed pages but ignore unrecognized Accept-Language header values' , async ( ) => {
212+ const { res } = await get ( redirectFrom , {
213+ headers : {
214+ // Only the last one is recognized
215+ 'Accept-Language' : 'sv,ja' ,
216+ } ,
217+ } )
218+ expect ( res . statusCode ) . toBe ( 302 )
219+ const expected = `/ja${ redirectTo } `
220+ expect ( res . headers . location ) . toBe ( expected )
221+ } )
222+
223+ test ( 'will inject the preferred language from cookie' , async ( ) => {
224+ const { res } = await get ( redirectFrom , {
225+ headers : {
226+ Cookie : `${ PREFERRED_LOCALE_COOKIE_NAME } =ja` ,
227+ 'Accept-Language' : 'es' , // note how this is going to be ignored
228+ } ,
229+ } )
230+ // 302 because the redirect depended on cookie
231+ expect ( res . statusCode ) . toBe ( 302 )
232+ const expected = `/ja${ redirectTo } `
159233 expect ( res . headers . location ) . toBe ( expected )
160234 } )
161235 } )
0 commit comments