forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
240 lines (206 loc) · 5.11 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package http
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"reflect"
"runtime"
"strings"
"testing"
cmds "github.com/ipfs/go-ipfs-cmds"
"github.com/ipfs/boxo/files"
)
func newReaderPathFile(t *testing.T, path string, reader io.ReadCloser, stat os.FileInfo) files.File {
f, err := files.NewReaderPathFile(path, reader, stat)
if err != nil {
t.Fatal(err)
}
return f
}
func TestHTTP(t *testing.T) {
type testcase struct {
path []string
v interface{}
vs []interface{}
file files.Directory
r string
err error
sendErr error
wait bool
close bool
}
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"},
sendErr: errors.New("an error occurred"),
},
{
path: []string{"doubleclose"},
v: "some value",
},
{
path: []string{"single"},
v: "some value",
wait: true,
},
{
path: []string{"reader"},
r: "the reader call returns a reader.",
},
{
path: []string{"echo"},
file: files.NewMapDirectory(map[string]files.Node{
"stdin": newReaderPathFile(t,
"/dev/stdin",
readCloser{
Reader: bytes.NewBufferString("This is the body of the request!"),
Closer: nopCloser{},
}, nil),
}),
vs: []interface{}{"i received:", "This is the body of the request!"},
close: true,
},
}
mkTest := func(tc testcase) func(*testing.T) {
return func(t *testing.T) {
env, srv := getTestServer(t, nil, true) // 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)
}
if tc.file != nil {
req.Files = tc.file
}
res, err := c.(*client).send(req)
if tc.sendErr != nil {
if err == nil {
t.Fatalf("expected error %q but got nil", tc.sendErr)
}
if err.Error() != tc.sendErr.Error() {
t.Fatalf("expected error %q but got %q", tc.sendErr, err)
}
return
} else if err != nil {
t.Fatal("unexpected error:", err)
}
if len(tc.vs) > 0 {
for _, tc.v = range tc.vs {
v, err := res.Next()
if err != nil {
t.Error("unexpected error:", err)
}
// TODO find a better way to solve this!
if s, ok := v.(*string); ok {
v = *s
}
t.Log("v:", v, "err:", err)
// if we don't expect a reader
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)
}
} else if len(tc.r) == 0 {
v, err := res.Next()
// TODO find a better way to solve this!
if s, ok := v.(*string); ok {
v = *s
}
t.Log("v:", v, "err:", err)
if tc.err != nil {
if err == nil {
t.Error("got nil error, expected:", tc.err)
} else if err.Error() != tc.err.Error() {
t.Errorf("got error %q, expected %q", err, tc.err)
}
} else if err != nil {
t.Fatal("unexpected error:", err)
}
// if we don't expect a reader
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)
}
} else {
v, err := res.Next()
// TODO find a better way to solve this!
if s, ok := v.(*string); ok {
v = *s
}
t.Log("v:", v, "err:", err)
if tc.err != nil {
if err == nil {
t.Error("got nil error, expected:", tc.err)
} else if err.Error() != tc.err.Error() {
t.Errorf("got error %q, expected %q", err, tc.err)
}
} else if err != nil {
t.Fatal("unexpected error:", err)
}
r, ok := v.(io.Reader)
if !ok {
t.Fatalf("expected a %T but got a %T", r, v)
}
var buf bytes.Buffer
_, err = io.Copy(&buf, r)
if err != nil {
t.Fatal("unexpected copy error:", err)
}
if buf.String() != tc.r {
t.Errorf("expected return string %q but got %q", tc.r, buf.String())
}
}
if tc.close && !res.(*Response).res.Close {
t.Error("expected the connection to be closed by the server but it wasn't")
}
wait, ok := getWaitChan(env)
if !ok {
t.Fatal("could not get wait chan")
}
if tc.wait {
<-wait
}
}
}
for i, tc := range tcs {
t.Run(fmt.Sprintf("%d-%s", i, strings.Join(tc.path, "/")), mkTest(tc))
}
}
type nopCloser struct{}
func (nopCloser) Close() error { return nil }
type readCloser struct {
io.Reader
io.Closer
}