-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mount_test.go
116 lines (91 loc) · 3.28 KB
/
Mount_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
package gorest_test
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/adamluzsi/testcase"
"github.com/stretchr/testify/require"
"github.com/adamluzsi/gorest"
)
var (
_ gorest.Multiplexer = http.NewServeMux()
_ gorest.Multiplexer = &gorest.Handler{}
)
type mux interface {
gorest.Multiplexer
http.Handler
}
func TestMount(t *testing.T) {
s := testcase.NewSpec(t)
var subject = func(t *testcase.T) {
gorest.Mount(
t.I(`multiplexer`).(gorest.Multiplexer),
t.I(`pattern`).(string),
t.I(`handler`).(http.Handler),
)
}
var multiplexer = func(t *testcase.T) mux { return t.I(`multiplexer`).(*http.ServeMux) }
s.Let(`multiplexer`, func(t *testcase.T) interface{} { return http.NewServeMux() })
s.Let(`handler`, func(t *testcase.T) interface{} { return gorest.NewHandler(TestController{}) })
s.When(`pattern lack trailing slash`, func(s *testcase.Spec) {
s.Let(`pattern`, func(t *testcase.T) interface{} { return `/path0` })
s.Then(`it will be still available to call even for the under paths`, func(t *testcase.T) {
subject(t)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, `/path0/123`, nil)
multiplexer(t).ServeHTTP(w, r)
require.Contains(t, w.Body.String(), `show:123`)
})
})
s.When(`pattern lack leading slash`, func(s *testcase.Spec) {
s.Let(`pattern`, func(t *testcase.T) interface{} { return `path1/` })
s.Then(`it will be still available to call even for the under paths`, func(t *testcase.T) {
subject(t)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, `/path1/123`, nil)
multiplexer(t).ServeHTTP(w, r)
require.Contains(t, w.Body.String(), `show:123`)
})
})
s.When(`pattern lack leading and trailing slash`, func(s *testcase.Spec) {
s.Let(`pattern`, func(t *testcase.T) interface{} { return `path2` })
s.Then(`it will be still available to call even for the under paths`, func(t *testcase.T) {
subject(t)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, `/path2/123`, nil)
multiplexer(t).ServeHTTP(w, r)
require.Contains(t, w.Body.String(), `show:123`)
})
})
s.When(`pattern includes nested path`, func(s *testcase.Spec) {
s.Let(`pattern`, func(t *testcase.T) interface{} { return `/test/this/out/` })
s.Then(`it will be still available to call even for the under paths`, func(t *testcase.T) {
subject(t)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, `/test/this/out/123`, nil)
multiplexer(t).ServeHTTP(w, r)
require.Contains(t, w.Body.String(), `show:123`)
})
})
s.Test(`E2E`, func(t *testcase.T) {
ctxHandler := gorest.DefaultContextHandler{ContextKey: `resourceID`}
resources := gorest.NewHandler(struct {
gorest.ContextHandler
gorest.ShowController
}{
ContextHandler: ctxHandler,
ShowController: gorest.AsShowController(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintf(w, `%s`, ctxHandler.GetResourceID(r.Context()))
})),
})
mux := http.NewServeMux()
gorest.Mount(mux, `/routes`, resources)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, `/routes/resourceID/`, &bytes.Buffer{})
mux.ServeHTTP(w, r)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, `resourceID`, w.Body.String())
})
}