forked from GoogleChrome/web.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pass-through query string on redirect (GoogleChrome#3090)
* expand redirect-handler * express .query has weird parsing rules * comment about why we don't use query * add tests
- Loading branch information
Showing
4 changed files
with
185 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
describe('Unit tests', function () { | ||
require('./src'); | ||
require('./redirect-handler'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @fileoverview Tests for the redirect-handler used by the server. | ||
*/ | ||
|
||
const assert = require('assert'); | ||
const { | ||
baseUrlPrefix, | ||
ensureTrailingSlashOnly, | ||
prepareHandler, | ||
} = require('../../redirect-handler'); | ||
|
||
const sharedYamlSource = ` | ||
redirects: | ||
- from: /subscribe | ||
to: /newsletter | ||
- from: /subscribe/all/... | ||
to: /newsletter | ||
- from: /foo/... | ||
to: /bar/... | ||
- from: /external/... | ||
to: https://google.com/... | ||
`; | ||
|
||
describe('redirect-handler', function () { | ||
it('normalizes paths', function () { | ||
assert.equal(ensureTrailingSlashOnly('/foo/index.html'), '/foo/'); | ||
assert.equal( | ||
ensureTrailingSlashOnly('/foo/index.HTML'), | ||
'/foo/index.HTML/', | ||
'must only match lower-case index.html', | ||
); | ||
assert.strictEqual(ensureTrailingSlashOnly(''), '/'); | ||
}); | ||
|
||
it('handles simple redirects', function () { | ||
const h = prepareHandler(sharedYamlSource, 'https://web.dev'); | ||
|
||
assert.strictEqual(h('/subscribe'), '/newsletter'); | ||
assert.strictEqual( | ||
h('/subscribe/index.html'), | ||
'/newsletter', | ||
'trailing index.html is ignored', | ||
); | ||
assert.strictEqual( | ||
h('/subscribe/other.html'), | ||
null, | ||
'unhandled URL returns null', | ||
); | ||
}); | ||
|
||
it('handles group redirects', function () { | ||
const h = prepareHandler(sharedYamlSource, 'https://web.dev'); | ||
|
||
assert.strictEqual( | ||
h('/subscribe/all/foo'), | ||
'/newsletter', | ||
'group => non-group redirect', | ||
); | ||
assert.strictEqual( | ||
h('/foo/x'), | ||
'/bar/x/', | ||
'group redirect functions, trailing slash added', | ||
); | ||
assert.strictEqual( | ||
h('/foo/x/index.html'), | ||
'/bar/x/', | ||
'index.html is stripped', | ||
); | ||
assert.strictEqual( | ||
h('/external/hello'), | ||
'https://google.com/hello/', | ||
'external redirect is also normalized', | ||
); | ||
assert.strictEqual(h('/external/'), 'https://google.com/'); | ||
assert.strictEqual( | ||
h('/external'), | ||
'https://google.com/', | ||
'matches without trailing slash', | ||
); | ||
}); | ||
|
||
it('includes query strings', function () { | ||
const h = prepareHandler(sharedYamlSource, 'https://web.dev'); | ||
|
||
assert.strictEqual(h('/subscribe/?foo'), '/newsletter?foo='); | ||
assert.strictEqual(h('/subscribe/?foo=1&foo=2'), '/newsletter?foo=1&foo=2'); | ||
}); | ||
|
||
it('has the expected baseUrl', function () { | ||
assert.strictEqual(baseUrlPrefix, 'https://web.dev/'); | ||
}); | ||
}); |