Skip to content

Commit 1b031cb

Browse files
committed
Added tests for colorForMethod() and colorForStatus()
1 parent 49415b1 commit 1b031cb

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

logger_test.go

+63-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,72 @@ func TestLogger(t *testing.T) {
2626
router := New()
2727
router.Use(LoggerWithWriter(buffer))
2828
router.GET("/example", func(c *Context) {})
29+
router.POST("/example", func(c *Context) {})
30+
router.PUT("/example", func(c *Context) {})
31+
router.DELETE("/example", func(c *Context) {})
32+
router.PATCH("/example", func(c *Context) {})
33+
router.HEAD("/example", func(c *Context) {})
34+
router.OPTIONS("/example", func(c *Context) {})
2935

3036
performRequest(router, "GET", "/example")
31-
3237
assert.Contains(t, buffer.String(), "200")
3338
assert.Contains(t, buffer.String(), "GET")
3439
assert.Contains(t, buffer.String(), "/example")
40+
41+
// I wrote these first (extending the above) but then realized they are more
42+
// like integration tests because they test the whole logging process rather
43+
// than individual functions. Im not sure where these should go.
44+
45+
performRequest(router, "POST", "/example")
46+
assert.Contains(t, buffer.String(), "200")
47+
assert.Contains(t, buffer.String(), "POST")
48+
assert.Contains(t, buffer.String(), "/example")
49+
50+
performRequest(router, "PUT", "/example")
51+
assert.Contains(t, buffer.String(), "200")
52+
assert.Contains(t, buffer.String(), "PUT")
53+
assert.Contains(t, buffer.String(), "/example")
54+
55+
performRequest(router, "DELETE", "/example")
56+
assert.Contains(t, buffer.String(), "200")
57+
assert.Contains(t, buffer.String(), "DELETE")
58+
assert.Contains(t, buffer.String(), "/example")
59+
60+
performRequest(router, "PATCH", "/example")
61+
assert.Contains(t, buffer.String(), "200")
62+
assert.Contains(t, buffer.String(), "PATCH")
63+
assert.Contains(t, buffer.String(), "/example")
64+
65+
performRequest(router, "HEAD", "/example")
66+
assert.Contains(t, buffer.String(), "200")
67+
assert.Contains(t, buffer.String(), "HEAD")
68+
assert.Contains(t, buffer.String(), "/example")
69+
70+
performRequest(router, "OPTIONS", "/example")
71+
assert.Contains(t, buffer.String(), "200")
72+
assert.Contains(t, buffer.String(), "OPTIONS")
73+
assert.Contains(t, buffer.String(), "/example")
74+
75+
performRequest(router, "GET", "/notfound")
76+
assert.Contains(t, buffer.String(), "404")
77+
assert.Contains(t, buffer.String(), "GET")
78+
assert.Contains(t, buffer.String(), "/notfound")
79+
80+
}
81+
82+
func TestColorForMethod(t *testing.T) {
83+
assert.Equal(t, colorForMethod("GET"), string([]byte{27, 91, 57, 55, 59, 52, 52, 109}), "get should be blue")
84+
assert.Equal(t, colorForMethod("POST"), string([]byte{27, 91, 57, 55, 59, 52, 54, 109}), "post should be cyan")
85+
assert.Equal(t, colorForMethod("PUT"), string([]byte{27, 91, 57, 55, 59, 52, 51, 109}), "put should be yellow")
86+
assert.Equal(t, colorForMethod("DELETE"), string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), "delete should be red")
87+
assert.Equal(t, colorForMethod("PATCH"), string([]byte{27, 91, 57, 55, 59, 52, 50, 109}), "patch should be green")
88+
assert.Equal(t, colorForMethod("HEAD"), string([]byte{27, 91, 57, 55, 59, 52, 53, 109}), "head should be magenta")
89+
assert.Equal(t, colorForMethod("OPTIONS"), string([]byte{27, 91, 57, 48, 59, 52, 55, 109}), "options should be white")
90+
}
91+
92+
func TestColorForStatus(t *testing.T) {
93+
assert.Equal(t, colorForStatus(200), string([]byte{27, 91, 57, 55, 59, 52, 50, 109}), "2xx should be green")
94+
assert.Equal(t, colorForStatus(301), string([]byte{27, 91, 57, 48, 59, 52, 55, 109}), "3xx should be white")
95+
assert.Equal(t, colorForStatus(404), string([]byte{27, 91, 57, 55, 59, 52, 51, 109}), "4xx should be yellow")
96+
assert.Equal(t, colorForStatus(2), string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), "other things should be red")
3597
}

0 commit comments

Comments
 (0)