-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bad routing regex for docs & blogs (#795)
* fix bad routing regex for docs & blogs * extract to routing.js & add test * add more test case * address code review * prettier
- Loading branch information
Showing
3 changed files
with
86 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
const {docsRouting, blogRouting} = require('../routing'); | ||
|
||
describe('Blog routing', () => { | ||
const blogRegex = blogRouting('/'); | ||
const blogRegex2 = blogRouting('/react/'); | ||
|
||
test('valid blog', () => { | ||
expect('/blog/test.html').toMatch(blogRegex); | ||
expect('/react/blog/test.html').toMatch(blogRegex2); | ||
}); | ||
|
||
test('invalid blog', () => { | ||
expect('/react/blog/test.html').not.toMatch(blogRegex); | ||
expect('/blog/test.html').not.toMatch(blogRegex2); | ||
}); | ||
|
||
test('assets not classified as blog', () => { | ||
expect('/blog/assets/any.png').not.toMatch(blogRegex); | ||
expect('/react/blog/assets/any.png').not.toMatch(blogRegex2); | ||
}); | ||
|
||
test('docs not classified as blog', () => { | ||
expect('/docs/en/blog.html').not.toMatch(blogRegex); | ||
expect('/docs/en/blog/blog.html').not.toMatch(blogRegex); | ||
expect('/react/docs/en/blog.html').not.toMatch(blogRegex2); | ||
expect('/react/docs/en/blog/blog.html').not.toMatch(blogRegex2); | ||
}); | ||
}); | ||
|
||
describe('Docs routing', () => { | ||
const docsRegex = docsRouting('/'); | ||
const docsRegex2 = docsRouting('/reason/'); | ||
|
||
test('valid docs', () => { | ||
expect('/docs/en/test.html').toMatch(docsRegex); | ||
expect('/reason/docs/en/test.html').toMatch(docsRegex2); | ||
}); | ||
|
||
test('invalid docs', () => { | ||
expect('/reason/docs/en/test.html').not.toMatch(docsRegex); | ||
expect('/docs/en/test.html').not.toMatch(docsRegex2); | ||
}); | ||
|
||
test('assets not classified as docs', () => { | ||
expect('/docs/en/notvalid.png').not.toMatch(docsRegex); | ||
expect('/reason/docs/en/notvalid.png').not.toMatch(docsRegex2); | ||
}); | ||
|
||
test('blog not classified as docs', () => { | ||
expect('/blog/docs.html').not.toMatch(docsRegex); | ||
expect('/blog/docs/docs.html').not.toMatch(docsRegex); | ||
expect('/reason/blog/docs.html').not.toMatch(docsRegex2); | ||
expect('/reason/blog/docs/docs.html').not.toMatch(docsRegex2); | ||
}); | ||
}); |
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,20 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
const escapeStringRegexp = require('escape-string-regexp'); | ||
|
||
function docsRouting(baseUrl) { | ||
return new RegExp(`^${escapeStringRegexp(baseUrl)}docs\/.*html$`); | ||
} | ||
|
||
function blogRouting(baseUrl) { | ||
return new RegExp(`^${escapeStringRegexp(baseUrl)}blog\/.*html$`); | ||
} | ||
|
||
module.exports = { | ||
docsRouting, | ||
blogRouting, | ||
}; |
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