This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
94 lines (74 loc) · 2.36 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
log "github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestRawOutput(t *testing.T) {
log, status := run(t, "text/plain", "a-b-c")
assert.Equal(t, http.StatusOK, status)
assert.Contains(t, log, "\"msg\":\"a-b-c\"")
assert.Contains(t, log, "test-ns")
assert.Contains(t, log, "test-ot")
assert.Contains(t, log, "test-on")
}
func TestRawMultiLineOutput(t *testing.T) {
log, status := run(t, "text/plain", "a-b-c\nd-e-f")
assert.Equal(t, http.StatusOK, status)
assert.Contains(t, log, "\"msg\":\"a-b-c\"")
assert.Contains(t, log, "\"msg\":\"d-e-f\"")
}
func TestJSONOutput(t *testing.T) {
log, status := run(t, "application/json", "{\"a\":1}")
assert.Equal(t, http.StatusOK, status)
assert.Contains(t, log, "{\"a\":1,")
assert.Contains(t, log, "test-ns")
assert.Contains(t, log, "test-ot")
assert.Contains(t, log, "test-on")
}
func TestJSONMultiOutput(t *testing.T) {
log, status := run(t, "application/json", "{\"a\":1}\n{\"b\":1}\n")
assert.Equal(t, http.StatusOK, status)
assert.Contains(t, log, "{\"a\":1,")
assert.Contains(t, log, "{\"b\":1,")
}
func TestJSONOutputMsgFromOriginal(t *testing.T) {
log, status := run(t, "application/json", "{\"msg\":\"a\"}")
assert.Equal(t, http.StatusOK, status)
assert.Contains(t, log, "\"msg\":\"a\",")
assert.NotContains(t, log, "\"fields.msg\"")
assert.Contains(t, log, "test-ns")
assert.Contains(t, log, "test-ot")
assert.Contains(t, log, "test-on")
}
func TestBadJSONOutput(t *testing.T) {
log, status := run(t, "application/json", "{\"a:1}")
assert.Equal(t, http.StatusBadRequest, status)
assert.Contains(t, log, "\"msg\":\"{\\\"a:1}")
assert.Contains(t, log, "test-ns")
assert.Contains(t, log, "test-ot")
assert.Contains(t, log, "test-on")
}
func run(t *testing.T, contenttype, content string) (string, int) {
buf := bytes.NewBuffer([]byte{})
format := new(log.JSONFormatter)
format.TimestampFormat = "2006-01-02 15:04:05"
logger := &log.Logger{
Out: buf,
Formatter: format,
Hooks: make(log.LevelHooks),
Level: log.InfoLevel,
}
r := CreateRouter(logger)
ts := httptest.NewServer(r)
defer ts.Close()
url := ts.URL + "/log/test-ns/test-ot/test-on"
resp, err := http.Post(url, contenttype, bytes.NewBufferString(content))
if err != nil {
t.Fatal(err)
}
return buf.String(), resp.StatusCode
}