diff options
author | ThaUnknown <6506529+ThaUnknown@users.noreply.github.com> | 2022-06-26 15:29:56 +0300 |
---|---|---|
committer | ThaUnknown <6506529+ThaUnknown@users.noreply.github.com> | 2022-08-30 15:58:18 +0300 |
commit | a278102732ee041e1a92af859b5e3cc21179d15c (patch) | |
tree | b052c9a94847a599546e7f1ce154aa941e361be6 | |
parent | 7aeea1757000741a04409dadeaf9fab3966b399d (diff) |
update tests, minor fixes
-rw-r--r-- | lib/server.js | 14 | ||||
-rw-r--r-- | test/node/server.js | 11 |
2 files changed, 15 insertions, 10 deletions
diff --git a/lib/server.js b/lib/server.js index 7b980e4..83653f8 100644 --- a/lib/server.js +++ b/lib/server.js @@ -77,7 +77,7 @@ class ServerBase { const listHtml = torrent.files .map(file => ( `<li> - <a href="${torrent.infoHash}/${escapeHtml(file.path)}"> + <a href="./${escapeHtml(file.path)}"> ${escapeHtml(file.path)} </a> (${escapeHtml(file.length)} bytes) @@ -217,7 +217,7 @@ class ServerBase { return ServerBase.serveTorrentPage(torrent, res) } - const file = torrent.files.find(file => file.path.replace(/\\/, '/') === filePath) + const file = torrent.files.find(file => file.path.replace(/\\/g, '/') === filePath) if (!file) { return ServerBase.serve404Page(res) } @@ -259,6 +259,10 @@ class NodeServer extends ServerBase { super(client, opts) this.server = http.createServer() + this._listen = this.server.listen + this.server.listen = this.listen.bind(this) + this._close = this.server.close + this.server.close = this.close.bind(this) this.sockets = new Set() this.pendingReady = new Set() @@ -301,14 +305,14 @@ class NodeServer extends ServerBase { this.closed = false this.server.on('connection', this.onConnection.bind(this)) this.server.on('request', this.wrapRequest.bind(this)) - return this.server.listen(...args) + return this._listen.apply(this.server, args) } - close (cb) { + close (cb = () => {}) { this.server.removeListener('connection', this.onConnection) this.server.removeListener('request', this.wrapRequest) - this.server.close(cb) super.close() + this._close.call(this.server, cb) } destroy (cb) { diff --git a/test/node/server.js b/test/node/server.js index 786a7cf..4df57e6 100644 --- a/test/node/server.js +++ b/test/node/server.js @@ -14,7 +14,7 @@ test('torrent.createServer: programmatic http server', t => { client.add(fixtures.leaves.torrent, torrent => { t.pass('got "torrent" event') - const server = torrent.createServer() + const { server } = client.createServer() server.listen(0, () => { const port = server.address().port @@ -39,16 +39,17 @@ test('torrent.createServer: programmatic http server', t => { }) const host = `http://localhost:${port}` + const path = `webtorrent/${torrent.infoHash}` // Index page should list files in the torrent - get.concat(`${host}/`, (err, res, data) => { - t.error(err, 'got http response for /') + get.concat(`${host}/${path}/`, (err, res, data) => { + t.error(err, `got http response for /${path}`) data = data.toString() t.ok(data.includes('Leaves of Grass by Walt Whitman.epub')) // Verify file content for first (and only) file - get.concat(`${host}/0`, (err, res, data) => { - t.error(err, 'got http response for /0') + get.concat(`${host}/${path}/${torrent.files[0].path}`, (err, res, data) => { + t.error(err, `got http response for /${path}/${torrent.files[0].path}`) t.deepEqual(data, fixtures.leaves.content) close() |