Skip to content

Commit

Permalink
test(requestlogger): add unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianccm committed May 18, 2022
1 parent 0e52661 commit c8c1847
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 7 deletions.
27 changes: 20 additions & 7 deletions core/requestlogger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/web"
"github.com/labstack/gommon/color"
)

type (
Expand Down Expand Up @@ -72,18 +71,32 @@ func humanBytes(bc int) string {
return strconv.Itoa(bc) + "b"
}

func statusCodeColor(code int) func(msg interface{}, styles ...string) string {
const (
green = "32"
blue = "34"
yellow = "33"
red = "31"
grey = "90"
)

func colored(color string) func(msg string) string {
return func(msg string) string {
return fmt.Sprintf("\x1b[%sm%s\x1b[0m", color, msg)
}
}

func statusCodeColor(code int) func(msg string) string {
switch {
case code >= 200 && code < 300:
return color.Green
return colored(green)
case code >= 300 && code < 400:
return color.Blue
return colored(blue)
case code >= 400 && code < 500:
return color.Yellow
return colored(yellow)
case code == 0 || (code >= 500 && code < 600):
return color.Red
return colored(red)
default:
return color.Grey
return colored(grey)
}
}

Expand Down
65 changes: 65 additions & 0 deletions core/requestlogger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package requestlogger

import (
"bytes"
"context"
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/web"
"github.com/stretchr/testify/assert"
)

func TestLogger(t *testing.T) {
logger := new(logger)

logSink := new(bytes.Buffer)
logger.Inject(&flamingo.StdLogger{Logger: *log.New(logSink, "", 0)}, nil)

recorder := httptest.NewRecorder()
request := web.CreateRequest(httptest.NewRequest(http.MethodPost, "/test", nil), nil)

responder := new(web.Responder).Inject(&web.Router{}, flamingo.NullLogger{}, &struct {
Engine flamingo.TemplateEngine "inject:\",optional\""
Debug bool "inject:\"config:flamingo.debug.mode\""
TemplateForbidden string "inject:\"config:flamingo.template.err403\""
TemplateNotFound string "inject:\"config:flamingo.template.err404\""
TemplateUnavailable string "inject:\"config:flamingo.template.err503\""
TemplateErrorWithCode string "inject:\"config:flamingo.template.errWithCode\""
}{})

tests := []struct {
testCase string
response web.Result
regex string
}{
{testCase: "regular response", response: &web.Response{}, regex: "^\x1b\\[32mPOST /test 200: 0b in \\d+(\\.\\d+)?..\x1b\\[0m\n$"},
{testCase: "http status ok", response: &web.Response{Status: http.StatusOK}, regex: "^\x1b\\[32mPOST /test 200: 0b in \\d+(\\.\\d+)?..\x1b\\[0m\n$"},
{testCase: "url redirect", response: &web.URLRedirectResponse{Response: web.Response{Status: http.StatusSeeOther}, URL: &url.URL{Path: "/foo"}}, regex: "^\x1b\\[34mPOST /test 303: 0b in \\d+(\\.\\d+)?.. \\(-> /foo\\)\x1b\\[0m\n$"},
{testCase: "route redirect", response: responder.RouteRedirect("/route", nil), regex: "^\x1b\\[34mPOST /test 303: 0b in \\d+(\\.\\d+)?.. \\(-> /route\\)\x1b\\[0m\n$"},
{testCase: "http notfound", response: &web.Response{Status: http.StatusNotFound}, regex: "^\x1b\\[33mPOST /test 404: 0b in \\d+(\\.\\d+)?..\x1b\\[0m\n$"},
{testCase: "internal server errror", response: &web.Response{Status: http.StatusInternalServerError}, regex: "^\x1b\\[31mPOST /test 500: 0b in \\d+(\\.\\d+)?..\x1b\\[0m\n$"},
{testCase: "error response", response: &web.ServerErrorResponse{Error: fmt.Errorf("test error")}, regex: "^\x1b\\[31mPOST /test 500: 5b in \\d+(\\.\\d+)?.. \\(Error: test error\\)\x1b\\[0m\n$"},
{testCase: "unknown status code", response: &web.Response{Status: 1}, regex: "^\x1b\\[90mPOST /test 1: 0b in \\d+(\\.\\d+)?..\x1b\\[0m\n$"},
}

for _, test := range tests {
t.Run(test.testCase, func(t *testing.T) {
logSink.Reset()
assert.NoError(t, logger.Filter(context.Background(), request, nil, web.NewFilterChain(func(ctx context.Context, req *web.Request, w http.ResponseWriter) web.Result {
return test.response
})).Apply(context.Background(), recorder))
assert.Regexp(t, test.regex, logSink.String())
})
}
}

func TestHumanBytes(t *testing.T) {
assert.Equal(t, "100b", humanBytes(100))
assert.Equal(t, "100kb", humanBytes(100000))
}

0 comments on commit c8c1847

Please sign in to comment.