Skip to content

Commit

Permalink
Add tests for DefaultLogFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
sairoutine committed Dec 7, 2018
1 parent bae4c3a commit 2f1ee78
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions logger_test.go
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -225,6 +226,34 @@ func TestLoggerWithConfigFormatting(t *testing.T) {

}

func TestDefaultLogFormatter(t *testing.T) {
termFalseParam := LogFormatterParams{
TimeStamp: time.Unix(1544173902, 0),
StatusCode: 200,
Latency: time.Second * 5,
ClientIP: "20.20.20.20",
Method: "GET",
Path: "/",
ErrorMessage: "",
IsTerm: false,
}

termTrueParam := LogFormatterParams{
TimeStamp: time.Unix(1544173902, 0),
StatusCode: 200,
Latency: time.Second * 5,
ClientIP: "20.20.20.20",
Method: "GET",
Path: "/",
ErrorMessage: "",
IsTerm: true,
}

assert.Equal(t, "[GIN] 2018/12/07 - 18:11:42 | 200 | 5s | 20.20.20.20 | GET /\n", DefaultLogFormatter(termFalseParam))

assert.Equal(t, "[GIN] 2018/12/07 - 18:11:42 |\x1b[97;42m 200 \x1b[0m| 5s | 20.20.20.20 |\x1b[97;44m GET \x1b[0m /\n", DefaultLogFormatter(termTrueParam))
}

func TestColorForMethod(t *testing.T) {
assert.Equal(t, string([]byte{27, 91, 57, 55, 59, 52, 52, 109}), colorForMethod("GET"), "get should be blue")
assert.Equal(t, string([]byte{27, 91, 57, 55, 59, 52, 54, 109}), colorForMethod("POST"), "post should be cyan")
Expand Down

0 comments on commit 2f1ee78

Please sign in to comment.