forked from lnguyen/go-transmission
-
Notifications
You must be signed in to change notification settings - Fork 7
/
transmission_test.go
102 lines (83 loc) · 2.54 KB
/
transmission_test.go
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
package transmission
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/go-martini/martini"
"github.com/martini-contrib/auth"
. "github.com/smartystreets/goconvey/convey"
)
var (
tMux *http.ServeMux
transmissionClient TransmissionClient
tServer *httptest.Server
)
func tSetup(output string) {
tMux = http.NewServeMux()
tServer = httptest.NewServer(tMux)
m := martini.New()
r := martini.NewRouter()
r.Post("/transmission/rpc", func() string {
return output
})
m.Action(r.Handle)
m.Use(auth.Basic("test", "test"))
tMux.Handle("/", m)
transmissionClient = New(tServer.URL, "test", "test")
}
func tTeardown() {
tServer.Close()
}
func TestGetTorrents(t *testing.T) {
tSetup(`{"arguments":{"torrents":[{"eta":-1,"id":5,
"leftUntilDone":0,"name":"Test",
"rateDownload":0,"rateUpload":0,"status":6,"uploadRatio":0.3114}]},
"result":"success"}`)
defer tTeardown()
Convey("Test get list torrents", t, func() {
torrents, err := transmissionClient.GetTorrents()
So(err, ShouldBeNil)
So(len(torrents), ShouldEqual, 1)
})
}
func TestRemoveTorrent(t *testing.T) {
tSetup(`{"arguments":{},"result":"success"}`)
defer tTeardown()
Convey("Test removing torrent", t, func() {
delCmd, err := NewDelCmd(1, true)
So(err, ShouldBeNil)
cmd, err := transmissionClient.ExecuteCommand(delCmd)
So(err, ShouldBeNil)
So(cmd.Result, ShouldEqual, "success")
})
}
func TestAddTorrentByFilename(t *testing.T) {
tSetup(`{"arguments":{"torrent-added":
{"hashString":"875a2d90068c32b4ce7992eaf56cd03f5be0d193",
"id":23,"name":"Test Name"}}
,"result":"success"}`)
defer tTeardown()
Convey("Test adding torrent by filename", t, func() {
addCmd, err := NewAddCmdByFilename("/tmp/file")
So(err, ShouldBeNil)
result, err := transmissionClient.ExecuteAddCommand(addCmd)
So(err, ShouldBeNil)
So(result.Name, ShouldEqual, "Test Name")
So(result.ID, ShouldEqual, 23)
})
}
func TestAddTorrentByMagnet(t *testing.T) {
tSetup(`{"arguments":{"torrent-added":
{"hashString":"875a2d90068c32b4ce7992eaf56cd03f5be0d193",
"id":23,"name":"CentOS 7.0 x64"}}
,"result":"success"}`)
defer tTeardown()
Convey("Test adding torrent by magnet link", t, func() {
addCmd, err := NewAddCmdByMagnet("magnet:?xt=urn:btih:1354ac45bfb3e644a04d69cc519e83283bd3ac6a&dn=CentOS+7.0+x64&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80")
So(err, ShouldBeNil)
result, err := transmissionClient.ExecuteAddCommand(addCmd)
So(err, ShouldBeNil)
So(result.Name, ShouldEqual, "CentOS 7.0 x64")
So(result.ID, ShouldEqual, 23)
})
}