forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
215 lines (185 loc) · 4.35 KB
/
parse_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
package http
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"testing"
cmds "github.com/ipfs/go-ipfs-cmds"
)
func TestParse(t *testing.T) {
root := &cmds.Command{
Subcommands: map[string]*cmds.Command{
"block": {
Subcommands: map[string]*cmds.Command{
"put": {
Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
defer resp.Close()
resp.Emit("done")
return nil
},
},
},
},
},
}
r, err := http.NewRequest("GET", "/block/put", nil)
if err != nil {
t.Fatal(err)
}
req, err := parseRequest(r, root)
if err != nil {
t.Fatal(err)
}
pth := req.Path
if pth[0] != "block" || pth[1] != "put" || len(pth) != 2 {
t.Errorf("incorrect path %v, expected %v", pth, []string{"block", "put"})
}
r, err = http.NewRequest("GET", "/block/bla", nil)
if err != nil {
t.Fatal(err)
}
_, err = parseRequest(r, root)
if err != ErrNotFound {
t.Errorf("expected ErrNotFound, got: %v", err)
}
}
type parseReqTestCase struct {
path string
opts url.Values
body io.Reader
cmdsReq *cmds.Request
err error
}
func (tc parseReqTestCase) test(t *testing.T) {
var vs = url.Values{}
for k, opts := range tc.opts {
for _, opt := range opts {
vs.Add(k, opt)
}
}
// we're just parsing the request, so the host part of the url doens't really matter
httpReq, err := http.NewRequest("GET", "http://127.0.0.1:5001"+tc.path, tc.body)
if err != nil {
t.Fatal(err)
}
httpReq.URL.RawQuery = vs.Encode()
req, err := parseRequest(httpReq, cmdRoot)
if !errEq(err, tc.err) {
t.Fatalf("expected error to be %v, but got %v", tc.err, err)
}
if err != nil {
return
}
if req.Command != tc.cmdsReq.Command {
t.Errorf("expected req.Command to be\n%v\n but got\n%v", tc.cmdsReq.Command, req.Command)
}
if !reflect.DeepEqual(req.Path, tc.cmdsReq.Path) {
t.Errorf("expected req.Path to be %v, but got %v", tc.cmdsReq.Path, req.Path)
}
if !reflect.DeepEqual(req.Arguments, tc.cmdsReq.Arguments) {
t.Errorf("expected req.Arguments to be %v, but got %v", tc.cmdsReq.Arguments, req.Arguments)
}
if !reflect.DeepEqual(req.Options, tc.cmdsReq.Options) {
t.Errorf("expected req.Options to be %v, but got %v", tc.cmdsReq.Options, req.Options)
}
}
func TestParseRequest(t *testing.T) {
tcs := []parseReqTestCase{
{
path: "/version",
opts: url.Values{
"all": []string{"true"},
},
cmdsReq: &cmds.Request{
Command: cmdRoot.Subcommands["version"],
Path: []string{"version"},
Arguments: []string{},
Options: cmds.OptMap{
"all": true,
cmds.EncLong: cmds.JSON,
},
},
},
}
for _, tc := range tcs {
tc.test(t)
}
}
type parseRespTestCase struct {
status int
header http.Header
body io.ReadCloser
values []interface{}
err error
}
func (tc parseRespTestCase) test(t *testing.T) {
httpResp := &http.Response{
StatusCode: tc.status,
Header: tc.header,
Body: tc.body,
}
resp, err := parseResponse(httpResp, &cmds.Request{Command: cmdRoot.Subcommands["version"]})
if !errEq(err, tc.err) {
t.Fatalf("expected error to be %v, but got %v", tc.err, err)
}
if err != nil {
return
}
t.Log(resp.(*Response).dec)
for _, v := range tc.values {
val, err := resp.Next()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(v, val) {
t.Fatalf("expected %v(%T) but got %v(%T)", v, v, val, val)
}
}
_, err = resp.Next()
if err != io.EOF {
t.Fatalf("expected EOF but got %v", err)
}
}
type fakeCloser struct {
io.Reader
}
func (c fakeCloser) Close() error { return nil }
func mkbuf(str string) io.ReadCloser {
buf := bytes.NewBuffer(nil)
buf.WriteString(str)
return fakeCloser{buf}
}
func TestParseResponse(t *testing.T) {
tcs := []parseRespTestCase{
{
status: 200,
header: http.Header{
contentTypeHeader: []string{"application/json"},
channelHeader: []string{"1"},
},
body: mkbuf(`{"Version":"0.1.2", "Commit":"c0mm17", "Repo":"4"}`),
values: []interface{}{
&VersionOutput{
Version: "0.1.2",
Commit: "c0mm17",
Repo: "4",
},
},
},
{
status: 500,
header: http.Header{
contentTypeHeader: []string{"evil/bad"},
channelHeader: []string{"1"},
},
body: mkbuf("test error"),
err: fmt.Errorf("unknown error content type: %s", "evil/bad"),
},
}
for _, tc := range tcs {
tc.test(t)
}
}