-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfasthttpmiddleware_test.go
50 lines (48 loc) · 1.28 KB
/
fasthttpmiddleware_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
package fasthttpmiddleware
import (
"bytes"
"github.com/buaazp/fasthttprouter"
"github.com/hanjm/zaplog"
"github.com/valyala/fasthttp"
"testing"
"time"
)
func TestNewNormalOnion(t *testing.T) {
exampleAuthFunc := func(ctx *fasthttp.RequestCtx) bool {
if bytes.HasPrefix(ctx.Path(), []byte("/protect")) {
return false
} else {
return true
}
}
logger := zaplog.NewNoCallerLogger(false)
middleware := NewNormalMiddlewareOnion(exampleAuthFunc, logger)
requestHandler := func(ctx *fasthttp.RequestCtx) {
ctx.WriteString("hello")
}
panicHandler := func(ctx *fasthttp.RequestCtx) {
panic("test panic")
}
router := fasthttprouter.New()
router.GET("/", middleware.Apply(requestHandler))
router.GET("/protect", middleware.Apply(requestHandler))
router.GET("/panic", middleware.Apply(panicHandler))
go func() {
fasthttp.ListenAndServe(":8000", router.Handler)
}()
time.Sleep(time.Second)
var resp []byte
code, _, _ := fasthttp.Get(resp, "http://127.0.0.1:8000/")
if code != 200 {
t.Fatal("unexpected response")
}
code, _, _ = fasthttp.Get(resp, "http://127.0.0.1:8000/protect")
if code != 403 {
t.Fatal("unexpected response")
}
code, _, _ = fasthttp.Get(resp, "http://127.0.0.1:8000/panic")
if code != 500 {
t.Fatal("unexpected response")
}
time.Sleep(time.Second)
}