forked from Tubbebubbe/transmission
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
66 lines (54 loc) · 1.42 KB
/
client_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
package transmission
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-martini/martini"
"github.com/martini-contrib/auth"
. "github.com/smartystreets/goconvey/convey"
)
var (
cMux *http.ServeMux
client ApiClient
cServer *httptest.Server
)
func RPCHandler(res http.ResponseWriter, req *http.Request) {
if req.Header.Get("X-Transmission-Session-Id") == "" {
res.Header().Set("X-Transmission-Session-Id", "123")
res.WriteHeader(http.StatusConflict)
return
}
fmt.Fprintf(res, `{"arguments":{},"result":"no method name"}`)
}
func cSetup() {
// test server
cMux = http.NewServeMux()
m := martini.New()
r := martini.NewRouter()
r.Post("/transmission/rpc", RPCHandler)
m.Action(r.Handle)
m.Use(auth.Basic("test", "test"))
cMux.Handle("/", m)
cServer = httptest.NewServer(cMux)
// github client configured to use test server
client = NewClient(cServer.URL, "test", "test")
}
func cTeardown() {
cServer.Close()
}
func TestPost(t *testing.T) {
cSetup()
defer cTeardown()
Convey("Test Post is working correctly", t, func() {
output, err := client.Post("")
So(err, ShouldBeNil)
So(string(output), ShouldEqual, `{"arguments":{},"result":"no method name"}`)
})
Convey("Test when auth is incorrect", t, func() {
fakeClient := NewClient(cServer.URL, "testfake", "testfake")
output, err := fakeClient.Post("")
So(err, ShouldBeNil)
So(string(output), ShouldEqual, "Not Authorized\n")
})
}