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: d9d44c2d1c56c4ced03f86f84af2c8e1e9d450a8 (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
var fs = require('fs')
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.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce'

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)
  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)
  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)
  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)
  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)
  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.seed (Buffer, Blob)', function (t) {
  t.plan(global.Blob !== undefined ? 8 : 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'
    ]
  }

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

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

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, function () {
    t.fail('unexpected "torrent" event (from add)')
  })
  client.seed(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')
  })
})