-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_test.go
93 lines (77 loc) · 1.89 KB
/
resource_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 web
import (
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestResource(t *testing.T) {
handler := func(n string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s %s", r.Method, n)
}
}
rs := &Resource{
Index: handler("index"),
Show: handler("show"),
Create: handler("create"),
Replace: handler("replace"),
Update: handler("update"),
Delete: handler("delete"),
}
var body string
var c int
_, body = testResource(t, "GET", "/users/", rs)
assert.Equal(t, "GET index", body)
_, body = testResource(t, "POST", "/users/", rs)
assert.Equal(t, "POST create", body)
_, body = testResource(t, "GET", "/users/a", rs)
assert.Equal(t, "GET show", body)
_, body = testResource(t, "PATCH", "/users/a", rs)
assert.Equal(t, "PATCH update", body)
_, body = testResource(t, "PUT", "/users/a", rs)
assert.Equal(t, "PUT replace", body)
_, body = testResource(t, "DELETE", "/users/a", rs)
assert.Equal(t, "DELETE delete", body)
// route not found
c, _ = testResource(t, "GET", "/users/a/b", rs)
assert.Equal(t, 404, c)
// wrong method
c, _ = testResource(t, "POST", "/users/a", rs)
assert.Equal(t, 405, c)
}
func TestParseComponents(t *testing.T) {
tests := []struct {
path string
exp []string
}{
{
"/abc",
[]string{"abc"},
},
{
"/a/bc/cde",
[]string{"a", "bc", "cde"},
},
{
"/a/bc/cde/",
[]string{"a", "bc", "cde"},
},
}
for _, test := range tests {
paths := PathParts(test.path)
assert.Equal(t, test.exp, paths)
}
}
func testResource(t testing.TB, method, path string, f http.Handler) (int, string) {
req, err := http.NewRequest(method, "http://example.com"+path, nil)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
mux := http.NewServeMux()
mux.Handle("/users/", f)
mux.ServeHTTP(w, req)
return w.Code, w.Body.String()
}