|
| 1 | +var Buffer = require('safe-buffer').Buffer |
| 2 | +var Client = require('../') |
| 3 | +var commonTest = require('./common') |
| 4 | +var fixtures = require('webtorrent-fixtures') |
| 5 | +var get = require('simple-get') |
| 6 | +var test = require('tape') |
| 7 | + |
| 8 | +var peerId = Buffer.from('01234567890123456789') |
| 9 | + |
| 10 | +function parseHtml (html) { |
| 11 | + var extractValue = new RegExp('[^v^h](\\d+)') |
| 12 | + var array = html.replace('torrents', '\n').split('\n').filter(function (line) { |
| 13 | + return line && line.trim().length > 0 |
| 14 | + }).map(function (line) { |
| 15 | + var a = extractValue.exec(line) |
| 16 | + return parseInt(a[1]) |
| 17 | + }) |
| 18 | + var i = 0 |
| 19 | + return { |
| 20 | + torrents: array[i++], |
| 21 | + activeTorrents: array[i++], |
| 22 | + peersAll: array[i++], |
| 23 | + peersSeederOnly: array[i++], |
| 24 | + peersLeecherOnly: array[i++], |
| 25 | + peersSeederAndLeecher: array[i++], |
| 26 | + peersIPv4: array[i++], |
| 27 | + peersIPv6: array[i] |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +test('server: get empty stats', function (t) { |
| 32 | + t.plan(11) |
| 33 | + |
| 34 | + commonTest.createServer(t, 'http', function (server, announceUrl) { |
| 35 | + var url = announceUrl.replace('/announce', '/stats') |
| 36 | + |
| 37 | + get.concat(url, function (err, res, data) { |
| 38 | + t.error(err) |
| 39 | + |
| 40 | + var stats = parseHtml(data.toString()) |
| 41 | + t.equal(res.statusCode, 200) |
| 42 | + t.equal(stats.torrents, 0) |
| 43 | + t.equal(stats.activeTorrents, 0) |
| 44 | + t.equal(stats.peersAll, 0) |
| 45 | + t.equal(stats.peersSeederOnly, 0) |
| 46 | + t.equal(stats.peersLeecherOnly, 0) |
| 47 | + t.equal(stats.peersSeederAndLeecher, 0) |
| 48 | + t.equal(stats.peersIPv4, 0) |
| 49 | + t.equal(stats.peersIPv6, 0) |
| 50 | + |
| 51 | + server.close(function () { t.pass('server closed') }) |
| 52 | + }) |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +test('server: get empty stats with json header', function (t) { |
| 57 | + t.plan(11) |
| 58 | + |
| 59 | + commonTest.createServer(t, 'http', function (server, announceUrl) { |
| 60 | + var opts = { |
| 61 | + url: announceUrl.replace('/announce', '/stats'), |
| 62 | + headers: { |
| 63 | + 'content-type': 'json' |
| 64 | + }, |
| 65 | + json: true |
| 66 | + } |
| 67 | + |
| 68 | + get.concat(opts, function (err, res, stats) { |
| 69 | + t.error(err) |
| 70 | + |
| 71 | + t.equal(res.statusCode, 200) |
| 72 | + t.equal(stats.torrents, 0) |
| 73 | + t.equal(stats.activeTorrents, 0) |
| 74 | + t.equal(stats.peersAll, 0) |
| 75 | + t.equal(stats.peersSeederOnly, 0) |
| 76 | + t.equal(stats.peersLeecherOnly, 0) |
| 77 | + t.equal(stats.peersSeederAndLeecher, 0) |
| 78 | + t.equal(stats.peersIPv4, 0) |
| 79 | + t.equal(stats.peersIPv6, 0) |
| 80 | + |
| 81 | + server.close(function () { t.pass('server closed') }) |
| 82 | + }) |
| 83 | + }) |
| 84 | +}) |
| 85 | + |
| 86 | +test('server: get empty stats on stats.json', function (t) { |
| 87 | + t.plan(11) |
| 88 | + |
| 89 | + commonTest.createServer(t, 'http', function (server, announceUrl) { |
| 90 | + var opts = { |
| 91 | + url: announceUrl.replace('/announce', '/stats.json'), |
| 92 | + json: true |
| 93 | + } |
| 94 | + |
| 95 | + get.concat(opts, function (err, res, stats) { |
| 96 | + t.error(err) |
| 97 | + |
| 98 | + t.equal(res.statusCode, 200) |
| 99 | + t.equal(stats.torrents, 0) |
| 100 | + t.equal(stats.activeTorrents, 0) |
| 101 | + t.equal(stats.peersAll, 0) |
| 102 | + t.equal(stats.peersSeederOnly, 0) |
| 103 | + t.equal(stats.peersLeecherOnly, 0) |
| 104 | + t.equal(stats.peersSeederAndLeecher, 0) |
| 105 | + t.equal(stats.peersIPv4, 0) |
| 106 | + t.equal(stats.peersIPv6, 0) |
| 107 | + |
| 108 | + server.close(function () { t.pass('server closed') }) |
| 109 | + }) |
| 110 | + }) |
| 111 | +}) |
| 112 | + |
| 113 | +test('server: get leecher stats.json', function (t) { |
| 114 | + t.plan(10) |
| 115 | + |
| 116 | + commonTest.createServer(t, 'http', function (server, announceUrl) { |
| 117 | + // announce a torrent to the tracker |
| 118 | + var client = new Client({ |
| 119 | + infoHash: fixtures.leaves.parsedTorrent.infoHash, |
| 120 | + announce: announceUrl, |
| 121 | + peerId: peerId, |
| 122 | + port: 6881 |
| 123 | + }) |
| 124 | + client.on('error', function (err) { t.error(err) }) |
| 125 | + client.on('warning', function (err) { t.error(err) }) |
| 126 | + |
| 127 | + client.start() |
| 128 | + |
| 129 | + server.once('start', function () { |
| 130 | + var opts = { |
| 131 | + url: announceUrl.replace('/announce', '/stats.json'), |
| 132 | + json: true |
| 133 | + } |
| 134 | + |
| 135 | + get.concat(opts, function (err, res, stats) { |
| 136 | + t.error(err) |
| 137 | + console.log(stats) |
| 138 | + |
| 139 | + t.equal(res.statusCode, 200) |
| 140 | + t.equal(stats.torrents, 1) |
| 141 | + t.equal(stats.activeTorrents, 1) |
| 142 | + t.equal(stats.peersAll, 1) |
| 143 | + t.equal(stats.peersSeederOnly, 0) |
| 144 | + t.equal(stats.peersLeecherOnly, 1) |
| 145 | + t.equal(stats.peersSeederAndLeecher, 0) |
| 146 | + |
| 147 | + client.destroy(function () { t.pass('client destroyed') }) |
| 148 | + server.close(function () { t.pass('server closed') }) |
| 149 | + }) |
| 150 | + }) |
| 151 | + }) |
| 152 | +}) |
0 commit comments