]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/http_test.go
c90a35ef5e11b362dab3afad799f994472e95cb2
[btrtrc.git] / tracker / http_test.go
1 package tracker
2
3 import (
4         "testing"
5
6         "github.com/stretchr/testify/assert"
7         "github.com/stretchr/testify/require"
8
9         "github.com/anacrolix/torrent/bencode"
10 )
11
12 var defaultHTTPUserAgent = "Go-Torrent"
13
14 func TestUnmarshalHTTPResponsePeerDicts(t *testing.T) {
15         var hr httpResponse
16         require.NoError(t, bencode.Unmarshal(
17                 []byte("d5:peersl"+
18                         "d2:ip7:1.2.3.47:peer id20:thisisthe20bytepeeri4:porti9999ee"+
19                         "d7:peer id20:thisisthe20bytepeeri2:ip39:2001:0db8:85a3:0000:0000:8a2e:0370:73344:porti9998ee"+
20                         "e"+
21                         "6:peers618:123412341234123456"+
22                         "e"),
23                 &hr))
24
25         require.Len(t, hr.Peers, 2)
26         assert.Equal(t, []byte("thisisthe20bytepeeri"), hr.Peers[0].ID)
27         assert.EqualValues(t, 9999, hr.Peers[0].Port)
28         assert.EqualValues(t, 9998, hr.Peers[1].Port)
29         assert.NotNil(t, hr.Peers[0].IP)
30         assert.NotNil(t, hr.Peers[1].IP)
31
32         assert.Len(t, hr.Peers6, 1)
33         assert.EqualValues(t, "1234123412341234", hr.Peers6[0].IP)
34         assert.EqualValues(t, 0x3536, hr.Peers6[0].Port)
35 }
36
37 func TestUnmarshalHttpResponseNoPeers(t *testing.T) {
38         var hr httpResponse
39         require.NoError(t, bencode.Unmarshal(
40                 []byte("d6:peers618:123412341234123456e"),
41                 &hr,
42         ))
43         require.Len(t, hr.Peers, 0)
44         assert.Len(t, hr.Peers6, 1)
45 }
46
47 func TestUnmarshalHttpResponsePeers6NotCompact(t *testing.T) {
48         var hr httpResponse
49         require.Error(t, bencode.Unmarshal(
50                 []byte("d6:peers6lee"),
51                 &hr,
52         ))
53 }