repair some tests and add new tests for logger and server error handler

This commit is contained in:
wernerr 2020-06-07 14:43:25 +02:00
parent fa0f34e585
commit ee3b44f9d1
4 changed files with 62 additions and 4 deletions

View File

@ -6,9 +6,11 @@ package fiber
import (
"errors"
"fmt"
"io/ioutil"
"net"
"net/http/httptest"
"regexp"
"strings"
"testing"
"time"
@ -34,6 +36,33 @@ func Test_App_Routes(t *testing.T) {
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) {
app := New()

View File

@ -446,7 +446,7 @@ func Test_Ctx_FormFile(t *testing.T) {
req := httptest.NewRequest(MethodPost, "/test", body)
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)
utils.AssertEqual(t, nil, err, "app.Test(req)")

View File

@ -15,7 +15,7 @@ func Test_Middleware_Compress(t *testing.T) {
app.Use(Compress())
app.Get("/", func(c *fiber.Ctx) {
c.SendFile("../ctx.go")
c.SendFile("../ctx.go", true)
})
req := httptest.NewRequest("GET", "/", nil)

View File

@ -2,7 +2,10 @@ package middleware
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"testing"
"github.com/gofiber/fiber"
@ -12,8 +15,8 @@ import (
// go test -run Test_Middleware_Logger
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}"
expect := "0.0.0.0--/test?query=query-example.com-GET-/test-http-/test-ref-ua-0s-500--error-5-0-header-query-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-500--error-5-0-header-query-cookie"
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
@ -41,3 +44,29 @@ func Test_Middleware_Logger(t *testing.T) {
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()),
)
}