Skip to content
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

Force HTTP check under corresponding protocols #56

Merged
merged 1 commit into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ const checkHttp = async (url, timeout) => {
const getAddress = async hostname => net.isIP(hostname) ? hostname : (await dnsLookupP(hostname)).address;

const isTargetReachable = timeout => async target => {
const isHTTP = target.startsWith('https://') || target.startsWith('http://');
const url = new URL(prependHttp(target));

if (!url.port) {
url.port = url.protocol === 'http:' ? 80 : 443;
if (!url.port && !isHTTP) {
url.port = 443;
}

let address;
Expand All @@ -55,7 +56,7 @@ const isTargetReachable = timeout => async target => {
return false;
}

if ([80, 443].includes(url.port)) {
if (isHTTP) {
return checkHttp(url.toString(), timeout);
}

Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {promisify} from 'util';
import dns from 'dns';
import http from 'http';
import test from 'ava';
import isReachable from '.';

Expand All @@ -26,6 +27,18 @@ test('multiple https urls', async t => {
t.true(await isReachable(['https://google.com', 'https://baidu.com']));
});

test('http server on custom port', async t => {
const server = http.createServer((_, response) => {
response.writeHead(200).end();
}).listen(8080);
t.true(await isReachable('http://localhost:8080'));
server.close();
});

test('unreachable http server on custom port', async t => {
t.false(await isReachable('http://localhost:8081'));
});

test('imap host and port', async t => {
t.true(await isReachable('imap.gmail.com:995'));
});
Expand Down