Skip to content

Commit

Permalink
handle torrent with no content.
Browse files Browse the repository at this point in the history
  • Loading branch information
adelolmo committed Aug 22, 2020
1 parent 856f9a7 commit 2848194
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 786 deletions.
33 changes: 0 additions & 33 deletions Godeps/Godeps.json

This file was deleted.

5 changes: 0 additions & 5 deletions Godeps/Readme

This file was deleted.

34 changes: 21 additions & 13 deletions delugeclient.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package delugeclient

import (
"golang.org/x/net/publicsuffix"
"net/http/cookiejar"
"net/http"
"fmt"
"log"
"crypto/tls"
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"golang.org/x/net/publicsuffix"
"log"
"net/http"
"net/http/cookiejar"
"os"
)

Expand All @@ -32,7 +32,7 @@ type RpcResponse struct {
}

func (r RpcResponse) String() string {
return fmt.Sprintf("id: '%d' result: '%s' error: {%s}", r.Id, r.Result, r.Error)
return fmt.Sprintf("id: '%d' result: '%t' error: {%s}", r.Id, r.Result, r.Error)
}
func (e RpcError) String() string {
return fmt.Sprintf("code: '%d' message: '%s'", e.Code, e.Message)
Expand Down Expand Up @@ -108,7 +108,7 @@ func (d *Deluge) Connect() error {
return fmt.Errorf("error code %d! %s", rr.Error.Code, rr.Error.Message)
}

d.Index ++
d.Index++
return nil
}

Expand All @@ -127,7 +127,7 @@ func (d *Deluge) AddMagnet(magnet string) error {
log.Println(rr)
return fmt.Errorf("error code %d! %s", rr.Error.Code, rr.Error.Message)
}
d.Index ++
d.Index++
return nil
}

Expand All @@ -146,7 +146,7 @@ func (d *Deluge) MoveToQueueTop(torrentId string) error {
log.Println(rr)
return fmt.Errorf("error code %d! %s", rr.Error.Code, rr.Error.Message)
}
d.Index ++
d.Index++
return nil
}

Expand Down Expand Up @@ -192,6 +192,14 @@ func (d *Deluge) Get(torrentId string) (*Torrent, error) {
return nil, nil
}

if len(rr.TorrentResult.Contents) == 0 {
return &Torrent{
Id: torrentId,
Files: make([]string, 0),
ShareRatio: 0,
}, nil
}

for k, v := range rr.TorrentResult.Contents {

contents := rr.TorrentResult.Contents[k]
Expand All @@ -213,7 +221,7 @@ func (d *Deluge) Get(torrentId string) (*Torrent, error) {
files = append(files, x)
}
}
d.Index ++
d.Index++
return &Torrent{
Id: torrentId,
Name: v.Path,
Expand Down Expand Up @@ -244,7 +252,7 @@ func (d *Deluge) GetAll() ([]Torrent, error) {
for k, v := range rr.Torrents.Map {
torrents = append(torrents, Torrent{Id: k, Name: v.Name, ShareRatio: v.Ratio, Progress: v.Progress})
}
d.Index ++
d.Index++
return torrents, nil
}

Expand All @@ -263,7 +271,7 @@ func (d *Deluge) Remove(torrentId string) error {
log.Println(rr)
return fmt.Errorf("error code %d! %s", rr.Error.Code, rr.Error.Message)
}
d.Index ++
d.Index++
return nil
}

Expand Down
70 changes: 49 additions & 21 deletions delugeclient_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package delugeclient_test

import (
"github.com/adelolmo/delugeclient"
"testing"
"fmt"
"net/http"
"github.com/adelolmo/delugeclient"
"github.com/bmizerany/assert"
"github.com/bmizerany/pat"
"github.com/drewolson/testflight"
"io"
"io/ioutil"
"net/http"
"strings"
"github.com/drewolson/testflight"
"github.com/bmizerany/pat"
"github.com/bmizerany/assert"
"testing"
)

func TestNewDelugeNoServerUrl(t *testing.T) {
assertPanic(t, func() {
delugeclient.NewDeluge("", "")
})
}

