Skip to content

Commit e336f1a

Browse files
committed
test: use webtorrent-fixtures
1 parent 1f4a68a commit e336f1a

File tree

10 files changed

+75
-56
lines changed

10 files changed

+75
-56
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var Client = require('bittorrent-tracker')
5252
var parseTorrent = require('parse-torrent')
5353
var fs = require('fs')
5454

55-
var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent')
55+
var torrent = fs.readFileSync(__dirname + '/torrents/name.torrent')
5656
var parsedTorrent = parseTorrent(torrent) // { infoHash: 'xxx', length: xx, announce: ['xx', 'xx'] }
5757

5858
var peerId = new Buffer('01234567890123456789')

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
4242
"xtend": "^4.0.0"
4343
},
4444
"devDependencies": {
45-
"magnet-uri": "^5.0.0",
46-
"parse-torrent": "^5.0.0",
45+
"magnet-uri": "^5.1.3",
4746
"standard": "^6.0.4",
48-
"tape": "^4.0.0"
47+
"tape": "^4.0.0",
48+
"webtorrent-fixtures": "^1.3.0"
4949
},
5050
"keywords": [
5151
"bittorrent",

test/client-large-torrent.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
var Client = require('../')
22
var common = require('./common')
3-
var fs = require('fs')
4-
var parseTorrent = require('parse-torrent')
5-
var path = require('path')
3+
var extend = require('xtend')
4+
var fixtures = require('webtorrent-fixtures')
65
var test = require('tape')
76

8-
var torrent = fs.readFileSync(path.join(__dirname, 'torrents/sintel-5gb.torrent'))
9-
var parsedTorrent = parseTorrent(torrent)
107
var peerId = new Buffer('01234567890123456789')
118

129
function testLargeTorrent (t, serverType) {
1310
t.plan(9)
1411

12+
var parsedTorrent = extend(fixtures.sintel.parsedTorrent)
13+
1514
common.createServer(t, serverType, function (server, announceUrl) {
1615
parsedTorrent.announce = [ announceUrl ]
1716
var client = new Client(peerId, 6881, parsedTorrent, { wrtc: {} })

test/client-magnet.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
var Client = require('../')
22
var common = require('./common')
3+
var fixtures = require('webtorrent-fixtures')
34
var magnet = require('magnet-uri')
45
var test = require('tape')
56

6-
var uri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub'
7-
var parsedTorrent = magnet(uri)
87
var peerId = new Buffer('01234567890123456789')
98

109
function testMagnet (t, serverType) {
1110
t.plan(9)
1211

12+
var parsedTorrent = magnet(fixtures.leaves.magnetURI)
13+
1314
common.createServer(t, serverType, function (server, announceUrl) {
1415
parsedTorrent.announce = [ announceUrl ]
1516

test/client.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
var Client = require('../')
22
var common = require('./common')
3-
var fs = require('fs')
4-
var parseTorrent = require('parse-torrent')
5-
var path = require('path')
3+
var extend = require('xtend')
4+
var fixtures = require('webtorrent-fixtures')
65
var test = require('tape')
76

8-
var torrent = fs.readFileSync(path.join(__dirname, 'torrents/bitlove-intro.torrent'))
9-
var parsedTorrent = parseTorrent(torrent)
107
var peerId1 = new Buffer('01234567890123456789')
118
var peerId2 = new Buffer('12345678901234567890')
129
var peerId3 = new Buffer('23456789012345678901')
1310
var port = 6881
1411

1512
function testClientStart (t, serverType) {
1613
t.plan(4)
14+
15+
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
16+
1717
common.createServer(t, serverType, function (server, announceUrl) {
1818
parsedTorrent.announce = [ announceUrl ]
1919
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
@@ -54,6 +54,9 @@ test('ws: client.start()', function (t) {
5454

5555
function testClientStop (t, serverType) {
5656
t.plan(3)
57+
58+
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
59+
5760
common.createServer(t, serverType, function (server, announceUrl) {
5861
parsedTorrent.announce = [ announceUrl ]
5962
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
@@ -94,6 +97,9 @@ test('ws: client.stop()', function (t) {
9497

9598
function testClientUpdate (t, serverType) {
9699
t.plan(4)
100+
101+
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
102+
97103
common.createServer(t, serverType, function (server, announceUrl) {
98104
parsedTorrent.announce = [ announceUrl ]
99105
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
@@ -141,6 +147,9 @@ test('ws: client.update()', function (t) {
141147

142148
function testClientScrape (t, serverType) {
143149
t.plan(4)
150+
151+
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
152+
144153
common.createServer(t, serverType, function (server, announceUrl) {
145154
parsedTorrent.announce = [ announceUrl ]
146155
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
@@ -178,6 +187,9 @@ test('udp: client.scrape()', function (t) {
178187

179188
function testClientAnnounceWithParams (t, serverType) {
180189
t.plan(5)
190+
191+
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
192+
181193
common.createServer(t, serverType, function (server, announceUrl) {
182194
parsedTorrent.announce = [ announceUrl ]
183195
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
@@ -220,6 +232,9 @@ test('ws: client.announce() with params', function (t) {
220232

221233
function testClientGetAnnounceOpts (t, serverType) {
222234
t.plan(5)
235+
236+
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
237+
223238
common.createServer(t, serverType, function (server, announceUrl) {
224239
parsedTorrent.announce = [ announceUrl ]
225240
var opts = {
@@ -268,6 +283,9 @@ test('ws: client `opts.getAnnounceOpts`', function (t) {
268283

269284
function testClientAnnounceWithNumWant (t, serverType) {
270285
t.plan(4)
286+
287+
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
288+
271289
common.createServer(t, serverType, function (server, announceUrl) {
272290
parsedTorrent.announce = [ announceUrl ]
273291
var client1 = new Client(peerId1, port, parsedTorrent, { wrtc: {} })

test/filter.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
var Client = require('../')
22
var common = require('./common')
3-
var fs = require('fs')
4-
var parseTorrent = require('parse-torrent')
5-
var path = require('path')
3+
var extend = require('xtend')
4+
var fixtures = require('webtorrent-fixtures')
65
var test = require('tape')
76

8-
var bitlove = fs.readFileSync(path.join(__dirname, 'torrents/bitlove-intro.torrent'))
9-
var parsedBitlove = parseTorrent(bitlove)
10-
11-
var leaves = fs.readFileSync(path.join(__dirname, 'torrents/leaves.torrent'))
12-
var parsedLeaves = parseTorrent(leaves)
13-
147
var peerId = new Buffer('01234567890123456789')
158

169
function testFilterOption (t, serverType) {
1710
t.plan(8)
1811

12+
var parsedAlice = extend(fixtures.alice.parsedTorrent)
13+
var parsedLeaves = extend(fixtures.leaves.parsedTorrent)
14+
1915
var opts = { serverType: serverType } // this is test-suite-only option
2016
opts.filter = function (infoHash, params, cb) {
2117
process.nextTick(function () {
22-
cb(infoHash !== parsedBitlove.infoHash)
18+
cb(infoHash !== parsedAlice.infoHash)
2319
})
2420
}
2521

2622
common.createServer(t, opts, function (server, announceUrl) {
27-
parsedBitlove.announce = [ announceUrl ]
23+
parsedAlice.announce = [ announceUrl ]
2824
parsedLeaves.announce = [ announceUrl ]
2925

30-
var client = new Client(peerId, 6881, parsedBitlove, { wrtc: {} })
26+
var client = new Client(peerId, 6881, parsedAlice, { wrtc: {} })
3127

3228
client.on('error', function (err) { t.error(err) })
3329
if (serverType === 'ws') common.mockWebsocketTracker(client)
@@ -82,25 +78,28 @@ test('ws: filter option blocks tracker from tracking torrent', function (t) {
8278
function testFilterCustomError (t, serverType) {
8379
t.plan(8)
8480

81+
var parsedLeaves = extend(fixtures.leaves.parsedTorrent)
82+
var parsedAlice = extend(fixtures.alice.parsedTorrent)
83+
8584
var opts = { serverType: serverType } // this is test-suite-only option
8685
opts.filter = function (infoHash, params, cb) {
8786
process.nextTick(function () {
88-
if (infoHash === parsedBitlove.infoHash) cb(new Error('bitlove blocked'))
87+
if (infoHash === parsedAlice.infoHash) cb(new Error('alice blocked'))
8988
else cb(true)
9089
})
9190
}
9291

9392
common.createServer(t, opts, function (server, announceUrl) {
94-
parsedBitlove.announce = [ announceUrl ]
93+
parsedAlice.announce = [ announceUrl ]
9594
parsedLeaves.announce = [ announceUrl ]
9695

97-
var client = new Client(peerId, 6881, parsedBitlove, { wrtc: {} })
96+
var client = new Client(peerId, 6881, parsedAlice, { wrtc: {} })
9897

9998
client.on('error', function (err) { t.error(err) })
10099
if (serverType === 'ws') common.mockWebsocketTracker(client)
101100

102101
client.once('warning', function (err) {
103-
t.ok(/bitlove blocked/.test(err.message), 'got client warning')
102+
t.ok(/alice blocked/.test(err.message), 'got client warning')
104103

105104
client.destroy(function () {
106105
t.pass('client destroyed')
@@ -126,7 +125,7 @@ function testFilterCustomError (t, serverType) {
126125

127126
server.removeAllListeners('warning')
128127
server.once('warning', function (err) {
129-
t.ok(/bitlove blocked/.test(err.message), 'got server warning')
128+
t.ok(/alice blocked/.test(err.message), 'got server warning')
130129
t.equal(Object.keys(server.torrents).length, 0)
131130
})
132131

test/scrape.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@ var bencode = require('bencode')
22
var Client = require('../')
33
var commonLib = require('../lib/common')
44
var commonTest = require('./common')
5-
var fs = require('fs')
5+
var extend = require('xtend')
6+
var fixtures = require('webtorrent-fixtures')
67
var get = require('simple-get')
7-
var parseTorrent = require('parse-torrent')
8-
var path = require('path')
98
var test = require('tape')
109

11-
var infoHash1 = 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa'
12-
var binaryInfoHash1 = commonLib.hexToBinary(infoHash1)
13-
var infoHash2 = 'bbb67059ed6bd08362da625b3ae77f6f4a075bbb'
14-
var binaryInfoHash2 = commonLib.hexToBinary(infoHash2)
15-
16-
var bitlove = fs.readFileSync(path.join(__dirname, 'torrents/bitlove-intro.torrent'))
17-
var parsedBitlove = parseTorrent(bitlove)
18-
var binaryBitlove = commonLib.hexToBinary(parsedBitlove.infoHash)
19-
2010
var peerId = new Buffer('01234567890123456789')
2111

2212
function testSingle (t, serverType) {
13+
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
14+
2315
commonTest.createServer(t, serverType, function (server, announceUrl) {
24-
parsedBitlove.announce = [ announceUrl ]
25-
var client = new Client(peerId, 6881, parsedBitlove)
16+
parsedTorrent.announce = [ announceUrl ]
17+
var client = new Client(peerId, 6881, parsedTorrent)
2618

2719
client.on('error', function (err) {
2820
t.error(err)
@@ -36,7 +28,7 @@ function testSingle (t, serverType) {
3628

3729
client.on('scrape', function (data) {
3830
t.equal(data.announce, announceUrl)
39-
t.equal(data.infoHash, parsedBitlove.infoHash)
31+
t.equal(data.infoHash, parsedTorrent.infoHash)
4032
t.equal(typeof data.complete, 'number')
4133
t.equal(typeof data.incomplete, 'number')
4234
t.equal(typeof data.downloaded, 'number')
@@ -58,10 +50,10 @@ test('udp: single info_hash scrape', function (t) {
5850

5951
function clientScrapeStatic (t, serverType) {
6052
commonTest.createServer(t, serverType, function (server, announceUrl) {
61-
Client.scrape(announceUrl, infoHash1, function (err, data) {
53+
Client.scrape(announceUrl, fixtures.leaves.parsedTorrent.infoHash, function (err, data) {
6254
t.error(err)
6355
t.equal(data.announce, announceUrl)
64-
t.equal(data.infoHash, infoHash1)
56+
t.equal(data.infoHash, fixtures.leaves.parsedTorrent.infoHash)
6557
t.equal(typeof data.complete, 'number')
6658
t.equal(typeof data.incomplete, 'number')
6759
t.equal(typeof data.downloaded, 'number')
@@ -81,6 +73,9 @@ test('udp: scrape using Client.scrape static method', function (t) {
8173
})
8274

8375
function clientScrapeMulti (t, serverType) {
76+
var infoHash1 = fixtures.leaves.parsedTorrent.infoHash
77+
var infoHash2 = fixtures.alice.parsedTorrent.infoHash
78+
8479
commonTest.createServer(t, serverType, function (server, announceUrl) {
8580
Client.scrape(announceUrl, [ infoHash1, infoHash2 ], function (err, results) {
8681
t.error(err)
@@ -115,6 +110,9 @@ test('udp: MULTI scrape using Client.scrape static method', function (t) {
115110
test('server: multiple info_hash scrape (manual http request)', function (t) {
116111
t.plan(13)
117112

113+
var binaryInfoHash1 = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash)
114+
var binaryInfoHash2 = commonLib.hexToBinary(fixtures.alice.parsedTorrent.infoHash)
115+
118116
commonTest.createServer(t, 'http', function (server, announceUrl) {
119117
var scrapeUrl = announceUrl.replace('/announce', '/scrape')
120118

@@ -148,13 +146,17 @@ test('server: multiple info_hash scrape (manual http request)', function (t) {
148146

149147
test('server: all info_hash scrape (manual http request)', function (t) {
150148
t.plan(10)
149+
150+
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
151+
var binaryInfoHash = commonLib.hexToBinary(parsedTorrent.infoHash)
152+
151153
commonTest.createServer(t, 'http', function (server, announceUrl) {
152154
var scrapeUrl = announceUrl.replace('/announce', '/scrape')
153155

154-
parsedBitlove.announce = [ announceUrl ]
156+
parsedTorrent.announce = [ announceUrl ]
155157

156158
// announce a torrent to the tracker
157-
var client = new Client(peerId, 6881, parsedBitlove)
159+
var client = new Client(peerId, 6881, parsedTorrent)
158160
client.on('error', function (err) { t.error(err) })
159161
client.on('warning', function (err) { t.error(err) })
160162

@@ -170,10 +172,10 @@ test('server: all info_hash scrape (manual http request)', function (t) {
170172
t.ok(data.files)
171173
t.equal(Object.keys(data.files).length, 1)
172174

173-
t.ok(data.files[binaryBitlove])
174-
t.equal(typeof data.files[binaryBitlove].complete, 'number')
175-
t.equal(typeof data.files[binaryBitlove].incomplete, 'number')
176-
t.equal(typeof data.files[binaryBitlove].downloaded, 'number')
175+
t.ok(data.files[binaryInfoHash])
176+
t.equal(typeof data.files[binaryInfoHash].complete, 'number')
177+
t.equal(typeof data.files[binaryInfoHash].incomplete, 'number')
178+
t.equal(typeof data.files[binaryInfoHash].downloaded, 'number')
177179

178180
client.destroy(function () { t.pass('client destroyed') })
179181
server.close(function () { t.pass('server closed') })
-597 Bytes
Binary file not shown.

test/torrents/leaves.torrent

-639 Bytes
Binary file not shown.

test/torrents/sintel-5gb.torrent

-25.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)