mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 17:42:14 +08:00
Add tests for DefaultLogFormatter
This commit is contained in:
parent
bae4c3a064
commit
e6bbd22614
@ -10,6 +10,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@ -225,6 +226,36 @@ func TestLoggerWithConfigFormatting(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDefaultLogFormatter(t *testing.T) {
|
||||||
|
timeStamp := time.Unix(1544173902, 0).UTC()
|
||||||
|
|
||||||
|
termFalseParam := LogFormatterParams{
|
||||||
|
TimeStamp: timeStamp,
|
||||||
|
StatusCode: 200,
|
||||||
|
Latency: time.Second * 5,
|
||||||
|
ClientIP: "20.20.20.20",
|
||||||
|
Method: "GET",
|
||||||
|
Path: "/",
|
||||||
|
ErrorMessage: "",
|
||||||
|
IsTerm: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
termTrueParam := LogFormatterParams{
|
||||||
|
TimeStamp: timeStamp,
|
||||||
|
StatusCode: 200,
|
||||||
|
Latency: time.Second * 5,
|
||||||
|
ClientIP: "20.20.20.20",
|
||||||
|
Method: "GET",
|
||||||
|
Path: "/",
|
||||||
|
ErrorMessage: "",
|
||||||
|
IsTerm: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 5s | 20.20.20.20 | GET /\n", DefaultLogFormatter(termFalseParam))
|
||||||
|
|
||||||
|
assert.Equal(t, "[GIN] 2018/12/07 - 09: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) {
|
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, 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")
|
assert.Equal(t, string([]byte{27, 91, 57, 55, 59, 52, 54, 109}), colorForMethod("POST"), "post should be cyan")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user