-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy patherrors_test.go
132 lines (112 loc) · 2.81 KB
/
errors_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package http
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"runtime"
"strings"
"testing"
"github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-cmds"
)
func TestErrors(t *testing.T) {
type testcase struct {
opts cmdkit.OptMap
path []string
bodyStr string
status string
errTrailer string
}
tcs := []testcase{
{
path: []string{"version"},
bodyStr: `{` +
`"Version":"0.1.2",` +
`"Commit":"c0mm17",` +
`"Repo":"4",` +
`"System":"` + runtime.GOARCH + "/" + runtime.GOOS + `",` +
`"Golang":"` + runtime.Version() + `"}` + "\n",
status: "200 OK",
},
// TODO this error should be sent as a value, because it is non-200
{
path: []string{"error"},
status: "500 Internal Server Error",
bodyStr: `{"Message":"an error occurred","Code":0,"Type":"error"}` + "\n",
},
{
path: []string{"lateerror"},
status: "200 OK",
bodyStr: `"some value"` + "\n",
errTrailer: "an error occurred",
},
{
path: []string{"encode"},
opts: cmdkit.OptMap{
cmds.EncLong: cmds.Text,
},
status: "500 Internal Server Error",
bodyStr: "an error occurred",
},
{
path: []string{"lateencode"},
opts: cmdkit.OptMap{
cmds.EncLong: cmds.Text,
},
status: "200 OK",
bodyStr: "hello\n",
errTrailer: "an error occurred",
},
{
path: []string{"doubleclose"},
status: "200 OK",
bodyStr: `"some value"` + "\n",
},
{
path: []string{"single"},
status: "200 OK",
bodyStr: `"some value"` + "\n",
},
{
path: []string{"reader"},
status: "200 OK",
bodyStr: "the reader call returns a reader.",
},
}
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, tc.opts, nil, nil, cmdRoot)
if err != nil {
t.Fatal(err)
}
httpReq, err := c.(*client).toHTTPRequest(req)
if err != nil {
t.Fatal("unexpected error:", err)
}
httpClient := http.DefaultClient
res, err := httpClient.Do(httpReq)
if err != nil {
t.Fatal("unexpected error", err)
}
if res.Status != tc.status {
t.Errorf("expected status %v, got %v", tc.status, res.Status)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal("err reading response body", err)
}
if bodyStr := string(body); bodyStr != tc.bodyStr {
t.Errorf("expected body string \n\n%v\n\n, got\n\n%v", tc.bodyStr, bodyStr)
}
if errTrailer := res.Trailer.Get(StreamErrHeader); errTrailer != tc.errTrailer {
t.Errorf("expected error header %q, got %q", tc.errTrailer, errTrailer)
}
}
}
for i, tc := range tcs {
t.Run(fmt.Sprintf("%d-%s", i, strings.Join(tc.path, "/")), mkTest(tc))
}
}