-
Notifications
You must be signed in to change notification settings - Fork 399
Fix redirect loop #2066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix redirect loop #2066
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,6 +225,13 @@ describe('Trailing Slashes Middleware', () => { | |
}; | ||
fakeConfig = new Map(); | ||
fakeConfig.set('enableTrailingSlashesMiddleware', true); | ||
fakeConfig.set('validClientApplications', ['firefox']); | ||
fakeConfig.set('validTrailingSlashUrlExceptions', [ | ||
'/$lang/lack/trailing', | ||
'/$clientApp/none/trailing', | ||
'/$lang/$clientApp/slash/trailing', | ||
'/no/trailing', | ||
]); | ||
}); | ||
|
||
it('should call next and do nothing with a valid, trailing slash URL', () => { | ||
|
@@ -248,6 +255,121 @@ describe('Trailing Slashes Middleware', () => { | |
assert.notOk(fakeNext.called); | ||
}); | ||
|
||
it('should not add trailing slashes if the URL has an exception', () => { | ||
const fakeReq = { | ||
originalUrl: '/no/trailing', | ||
headers: {}, | ||
}; | ||
trailingSlashesMiddleware( | ||
fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); | ||
assert.notOk(fakeRes.redirect.called); | ||
assert.ok(fakeNext.called); | ||
}); | ||
|
||
it('should handle trailing slash exceptions with $lang', () => { | ||
const fakeReq = { | ||
originalUrl: '/en-US/lack/trailing', | ||
headers: {}, | ||
}; | ||
trailingSlashesMiddleware( | ||
fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); | ||
assert.notOk(fakeRes.redirect.called); | ||
assert.ok(fakeNext.called); | ||
}); | ||
|
||
it('should handle trailing slash exceptions with $clientApp', () => { | ||
const fakeReq = { | ||
originalUrl: '/firefox/none/trailing', | ||
headers: {}, | ||
}; | ||
trailingSlashesMiddleware( | ||
fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); | ||
assert.notOk(fakeRes.redirect.called); | ||
assert.ok(fakeNext.called); | ||
}); | ||
|
||
it('should handle trailing slash exceptions with $lang/$clientApp', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may have missed it, but it would be good to ensure that clientApp and or locale prefixed urls still get redirs if not excluded. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I didn't actually test that but I can. Sure. Edit: as in: they do but I didn't write a test for it. |
||
const fakeReq = { | ||
originalUrl: '/fr/firefox/slash/trailing', | ||
headers: {}, | ||
}; | ||
trailingSlashesMiddleware( | ||
fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); | ||
assert.notOk(fakeRes.redirect.called); | ||
assert.ok(fakeNext.called); | ||
}); | ||
|
||
it('should not be an exception without $lang/$clientApp', () => { | ||
const fakeReq = { | ||
originalUrl: '/slash/trailing', | ||
headers: {}, | ||
}; | ||
trailingSlashesMiddleware( | ||
fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); | ||
assert.deepEqual(fakeRes.redirect.firstCall.args, | ||
[301, '/slash/trailing/']); | ||
assert.notOk(fakeNext.called); | ||
}); | ||
|
||
it('should not be an exception without both $lang/$clientApp', () => { | ||
const fakeReq = { | ||
originalUrl: '/en-US/slash/trailing', | ||
headers: {}, | ||
}; | ||
trailingSlashesMiddleware( | ||
fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); | ||
assert.deepEqual(fakeRes.redirect.firstCall.args, | ||
[301, '/en-US/slash/trailing/']); | ||
assert.notOk(fakeNext.called); | ||
}); | ||
|
||
it('redirects a URL with $lang and $clientApp without an exception', () => { | ||
const fakeReq = { | ||
originalUrl: '/en-US/firefox/trailing', | ||
headers: {}, | ||
}; | ||
trailingSlashesMiddleware( | ||
fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); | ||
assert.deepEqual(fakeRes.redirect.firstCall.args, | ||
[301, '/en-US/firefox/trailing/']); | ||
assert.notOk(fakeNext.called); | ||
}); | ||
|
||
it('detects an exception that has a query string', () => { | ||
const fakeReq = { | ||
originalUrl: '/no/trailing?query=test', | ||
headers: {}, | ||
}; | ||
trailingSlashesMiddleware( | ||
fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); | ||
assert.notOk(fakeRes.redirect.called); | ||
assert.ok(fakeNext.called); | ||
}); | ||
|
||
it('does not remove query string', () => { | ||
const fakeReq = { | ||
originalUrl: '/hello?query=test', | ||
headers: {}, | ||
}; | ||
trailingSlashesMiddleware( | ||
fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); | ||
assert.deepEqual(fakeRes.redirect.firstCall.args, | ||
[301, '/hello/?query=test']); | ||
assert.notOk(fakeNext.called); | ||
}); | ||
|
||
it('should not be an exception without both $lang/$clientApp', () => { | ||
const fakeReq = { | ||
originalUrl: '/firefox/slash/trailing', | ||
headers: {}, | ||
}; | ||
trailingSlashesMiddleware( | ||
fakeReq, fakeRes, fakeNext, { _config: fakeConfig }); | ||
assert.deepEqual(fakeRes.redirect.firstCall.args, | ||
[301, '/firefox/slash/trailing/']); | ||
assert.notOk(fakeNext.called); | ||
}); | ||
|
||
it('should include query params in the redirect', () => { | ||
const fakeReq = { | ||
originalUrl: '/foo/search?q=foo&category=bar', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be indented?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it breaks 80 chars if not.