Welcome to mirror list, hosted at ThFree Co, Russian Federation.

basic.js « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b44c4661fce7f7c3c10311fa9ee044d386c49e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var fs = require('fs')
var extend = require('xtend')
var parseTorrent = require('parse-torrent')
var test = require('tape')
var WebTorrent = require('../')

var leaves = fs.readFileSync(__dirname + '/torrents/leaves.torrent')
var leavesTorrent = parseTorrent(leaves)
var leavesBook = fs.readFileSync(__dirname + '/content/Leaves of Grass by Walt Whitman.epub')

var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80'

test('client.add/remove: magnet uri, utf-8 string', function (t) {
  var client = new WebTorrent({ dht: false, tracker: false })
  var torrent = client.add('magnet:?xt=urn:btih:' + leavesTorrent.infoHash)
  t.equal(client.torrents.length, 1)
  torrent.on('infoHash', function () {
    t.equal(torrent.infoHash, leavesTorrent.infoHash)
    t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) === 0)
    client.remove('magnet:?xt=urn:btih:' + leavesTorrent.infoHash)
    t.equal(client.torrents.length, 0)
    client.destroy()
    t.end()
  })
})

test('client.add/remove: torrent file, buffer', function (t) {
  var client = new WebTorrent({ dht: false, tracker: false })
  var torrent = client.add(leaves)
  t.equal(client.torrents.length, 1)
  torrent.on('infoHash', function () {
    t.equal(torrent.infoHash, leavesTorrent.infoHash)
    t.equal(torrent.magnetURI, leavesMagnetURI)
    client.remove(leaves)
    t.equal(client.torrents.length, 0)
    client.destroy()
    t.end()
  })
})

test('client.add/remove: info hash, hex string', function (t) {
  var client = new WebTorrent({ dht: false, tracker: false })
  var torrent = client.add(leavesTorrent.infoHash)
  t.equal(client.torrents.length, 1)
  torrent.on('infoHash', function () {
    t.equal(torrent.infoHash, leavesTorrent.infoHash)
    t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) === 0)
    client.remove(leavesTorrent.infoHash)
    t.equal(client.torrents.length, 0)
    client.destroy()
    t.end()
  })
})

test('client.add/remove: info hash, buffer', function (t) {
  var client = new WebTorrent({ dht: false, tracker: false })
  var torrent = client.add(new Buffer(leavesTorrent.infoHash, 'hex'))
  t.equal(client.torrents.length, 1)
  torrent.on('infoHash', function () {
    t.equal(torrent.infoHash, leavesTorrent.infoHash)
    t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) === 0)
    client.remove(new Buffer(leavesTorrent.infoHash, 'hex'))
    t.equal(client.torrents.length, 0)
    client.destroy()
    t.end()
  })
})

test('client.add/remove: parsed torrent, from `parse-torrent`', function (t) {
  var client = new WebTorrent({ dht: false, tracker: false })
  var torrent = client.add(leavesTorrent)
  t.equal(client.torrents.length, 1)
  torrent.on('infoHash', function () {
    t.equal(torrent.infoHash, leavesTorrent.infoHash)
    t.equal(torrent.magnetURI, leavesMagnetURI)
    client.remove(leavesTorrent)
    t.equal(client.torrents.length, 0)
    client.destroy()
    t.end()
  })
})

test('client.add/remove: parsed torrent, with string type announce property', function (t) {
  var client = new WebTorrent({ dht: false, tracker: false })
  var modifiedParsedTorrent = extend(leavesTorrent, {
    announce: leavesTorrent.announce[0]
  })
  var torrent = client.add(modifiedParsedTorrent)
  t.equal(client.torrents.length, 1)
  torrent.on('infoHash', function () {
    t.equal(torrent.infoHash, leavesTorrent.infoHash)
    client.remove(leavesTorrent)
    t.equal(client.torrents.length, 0)
    client.destroy()
    t.end()
  })
})

test('client.remove: remove by Torrent object', function (t) {
  var client = new WebTorrent({ dht: false, tracker: false })
  var torrent = client.add(leavesTorrent.infoHash)
  t.equal(client.torrents.length, 1)
  torrent.on('infoHash', function () {
    t.equal(torrent.infoHash, leavesTorrent.infoHash)
    client.remove(torrent)
    t.equal(client.torrents.length, 0)
    client.destroy()
    t.end()
  })
})

test('client.seed: torrent file (Buffer)', function (t) {
  t.plan(4)

  var opts = {
    name: 'Leaves of Grass by Walt Whitman.epub',
    announce: [
      'http://tracker.thepiratebay.org/announce',
      'udp://tracker.openbittorrent.com:80',
      'udp://tracker.ccc.de:80',
      'udp://tracker.publicbt.com:80',
      'udp://fr33domtracker.h33t.com:3310/announce',
      'http://tracker.bittorrent.am/announce'
    ]
  }

  var client = new WebTorrent({ dht: false, tracker: false })
  client.seed(leavesBook, opts, function (torrent1) {
    t.equal(client.torrents.length, 1)
    t.equal(torrent1.infoHash, leavesTorrent.infoHash)
    t.equal(torrent1.magnetURI, leavesMagnetURI)
    client.remove(torrent1)
    t.equal(client.torrents.length, 0)
    client.destroy()
  })
})

test('client.seed: torrent file (Blob)', function (t) {
  var opts = {
    name: 'Leaves of Grass by Walt Whitman.epub',
    announce: [
      'http://tracker.thepiratebay.org/announce',
      'udp://tracker.openbittorrent.com:80',
      'udp://tracker.ccc.de:80',
      'udp://tracker.publicbt.com:80',
      'udp://fr33domtracker.h33t.com:3310/announce',
      'http://tracker.bittorrent.am/announce'
    ]
  }

  if (global.Blob !== undefined) {
    t.plan(4)
    var client = new WebTorrent({ dht: false, tracker: false })
    client.seed(new global.Blob([ leavesBook ]), opts, function (torrent2) {
      t.equal(client.torrents.length, 1)
      t.equal(torrent2.infoHash, leavesTorrent.infoHash)
      t.equal(torrent2.magnetURI, leavesMagnetURI)
      client.remove(torrent2)
      t.equal(client.torrents.length, 0)
      client.destroy()
    })
  } else {
    t.pass('Skipping Blob test because missing `Blob` constructor')
    t.end()
  }
})

test('after client.destroy(), throw on client.add() or client.seed()', function (t) {
  t.plan(3)

  var client = new WebTorrent({ dht: false, tracker: false })
  client.destroy(function () {
    t.pass('client destroyed')
  })
  t.throws(function () {
    client.add('magnet:?xt=urn:btih:' + leavesTorrent.infoHash)
  })
  t.throws(function () {
    client.seed(new Buffer('sup'))
  })
})

test('after client.destroy(), no "torrent" or "ready" events emitted', function (t) {
  t.plan(1)

  var client = new WebTorrent({ dht: false, tracker: false })
  client.add(leaves, { name: 'leaves' }, function () {
    t.fail('unexpected "torrent" event (from add)')
  })
  client.seed(leavesBook, { name: 'leavesBook' }, function () {
    t.fail('unexpected "torrent" event (from seed)')
  })
  client.on('ready', function () {
    t.fail('unexpected "ready" event')
  })
  client.destroy(function () {
    t.pass('client destroyed')
  })
})