Added middleware support

This commit is contained in:
2024-03-13 20:18:01 +01:00
parent 02328cb41e
commit c13dbc55d2
9 changed files with 135 additions and 157 deletions

View File

@ -39,24 +39,21 @@ func TestRouter(t *testing.T) {
})
s.Get("/request/data", func(ctx server.Context) error {
request := ctx.Request()
method := request.Method()
protocol := request.Protocol()
host := request.Host()
path := request.Path()
method := ctx.Method()
protocol := ctx.Protocol()
host := ctx.Host()
path := ctx.Path()
return ctx.String(fmt.Sprintf("%s %s %s %s", method, protocol, host, path))
})
s.Get("/request/header", func(ctx server.Context) error {
request := ctx.Request()
acceptEncoding := request.Header("Accept-Encoding")
acceptEncoding := ctx.RequestHeader("Accept-Encoding")
return ctx.String(acceptEncoding)
})
s.Get("/response/header", func(ctx server.Context) error {
response := ctx.Response()
response.SetHeader("Content-Type", "text/plain")
contentType := response.Header("Content-Type")
ctx.Header("Content-Type", "text/plain")
contentType := ctx.ResponseHeader("Content-Type")
return ctx.String(contentType)
})
@ -120,6 +117,21 @@ func TestRouter(t *testing.T) {
}
}
func TestMiddleware(t *testing.T) {
s := server.New()
s.Use(func(ctx server.Context) error {
ctx.Header("Middleware", "true")
return ctx.Next()
})
request := httptest.NewRequest(http.MethodGet, "/", nil)
response := httptest.NewRecorder()
s.ServeHTTP(response, request)
assert.Equal(t, response.Header().Get("Middleware"), "true")
}
func TestPanic(t *testing.T) {
s := server.New()