handle localhost urls with path
This commit is contained in:
parent
f3ce1c300a
commit
ddd3b08a4a
2 changed files with 22 additions and 5 deletions
|
@ -16,6 +16,20 @@ const isLocalhost = (url: string): boolean => {
|
||||||
return host === 'localhost' && !isNaN(Number(port));
|
return host === 'localhost' && !isNaN(Number(port));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const isMissingHttp = (keywords: string): boolean => {
|
||||||
|
if (keywords.includes('.') && !keywords.includes(' ')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
let u = new URL('http://' + keywords);
|
||||||
|
return isLocalhost(u.host)
|
||||||
|
} catch (e) {
|
||||||
|
// fallthrough
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
const searchUrl = (keywords: string, search: Search): string => {
|
const searchUrl = (keywords: string, search: Search): string => {
|
||||||
try {
|
try {
|
||||||
let u = new URL(keywords);
|
let u = new URL(keywords);
|
||||||
|
@ -25,10 +39,11 @@ const searchUrl = (keywords: string, search: Search): string => {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// fallthrough
|
// fallthrough
|
||||||
}
|
}
|
||||||
if (isLocalhost(keywords) ||
|
|
||||||
(keywords.includes('.') && !keywords.includes(' '))) {
|
if (isMissingHttp(keywords)) {
|
||||||
return 'http://' + keywords;
|
return 'http://' + keywords;
|
||||||
}
|
}
|
||||||
|
|
||||||
let template = search.engines[search.defaultEngine];
|
let template = search.engines[search.defaultEngine];
|
||||||
let query = keywords;
|
let query = keywords;
|
||||||
|
|
||||||
|
|
|
@ -41,13 +41,15 @@ describe("shared/commands/parsers", () => {
|
||||||
expect(parsers.searchUrl('localhost', config))
|
expect(parsers.searchUrl('localhost', config))
|
||||||
.to.equal('http://localhost');
|
.to.equal('http://localhost');
|
||||||
expect(parsers.searchUrl('http://localhost', config))
|
expect(parsers.searchUrl('http://localhost', config))
|
||||||
.to.equal('http://localhost');
|
.to.equal('http://localhost/');
|
||||||
expect(parsers.searchUrl('localhost:8080', config))
|
expect(parsers.searchUrl('localhost:8080', config))
|
||||||
.to.equal('http://locahost:8080');
|
.to.equal('http://localhost:8080');
|
||||||
expect(parsers.searchUrl('localhost:80nan', config))
|
expect(parsers.searchUrl('localhost:80nan', config))
|
||||||
.to.equal('https://google.com/search?q=localhost:80nan');
|
.to.equal('https://google.com/search?q=localhost%3A80nan');
|
||||||
expect(parsers.searchUrl('localhost 8080', config))
|
expect(parsers.searchUrl('localhost 8080', config))
|
||||||
.to.equal('https://google.com/search?q=localhost%208080');
|
.to.equal('https://google.com/search?q=localhost%208080');
|
||||||
|
expect(parsers.searchUrl('localhost:80/build', config))
|
||||||
|
.to.equal('http://localhost:80/build')
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Reference in a new issue