-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_mwhandler_test.go
126 lines (118 loc) · 3.36 KB
/
example_mwhandler_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
117
118
119
120
121
122
123
124
125
126
package nvelope_test
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"time"
"github.com/muir/nape"
"github.com/muir/nvelope"
"github.com/gorilla/mux"
)
func RequestTimingMiddlewareHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("timing start")
before := time.Now()
next.ServeHTTP(w, r)
after := time.Now()
duration := after.Sub(before)
fmt.Println("timing end, Request took", duration.Round(time.Hour))
})
}
func AuthenticationMiddlewareHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("authentication start")
a := r.Header.Get("Authentication")
if a != "good" {
w.WriteHeader(401)
_, _ = w.Write([]byte("Invalid authentication"))
fmt.Println("authentication end (failed)")
return
}
next.ServeHTTP(w, r)
fmt.Println("authentication end")
})
}
func AuthorizationMiddlewareHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("authorization start")
vars := mux.Vars(r)
if vars["with"] != "john" {
w.WriteHeader(403)
_, _ = w.Write([]byte("Invalid authorization"))
fmt.Println("authorization end (failed)")
return
}
next.ServeHTTP(w, r)
fmt.Println("authorization end")
})
}
func ServiceWithMiddlewareHandler(router *mux.Router) {
service := nape.RegisterServiceWithMux("example", router)
service.RegisterEndpoint("/a/path/{with}/{parameters}",
// order matters and this is a correct order
nvelope.MiddlewareHandlerBaseWriter(RequestTimingMiddlewareHandler),
nvelope.NoLogger,
nvelope.InjectWriter,
nvelope.AutoFlushWriter, // because middlware won't Flush()
nvelope.MiddlewareHandlerDeferredWriter(AuthenticationMiddlewareHandler, AuthorizationMiddlewareHandler),
nvelope.EncodeJSON,
nvelope.CatchPanic,
func() (nvelope.Response, error) {
fmt.Println("thing")
return "did a thing", nil
},
).Methods("GET")
}
// Example shows an injection chain handling a single endpoint using nject,
// nape, and nvelope.
func ExampleServiceWithMiddlewareHandler() {
r := mux.NewRouter()
ServiceWithMiddlewareHandler(r)
ts := httptest.NewServer(r)
client := ts.Client()
doGet := func(url string, authHeader string) {
req, err := http.NewRequestWithContext(context.Background(), "GET", ts.URL+url, nil)
if err != nil {
fmt.Println("request error:", err)
return
}
req.Header.Set("Authentication", authHeader)
res, err := client.Do(req)
if err != nil {
fmt.Println("response error:", err)
return
}
b, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println("read error:", err)
return
}
res.Body.Close()
fmt.Println(res.StatusCode, "->"+string(b))
}
doGet("/a/path/john/37", "good")
doGet("/a/path/john/37", "bad")
doGet("/a/path/fred/37", "good")
// Output: timing start
// authentication start
// authorization start
// thing
// authorization end
// authentication end
// timing end, Request took 0s
// 200 ->"did a thing"
// timing start
// authentication start
// authentication end (failed)
// timing end, Request took 0s
// 401 ->Invalid authentication
// timing start
// authentication start
// authorization start
// authorization end (failed)
// authentication end
// timing end, Request took 0s
// 403 ->Invalid authorization
}