forked from celrenheit/lion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_test.go
83 lines (71 loc) · 2.9 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
package lion
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
type testResource struct{}
func (tr testResource) Uses() Middlewares { return Middlewares{} }
func (tr testResource) GetMiddlewares() Middlewares { return Middlewares{newTestResMW("Get")} }
func (tr testResource) HeadMiddlewares() Middlewares { return Middlewares{newTestResMW("Head")} }
func (tr testResource) PostMiddlewares() Middlewares { return Middlewares{newTestResMW("Post")} }
func (tr testResource) PutMiddlewares() Middlewares { return Middlewares{newTestResMW("Put")} }
func (tr testResource) DeleteMiddlewares() Middlewares { return Middlewares{newTestResMW("Delete")} }
func (tr testResource) TraceMiddlewares() Middlewares { return Middlewares{newTestResMW("Trace")} }
func (tr testResource) OptionsMiddlewares() Middlewares { return Middlewares{newTestResMW("Options")} }
func (tr testResource) ConnectMiddlewares() Middlewares { return Middlewares{newTestResMW("Connect")} }
func (tr testResource) PatchMiddlewares() Middlewares { return Middlewares{newTestResMW("Patch")} }
func (tr testResource) Get(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Get")
}
func (tr testResource) Head(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Head")
}
func (tr testResource) Post(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Post")
}
func (tr testResource) Put(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Put")
}
func (tr testResource) Delete(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Delete")
}
func (tr testResource) Trace(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Trace")
}
func (tr testResource) Options(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Options")
}
func (tr testResource) Connect(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Connect")
}
func (tr testResource) Patch(c Context) {
c.String("Patch")
}
func TestResources(t *testing.T) {
methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "OPTIONS", "CONNECT", "PATCH"}
expected := []string{"Get", "Head", "Post", "Put", "Delete", "Trace", "Options", "Connect", "Patch"}
tr := testResource{}
r := New()
r.Resource("/testpath", tr)
for i := 0; i < len(methods); i++ {
w := httptest.NewRecorder()
req, _ := http.NewRequest(methods[i], "/testpath", nil)
r.ServeHTTP(w, req)
if w.Body.String() != expected[i] {
t.Errorf("[Resource] Expected body %s but got %s for http method %s", expected[i], w.Body.String(), methods[i])
}
if w.Header().Get("foo") != expected[i] {
t.Errorf("[Resource] Expected header %s but got %s for http method %s", expected[i], w.Header().Get("foo"), methods[i])
}
}
}
func newTestResMW(header string) Middleware {
return MiddlewareFunc(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("foo", header)
next.ServeHTTP(w, r)
})
})
}