fetch-dht/integration.js

51 lines
1.2 KiB
JavaScript

const assert = require('assert');
const fetch = require('node-fetch');
const infoHash = '(m.%5bO%83i%85S(3j%c1%26%3a%e0*z%60%d5';
(async () => {
if (!process.env.ENDPOINT) {
throw new Error('Endpoint must be defined.');
}
console.log(`testing ${process.env.ENDPOINT} with infoHash '${infoHash}'`);
const res = await fetch(
`${process.env.ENDPOINT}/fetch?info_hash=${infoHash}&json=1`,
);
assert.deepEqual(res.status, 200, 'expected response to be ok');
const data = await res.json();
assert.deepEqual(
typeof data,
'object',
'expected response to be an array (is not an object)',
);
assert.ok(
data instanceof Array,
'expected response to be an array (is not an object)',
);
assert.ok(data.length > 0, 'expected popular torrent to have peers');
data.forEach((peer) => {
assert.deepEqual(
typeof peer,
'string',
'expected every response entry to be a string',
);
assert.match(
peer,
/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:[0-9]{1,5}/,
'expected every response entry to resemble ip:port',
);
});
})().then(
() => {
console.log('ok!');
process.exit(0);
},
(e) => {
console.log('error!');
console.log(e);
process.exit(1);
},
);