Skip to content

Commit

Permalink
fix: dont call next on redirect (#479)
Browse files Browse the repository at this point in the history
* dont call next on redirect

* add test

* improve test

* return with function
  • Loading branch information
StarpTech authored and isaachinman committed Aug 28, 2019
1 parent d189349 commit 6ce951b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
48 changes: 47 additions & 1 deletion __tests__/middlewares/next-i18next-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const subpathFromLng: jest.Mock = require('../../src/utils').subpathFromLng
const subpathIsRequired: jest.Mock = require('../../src/utils').subpathIsRequired
const subpathIsPresent: jest.Mock = require('../../src/utils').subpathIsPresent
const removeSubpath: jest.Mock = require('../../src/utils').removeSubpath
const addSubpath: jest.Mock = require('../../src/utils').addSubpath

jest.mock('i18next-express-middleware', () => ({
handle: jest.fn(() => jest.fn()),
Expand All @@ -24,6 +25,7 @@ jest.mock('../../src/utils', () => ({
lngFromReq: jest.fn(),
removeSubpath: jest.fn(),
isServer: jest.fn(),
addSubpath: jest.fn()
}))

describe('next-18next middleware', () => {
Expand All @@ -41,7 +43,10 @@ describe('next-18next middleware', () => {
req = {
url: '/myapp.com/',
}
res = {}
res = {
redirect: jest.fn(),
header: jest.fn()
}
next = jest.fn()
})

Expand All @@ -54,6 +59,9 @@ describe('next-18next middleware', () => {
subpathFromLng.mockReset()
lngFromReq.mockReset()
removeSubpath.mockReset()
addSubpath.mockReset()
res.redirect.mockReset()
res.header.mockReset()
})

const callAllMiddleware = () => {
Expand Down Expand Up @@ -133,5 +141,43 @@ describe('next-18next middleware', () => {
expect(removeSubpath).toHaveBeenCalledTimes(1)
expect(next).toBeCalledTimes(1)
})

it('redirect to locale subpath when subpath was not present', () => {

const language = 'de'
const subpath = 'german'
req = {
url: `/page1`,
query: {},
i18n: {
options: {
localeSubpaths: {
[language]: subpath,
}
}
}
}

subpathIsRequired.mockReturnValue(true)
subpathIsPresent.mockReturnValue(false)
lngFromReq.mockReturnValue(language)
subpathFromLng.mockReturnValue(subpath)
addSubpath.mockReturnValue(`/${subpath}/page1`)

callAllMiddleware()

expect(req.url).toBe('/page1')
expect(req.query).toEqual({})

expect(removeSubpath).toHaveBeenCalledTimes(0)
expect(redirectWithoutCache).toHaveBeenCalledWith(res, '/german/page1')
expect(addSubpath).toHaveBeenCalledTimes(1)
expect(subpathIsRequired).toHaveBeenCalledTimes(1)
expect(subpathIsPresent).toHaveBeenCalledTimes(3)
expect(lngFromReq).toHaveBeenCalledTimes(1)
expect(subpathFromLng).toHaveBeenCalledTimes(3)
expect(removeSubpath).toHaveBeenCalledTimes(0)
expect(next).toBeCalledTimes(0)
})
})
})
3 changes: 1 addition & 2 deletions src/middlewares/next-i18next-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ export default function (nexti18next) {
If a language subpath is required and
not present, prepend correct subpath
*/
redirectWithoutCache(res, addSubpath(req.url, currentLngSubpath))

return redirectWithoutCache(res, addSubpath(req.url, currentLngSubpath))
}

}
Expand Down

0 comments on commit 6ce951b

Please sign in to comment.