mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
repair some tests and add new tests for logger and server error handler
This commit is contained in:
parent
fa0f34e585
commit
ee3b44f9d1
29
app_test.go
29
app_test.go
@ -6,9 +6,11 @@ package fiber
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -34,6 +36,33 @@ func Test_App_Routes(t *testing.T) {
|
|||||||
utils.AssertEqual(t, 3, len(app.Routes()))
|
utils.AssertEqual(t, 3, len(app.Routes()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_App_ServerErrorHandler_SmallReadBuffer(t *testing.T) {
|
||||||
|
expectedError := regexp.MustCompile(
|
||||||
|
`error when reading request headers: small read buffer\. Increase ReadBufferSize\. Buffer size=4096, contents: "GET / HTTP/1.1\\r\\nHost: example\.com\\r\\nVery-Long-Header: -+`,
|
||||||
|
)
|
||||||
|
app := New()
|
||||||
|
|
||||||
|
app.Get("/", func(c *Ctx) {
|
||||||
|
panic(errors.New("Should never called"))
|
||||||
|
})
|
||||||
|
|
||||||
|
request := httptest.NewRequest("GET", "/", nil)
|
||||||
|
logHeaderSlice := make([]string, 5000, 5000)
|
||||||
|
request.Header.Set("Very-Long-Header", strings.Join(logHeaderSlice, "-"))
|
||||||
|
_, err := app.Test(request)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expect an error at app.Test(request)")
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.AssertEqual(
|
||||||
|
t,
|
||||||
|
true,
|
||||||
|
expectedError.MatchString(err.Error()),
|
||||||
|
fmt.Sprintf("Has: %s, expected pattern: %s", err.Error(), expectedError.String()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func Test_App_ErrorHandler(t *testing.T) {
|
func Test_App_ErrorHandler(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
|
||||||
|
@ -446,7 +446,7 @@ func Test_Ctx_FormFile(t *testing.T) {
|
|||||||
|
|
||||||
req := httptest.NewRequest(MethodPost, "/test", body)
|
req := httptest.NewRequest(MethodPost, "/test", body)
|
||||||
req.Header.Set(HeaderContentType, writer.FormDataContentType())
|
req.Header.Set(HeaderContentType, writer.FormDataContentType())
|
||||||
//req.Header.Set(HeaderContentLength, strconv.Itoa(len(body.Bytes())))
|
req.Header.Set(HeaderContentLength, strconv.Itoa(len(body.Bytes())))
|
||||||
|
|
||||||
resp, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||||
|
@ -15,7 +15,7 @@ func Test_Middleware_Compress(t *testing.T) {
|
|||||||
app.Use(Compress())
|
app.Use(Compress())
|
||||||
|
|
||||||
app.Get("/", func(c *fiber.Ctx) {
|
app.Get("/", func(c *fiber.Ctx) {
|
||||||
c.SendFile("../ctx.go")
|
c.SendFile("../ctx.go", true)
|
||||||
})
|
})
|
||||||
|
|
||||||
req := httptest.NewRequest("GET", "/", nil)
|
req := httptest.NewRequest("GET", "/", nil)
|
||||||
|
@ -2,7 +2,10 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gofiber/fiber"
|
"github.com/gofiber/fiber"
|
||||||
@ -12,8 +15,8 @@ import (
|
|||||||
|
|
||||||
// go test -run Test_Middleware_Logger
|
// go test -run Test_Middleware_Logger
|
||||||
func Test_Middleware_Logger(t *testing.T) {
|
func Test_Middleware_Logger(t *testing.T) {
|
||||||
format := "${ip}-${ips}-${url}-${host}-${method}-${path}-${protocol}-${route}-${referer}-${ua}-${latency}-${status}-${body}-${error}-${bytesSent}-${bytesReceived}-${header:header}-${query:query}-${cookie:cookie}"
|
format := "${ip}-${ips}-${url}-${host}-${method}-${path}-${protocol}-${route}-${referer}-${ua}-${status}-${body}-${error}-${bytesSent}-${bytesReceived}-${header:header}-${query:query}-${cookie:cookie}"
|
||||||
expect := "0.0.0.0--/test?query=query-example.com-GET-/test-http-/test-ref-ua-0s-500--error-5-0-header-query-cookie"
|
expect := "0.0.0.0--/test?query=query-example.com-GET-/test-http-/test-ref-ua-500--error-5-0-header-query-cookie"
|
||||||
|
|
||||||
buf := bytebufferpool.Get()
|
buf := bytebufferpool.Get()
|
||||||
defer bytebufferpool.Put(buf)
|
defer bytebufferpool.Put(buf)
|
||||||
@ -41,3 +44,29 @@ func Test_Middleware_Logger(t *testing.T) {
|
|||||||
utils.AssertEqual(t, expect, buf.String())
|
utils.AssertEqual(t, expect, buf.String())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Middleware_Logger_WithDefaulFormat(t *testing.T) {
|
||||||
|
expectedOutputPattern := regexp.MustCompile(`^\d{2}:\d{2}:\d{2} GET / - 0\.0\.0\.0 - 200 - \d+(\.\d+)?.{1,3}
|
||||||
|
$`)
|
||||||
|
// fake output
|
||||||
|
buf := bytebufferpool.Get()
|
||||||
|
defer bytebufferpool.Put(buf)
|
||||||
|
|
||||||
|
config := LoggerConfigDefault
|
||||||
|
config.Output = buf
|
||||||
|
app := fiber.New(&fiber.Settings{DisableStartupMessage: true})
|
||||||
|
app.Use(LoggerWithConfig(config))
|
||||||
|
|
||||||
|
app.Get("/", func(ctx *fiber.Ctx) {
|
||||||
|
ctx.SendStatus(200)
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err := app.Test(httptest.NewRequest(http.MethodGet, "/", nil))
|
||||||
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||||
|
utils.AssertEqual(
|
||||||
|
t,
|
||||||
|
true,
|
||||||
|
expectedOutputPattern.MatchString(buf.String()),
|
||||||
|
fmt.Sprintf("Has: %s, expected pattern: %s", buf.String(), expectedOutputPattern.String()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user