forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
93 lines (82 loc) · 1.86 KB
/
http_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
package http
import (
"context"
"errors"
"fmt"
"io"
"reflect"
"runtime"
"testing"
cmds "github.com/ipfs/go-ipfs-cmds"
)
func TestHTTP(t *testing.T) {
type testcase struct {
path []string
v interface{}
err error
}
tcs := []testcase{
{
path: []string{"version"},
v: &VersionOutput{
Version: "0.1.2",
Commit: "c0mm17",
Repo: "4",
System: runtime.GOARCH + "/" + runtime.GOOS, //TODO: Precise version here
Golang: runtime.Version(),
},
},
{
path: []string{"error"},
err: errors.New("an error occurred"),
},
{
path: []string{"doubleclose"},
v: "some value",
},
}
mkTest := func(tc testcase) func(*testing.T) {
return func(t *testing.T) {
srv := getTestServer(t, nil) // handler_test:/^func getTestServer/
c := NewClient(srv.URL)
req, err := cmds.NewRequest(context.Background(), tc.path, nil, nil, nil, cmdRoot)
if err != nil {
t.Fatal(err)
}
res, err := c.Send(req)
if err != nil {
t.Fatal("unexpected error:", err)
}
v, err := res.Next()
if tc.err != nil {
if err == nil {
t.Fatal("got nil error, expected:", tc.err)
} else if err.Error() != tc.err.Error() {
t.Fatalf("got error %q, expected %q", err, tc.err)
}
} else if err != nil {
t.Fatal("unexpected error:", err)
}
// TODO find a better way to solve this!
if s, ok := v.(*string); ok {
v = *s
}
if !reflect.DeepEqual(v, tc.v) {
t.Errorf("expected value to be %v but got: %+v", tc.v, v)
}
_, err = res.Next()
if tc.err != nil {
if err == nil {
t.Fatal("got nil error, expected:", tc.err)
} else if err.Error() != tc.err.Error() {
t.Fatalf("got error %q, expected %q", err, tc.err)
}
} else if err != io.EOF {
t.Fatal("expected io.EOF error, got:", err)
}
}
}
for i, tc := range tcs {
t.Run(fmt.Sprint(i), mkTest(tc))
}
}