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

download-metadata.js « node « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9a185e98fa496f66b4a6afc79283353fecc5ffe1 (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
var fixtures = require('webtorrent-fixtures')
var http = require('http')
var MemoryChunkStore = require('memory-chunk-store')
var test = require('tape')
var WebTorrent = require('../../')

function createServer (data, cb) {
  var server = http.createServer(function (req, res) {
    if (req.url !== '/') {
      res.statusCode = 404
      res.end()
    } else {
      res.end(data)
    }
  })

  server.on('listening', function () {
    var address = server.address()
    var url = 'http://127.0.0.1:' + address.port + '/'
    cb(url, server)
  })

  server.listen()
}

test('Download metadata for magnet URI with xs parameter', function (t) {
  t.plan(3)

  var client = new WebTorrent({ dht: false, tracker: false })

  client.on('error', function (err) { t.fail(err) })
  client.on('warning', function (err) { t.fail(err) })

  createServer(fixtures.leaves.torrent, function (url, server) {
    var encodedUrl = encodeURIComponent(url)
    client.add(fixtures.leaves.magnetURI + '&xs=' + encodedUrl, { store: MemoryChunkStore }, function (torrent) {
      t.equal(torrent.files[0].name, 'Leaves of Grass by Walt Whitman.epub')
      client.destroy(function (err) { t.error(err, 'client destroyed') })
      server.close(function () { t.pass('server closed') })
    })
  })
})

test('Download metadata for magnet URI with 2 xs parameters', function (t) {
  t.plan(4)

  var client = new WebTorrent({ dht: false, tracker: false })

  client.on('error', function (err) { t.fail(err) })
  client.on('warning', function (err) { t.fail(err) })

  createServer(fixtures.leaves.torrent, function (url1, server1) {
    var encodedUrl1 = encodeURIComponent(url1)

    createServer(fixtures.leaves.torrent, function (url2, server2) {
      var encodedUrl2 = encodeURIComponent(url2)

      var uri = fixtures.leaves.magnetURI + '&xs=' + encodedUrl1 + '&xs=' + encodedUrl2

      client.add(uri, { store: MemoryChunkStore }, function (torrent) {
        t.equal(torrent.files[0].name, 'Leaves of Grass by Walt Whitman.epub')
        client.destroy(function (err) { t.error(err, 'client destroyed') })
        server1.close(function () { t.pass('server closed') })
        server2.close(function () { t.pass('server closed') })
      })
    })
  })
})

test('Download metadata for magnet URI with 2 xs parameters, with 1 invalid protocol', function (t) {
  t.plan(3)

  var client = new WebTorrent({ dht: false, tracker: false })

  client.on('error', function (err) { t.fail(err) })
  client.on('warning', function (err) { t.fail(err) })

  createServer(fixtures.leaves.torrent, function (url, server) {
    var encodedUrl1 = encodeURIComponent('invalidurl:example')
    var encodedUrl2 = encodeURIComponent(url)
    var uri = fixtures.leaves.magnetURI + '&xs=' + encodedUrl1 + '&xs=' + encodedUrl2

    client.add(uri, { store: MemoryChunkStore }, function (torrent) {
      t.equal(torrent.files[0].name, 'Leaves of Grass by Walt Whitman.epub')
      client.destroy(function (err) { t.error(err, 'client destroyed') })
      server.close(function () { t.pass('server closed') })
    })
  })
})

test('Download metadata for magnet URI with 2 xs parameters, with 1 404 URL', function (t) {
  t.plan(3)

  var client = new WebTorrent({ dht: false, tracker: false })

  client.on('error', function (err) { t.fail(err) })
  client.on('warning', function (err) { t.fail(err) })

  createServer(fixtures.leaves.torrent, function (url, server) {
    var encodedUrl1 = encodeURIComponent(url + 'blah_404')
    var encodedUrl2 = encodeURIComponent(url)
    var uri = fixtures.leaves.magnetURI + '&xs=' + encodedUrl1 + '&xs=' + encodedUrl2

    client.add(uri, { store: MemoryChunkStore }, function (torrent) {
      t.equal(torrent.files[0].name, 'Leaves of Grass by Walt Whitman.epub')
      client.destroy(function (err) { t.error(err, 'client destroyed') })
      server.close(function () { t.pass('server closed') })
    })
  })
})

test('Download metadata magnet URI with unsupported protocol in xs parameter', function (t) {
  t.plan(1)

  var client = new WebTorrent({ dht: false, tracker: false })

  client.on('error', function (err) { t.fail(err) })
  client.on('warning', function (err) { t.fail(err) })

  client.add(fixtures.leaves.magnetURI + '&xs=' + encodeURIComponent('invalidurl:example'), { store: MemoryChunkStore })

  setTimeout(function () {
    // no crash by now
    client.destroy(function (err) { t.error(err, 'client destroyed') })
  }, 100)
})