forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiprefix_test.go
68 lines (60 loc) · 1.31 KB
/
apiprefix_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
package http
import (
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestAPIPrefixHandler(t *testing.T) {
type testcase struct {
prefix string
reqURL string
nextReqURL string
respBody string
nextCalled bool
status int
}
tcs := []testcase{
{
prefix: "/api/v0",
reqURL: "/api/v0/version",
nextReqURL: "/version",
respBody: "ok",
nextCalled: true,
status: 200,
},
{
prefix: "/api/v0",
reqURL: "/api/v1/version",
nextReqURL: "/version",
respBody: "404 page not found\n",
nextCalled: false,
status: 404,
},
}
assert := func(name string, exp, real interface{}) {
if exp != real {
t.Errorf("expected %s to be %q, but got %q", name, exp, real)
} else {
t.Log("ok:", name)
}
}
for _, tc := range tcs {
var (
called bool
h http.Handler
)
r := httptest.NewRequest("", tc.reqURL, nil)
w := httptest.NewRecorder()
h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
assert("next r.URL.Path", tc.nextReqURL, r.URL.Path)
io.WriteString(w, "ok")
})
h = newPrefixHandler(tc.prefix, h)
h.ServeHTTP(w, r)
assert("called", tc.nextCalled, called)
assert("response status", tc.status, w.Code)
assert("response body", tc.respBody, w.Body.String())
}
}