This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
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.
fix(pretty-urls): Limiting pretty URLs for trailing index URL segment
For the sake of more wide deployment support, we limit/restrict the pretty URL support for trailing "index" URL segments only. Before it was not restricted to this, means: "foo.com/mypage.html" was transformed "foo.com/mypage". This case is taken out now and will stay like "foo.com/mypage.html". Fixes #224
- Loading branch information
Showing
5 changed files
with
81 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
module.exports = { _: [], p: true, pretty: true }; | ||
module.exports = { _: [] }; |
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,44 +1,73 @@ | ||
const { isAbsoluteURL, generateURL } = require('../url'); | ||
const { isAbsoluteURL, generateURL } = require("../url"); | ||
|
||
jest.mock('../args'); | ||
jest.mock("../args"); | ||
|
||
describe('isAbsoluteURL', () => { | ||
test('it fails if url is not given', () => { | ||
describe("isAbsoluteURL", () => { | ||
test("it fails if url is not given", () => { | ||
expect(() => isAbsoluteURL()).toThrow(); | ||
}); | ||
|
||
test('it returns true if url is absolute', () => { | ||
expect(isAbsoluteURL('https://www.google.com')).toBe(true); | ||
test("it returns true if url is absolute", () => { | ||
expect(isAbsoluteURL("https://www.google.com")).toBe(true); | ||
}); | ||
|
||
test('it returns false if url is relative', () => { | ||
expect(isAbsoluteURL('../foo')).toBe(false); | ||
test("it returns false if url is relative", () => { | ||
expect(isAbsoluteURL("../foo")).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("generateURL", () => { | ||
// Restore original env vars | ||
// NOTE: https://stackoverflow.com/a/48042799/778340 | ||
const OLD_ENV = process.env; | ||
|
||
describe('generateURL', () => { | ||
test('it fails if url is not given or not valid', () => { | ||
expect(() => generateURL()).toThrow(); | ||
}) | ||
beforeEach(() => { | ||
jest.resetModules(); // most important - it clears the cache | ||
process.env = { ...OLD_ENV }; // make a copy | ||
}); | ||
|
||
test('pathname', () => { | ||
expect(generateURL('http://www.google.de').pathname).toBe('/'); | ||
expect(generateURL('http://www.google.de/foo').pathname).toBe('/foo'); | ||
}) | ||
afterAll(() => { | ||
process.env = OLD_ENV; // restore old env | ||
}); | ||
|
||
test('href', () => { | ||
expect(generateURL('http://www.google.de').href).toBe( | ||
'http://www.google.de/' | ||
); | ||
test("it fails if url is not given or not valid", () => { | ||
expect(() => generateURL()).toThrow(); | ||
}); | ||
|
||
expect(generateURL('http://www.google.de/foo?myquery=2').href).toBe( | ||
'http://www.google.de/foo?myquery=2' | ||
); | ||
}) | ||
test("pathname", () => { | ||
expect(generateURL("http://www.google.de").pathname).toBe("/"); | ||
expect(generateURL("http://www.google.de/foo").pathname).toBe("/foo"); | ||
}); | ||
|
||
test("href", () => { | ||
expect(generateURL("http://www.google.de").href).toBe( | ||
"http://www.google.de/" | ||
); | ||
|
||
expect(generateURL("http://www.google.de/foo?myquery=2").href).toBe( | ||
"http://www.google.de/foo?myquery=2" | ||
); | ||
}); | ||
|
||
test('Removes /index on prettyfied mode', () => { | ||
process.env.URL = 'http://foo.de'; | ||
expect(generateURL('subfolder/index').href).toBe('http://foo.de/subfolder') | ||
}) | ||
}) | ||
test("Removes /index on pretty mode", () => { | ||
process.env.PRETTY = "true"; | ||
process.env.URL = "http://foo.de"; | ||
expect(generateURL("subfolder/index").href).toBe("http://foo.de/subfolder"); | ||
}); | ||
|
||
test("Does not remove /index.html on non-pretty mode", () => { | ||
process.env.PRETTY = "false"; | ||
process.env.URL = "http://foo.de"; | ||
expect(generateURL("subfolder/index").href).toBe( | ||
"http://foo.de/subfolder/index.html" | ||
); | ||
}); | ||
|
||
test("Does not remove trailing segments other than index on pretty mode", () => { | ||
process.env.PRETTY = "true"; | ||
process.env.URL = "http://foo.de"; | ||
expect(generateURL("subfolder/impressum").href).toBe( | ||
"http://foo.de/subfolder/impressum.html" | ||
); | ||
}); | ||
}); |
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