func TestConnection(t *testing.T) {
testflight.WithServer(Handler(""), func(r *testflight.Requester) {
client := delugeclient.NewDeluge("http://" + r.Url(""), "pass")
client := delugeclient.NewDeluge("http://"+r.Url(""), "pass")
fmt.Println(r.Url(""))
if err := client.Connect(); err != nil {
fmt.Println(err)
Expand All @@ -31,7 +32,7 @@ func TestConnection(t *testing.T) {

func TestConnectionWrongPassword(t *testing.T) {
testflight.WithServer(WrongPasswordHandler(), func(r *testflight.Requester) {
client := delugeclient.NewDeluge("http://" + r.Url(""), "xxx")
client := delugeclient.NewDeluge("http://"+r.Url(""), "xxx")
if err := client.Connect(); err == nil {
t.Fail()
}
Expand All @@ -44,7 +45,7 @@ func TestAddingMagnet(t *testing.T) {
{"id": 2, "result": true, "error":{"code":0, "message":""}}
`),
func(r *testflight.Requester) {
client := delugeclient.NewDeluge("http://" + r.Url(""), "pass")
client := delugeclient.NewDeluge("http://"+r.Url(""), "pass")
if err := client.Connect(); err != nil {
t.Fail()
}
Expand All @@ -55,6 +56,33 @@ func TestAddingMagnet(t *testing.T) {
})
}

func TestGettingNoFiles(t *testing.T) {
testflight.WithServer(Handler(
`{
"id": 2,
"result": {
"type": "dir",
"contents": {}
},
"error": null
}`), func(r *testflight.Requester) {
client := delugeclient.NewDeluge("http://"+r.Url(""), "pass")
if err := client.Connect(); err != nil {
t.Fail()
}
torrent, err := client.Get("id")
if err != nil {
fmt.Println(err)
t.Fail()
}
fmt.Println(torrent)
assert.Equal(t, "id", torrent.Id)
assert.Equal(t, "", torrent.Name)
assert.Equal(t, 0.0, torrent.ShareRatio)
assert.Equal(t, 0, len(torrent.Files))
})
}

func TestGettingSingleFile(t *testing.T) {
testflight.WithServer(Handler(
`{
Expand All @@ -77,7 +105,7 @@ func TestGettingSingleFile(t *testing.T) {
"error": null
}`),
func(r *testflight.Requester) {
client := delugeclient.NewDeluge("http://" + r.Url(""), "pass")
client := delugeclient.NewDeluge("http://"+r.Url(""), "pass")
if err := client.Connect(); err != nil {
t.Fail()
}
Expand All @@ -102,9 +130,9 @@ func TestGettingMultipleFiles(t *testing.T) {
"result": {
"type": "dir",
"contents": {
"Some.Linux.Disto": {
"Some.Linux.Distro": {
"priority": 1,
"path": "Some.Linux.Disto",
"path": "Some.Linux.Distro",
"progress": 85.989601135254,
"progresses": [
10199684.56,
Expand All @@ -119,7 +147,7 @@ func TestGettingMultipleFiles(t *testing.T) {
"index": 1,
"offset": 1019968456,
"progress": 1,
"path": "Some.Linux.Disto\/README.txt",
"path": "Some.Linux.Distro\/README.txt",
"type": "file",
"size": 30
},
Expand All @@ -128,7 +156,7 @@ func TestGettingMultipleFiles(t *testing.T) {
"index": 0,
"offset": 0,
"progress": 1,
"path": "Some.Linux.Disto\/Distribution.iso",
"path": "Some.Linux.Distro\/Distribution.iso",
"type": "file",
"size": 1019968456
},
Expand All @@ -137,7 +165,7 @@ func TestGettingMultipleFiles(t *testing.T) {
"index": 2,
"offset": 1019968486,
"progress": 1,
"path": "Some.Linux.Disto\/distribution.nfo",
"path": "Some.Linux.Distro\/distribution.nfo",
"type": "file",
"size": 57
}
Expand All @@ -149,7 +177,7 @@ func TestGettingMultipleFiles(t *testing.T) {
"error": null
}`),
func(r *testflight.Requester) {
client := delugeclient.NewDeluge("http://" + r.Url(""), "pass")
client := delugeclient.NewDeluge("http://"+r.Url(""), "pass")
if err := client.Connect(); err != nil {
t.Fail()
}
Expand All @@ -160,7 +188,7 @@ func TestGettingMultipleFiles(t *testing.T) {
}
fmt.Println(torrent)
assert.Equal(t, "id", torrent.Id)
assert.Equal(t, "Some.Linux.Disto", torrent.Name)
assert.Equal(t, "Some.Linux.Distro", torrent.Name)
assert.Equal(t, 1.0, torrent.ShareRatio)
assert.Equal(t, 85.989601135254, torrent.Progress)
assert.Equal(t, "README.txt", torrent.Files[0])
Expand All @@ -179,7 +207,7 @@ func TestGettingAll(t *testing.T) {
"asdfgh123456": {
"message": "OK",
"ratio": 4.08238410949707,
"name": "Some.Linux.Disto"
"name": "Some.Linux.Distro"
},
"123456asdfgh": {
"message": "OK",
Expand All @@ -192,7 +220,7 @@ func TestGettingAll(t *testing.T) {
}
`),
func(r *testflight.Requester) {
client := delugeclient.NewDeluge("http://" + r.Url(""), "pass")
client := delugeclient.NewDeluge("http://"+r.Url(""), "pass")
if err := client.Connect(); err != nil {
t.Fail()
}
Expand All @@ -204,7 +232,7 @@ func TestGettingAll(t *testing.T) {
fmt.Println(torrents)
assert.Equal(t, 2, len(torrents))
assert.Equal(t, "asdfgh123456", torrents[0].Id)
assert.Equal(t, "Some.Linux.Disto", torrents[0].Name)
assert.Equal(t, "Some.Linux.Distro", torrents[0].Name)
assert.Equal(t, 4.08238410949707, torrents[0].ShareRatio)
assert.Equal(t, "123456asdfgh", torrents[1].Id)
assert.Equal(t, "Some.Video", torrents[1].Name)
Expand All @@ -218,7 +246,7 @@ func TestRemovingTorrent(t *testing.T) {
{"id": 2, "result": true, "error":{"code":0, "message":""}}
`),
func(r *testflight.Requester) {
client := delugeclient.NewDeluge("http://" + r.Url(""), "pass")
client := delugeclient.NewDeluge("http://"+r.Url(""), "pass")
if err := client.Connect(); err != nil {
t.Fail()
}
Expand Down
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/adelolmo/delugeclient

go 1.14

require (
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869
github.com/bmizerany/pat v0.0.0-20160217103242-c068ca2f0aac
github.com/drewolson/testflight v0.0.0-20160816135644-7040c250b472
github.com/kr/pretty v0.0.0-20160823170715-cfb55aafdaf3 // indirect
github.com/kr/text v0.0.0-20160504234017-7cafcd837844 // indirect
golang.org/x/net v0.0.0-20170224200717-10c134ea0df1
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/bmizerany/pat v0.0.0-20160217103242-c068ca2f0aac h1:X5YRFJiteUM3rajABEYJSzw1KWgmp1ulPFKxpfLm0M4=
github.com/bmizerany/pat v0.0.0-20160217103242-c068ca2f0aac/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/drewolson/testflight v0.0.0-20160816135644-7040c250b472 h1:ZqGX/6KPu6Mkt8ei+5ChMGWqD+sBjyZ15t390BGPZMo=
github.com/drewolson/testflight v0.0.0-20160816135644-7040c250b472/go.mod h1:4DQA1+9ebJu104KevSVjTEZwJDyvD3m8Q5YFS7J2aqM=
github.com/kr/pretty v0.0.0-20160823170715-cfb55aafdaf3 h1:dhwb1Ev84SKKVBfLuhR4bw/29yYHzwtTyTLUWWnvYxI=
github.com/kr/pretty v0.0.0-20160823170715-cfb55aafdaf3/go.mod h1:Bvhd+E3laJ0AVkG0c9rmtZcnhV0HQ3+c3YxxqTvc/gA=
github.com/kr/text v0.0.0-20160504234017-7cafcd837844 h1:kpzneEBeC0dMewP3gr/fADv1OlblH9r1goWVwpOt3TU=
github.com/kr/text v0.0.0-20160504234017-7cafcd837844/go.mod h1:sjUstKUATFIcff4qlB53Kml0wQPtJVc/3fWrmuUmcfA=
golang.org/x/net v0.0.0-20170224200717-10c134ea0df1 h1:65sJBFLxq91yL9ATvtv/ShzUYhbDfr5A25hDkQg9lMw=
golang.org/x/net v0.0.0-20170224200717-10c134ea0df1/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Loading

0 comments on commit 2848194

Please sign in to comment.