forked from go-aah/aah
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_test.go
79 lines (62 loc) · 2.25 KB
/
middleware_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
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// Source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package aah
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"aahframe.work/ahttp"
"github.com/stretchr/testify/assert"
)
// TestHandler ...
type TestHandler struct {
}
func (th *TestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(ahttp.HeaderContentType, "text/html; charset=utf-8")
_, _ = w.Write([]byte("TestHandler.ServeHTTP\n"))
_, _ = w.Write([]byte(r.Method + "--" + r.URL.Path + "\n"))
}
func TestMiddlewareToHandler(t *testing.T) {
a := newApp()
e := a.he
e.Middlewares(
ToMiddleware(thirdPartyMiddleware3),
ToMiddleware(http.HandlerFunc(thirdPartyMiddleware2)),
ToMiddleware(&TestHandler{}),
ToMiddleware(thirdPartyMiddleware1),
ToMiddleware(invaildHandlerType),
)
req := httptest.NewRequest(ahttp.MethodGet, "http://localhost:8080/doc/v0.3/mydoc.html", nil)
ctx := newContext(httptest.NewRecorder(), req)
// Execute the middleware
e.mwChain[0].Next(ctx)
w := ctx.Res.Unwrap().(*httptest.ResponseRecorder)
resp := w.Result()
body := responseBody(resp)
t.Log(body)
assert.Equal(t, http.StatusAccepted, resp.StatusCode)
assert.Equal(t, "text/html; charset=utf-8", resp.Header.Get(ahttp.HeaderContentType))
assert.True(t, strings.Contains(body, "localhost:8080--GET--/doc/v0.3/mydoc.html"))
}
func thirdPartyMiddleware1(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("thirdPartyMiddleware1\n"))
_, _ = w.Write([]byte(r.Method + "--" + r.URL.Path + "\n"))
}
func thirdPartyMiddleware2(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("thirdPartyMiddleware2\n"))
_, _ = w.Write([]byte(r.Host + "--" + r.Method + "--" + r.URL.Path + "\n"))
}
func thirdPartyMiddleware3(w http.ResponseWriter, r *http.Request) {
w.Header().Set(ahttp.HeaderContentType, "text/html; charset=utf-8")
// doesn't make sense right!!!
// just for testing; to differentiate the default 200 code
w.WriteHeader(http.StatusAccepted)
_, _ = w.Write([]byte("thirdPartyMiddleware3\n"))
_, _ = w.Write([]byte(r.Method + "--" + r.URL.Path + "\n"))
}
func invaildHandlerType(e *Event) {
fmt.Println("This is invaild handler type")
}