forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
124 lines (105 loc) · 3.02 KB
/
logger_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package goshopify
import (
"bytes"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"testing"
)
func TestLeveledLogger(t *testing.T) {
tests := []struct {
level int
input string
stdout string
stderr string
}{
{
level: LevelError,
input: "log",
stderr: "[ERROR] error log\n",
stdout: "",
},
{
level: LevelWarn,
input: "log",
stderr: "[ERROR] error log\n[WARN] warn log\n",
stdout: "",
},
{
level: LevelInfo,
input: "log",
stderr: "[ERROR] error log\n[WARN] warn log\n",
stdout: "[INFO] info log\n",
},
{
level: LevelDebug,
input: "log",
stderr: "[ERROR] error log\n[WARN] warn log\n",
stdout: "[INFO] info log\n[DEBUG] debug log\n",
},
}
for _, test := range tests {
err := &bytes.Buffer{}
out := &bytes.Buffer{}
log := &LeveledLogger{Level: test.level, stderrOverride: err, stdoutOverride: out}
log.Errorf("error %s", test.input)
log.Warnf("warn %s", test.input)
log.Infof("info %s", test.input)
log.Debugf("debug %s", test.input)
stdout := out.String()
stderr := err.String()
if stdout != test.stdout {
t.Errorf("leveled logger %d expected stdout \"%s\" received \"%s\"", test.level, test.stdout, stdout)
}
if stderr != test.stderr {
t.Errorf("leveled logger %d expected stderr \"%s\" received \"%s\"", test.level, test.stderr, stderr)
}
}
log := &LeveledLogger{Level: LevelDebug}
if log.stderr() != os.Stderr {
t.Errorf("leveled logger with no stderr override expects os.Stderr")
}
if log.stdout() != os.Stdout {
t.Errorf("leveled logger with no stdout override expects os.Stdout")
}
}
func TestDoGetHeadersDebug(t *testing.T) {
err := &bytes.Buffer{}
out := &bytes.Buffer{}
logger := &LeveledLogger{Level: LevelDebug, stderrOverride: err, stdoutOverride: out}
reqExpected := "[DEBUG] GET: //http:%2F%2Ftest.com/foo/1\n[DEBUG] SENT: request body\n"
resExpected := "[DEBUG] RECV 200: OK\n[DEBUG] RESP: response body\n"
client := NewClient(app, "fooshop", "abcd", WithLogger(logger))
client.logBody(nil, "")
if out.String() != "" {
t.Errorf("logBody expected empty log output but received \"%s\"", out.String())
}
client.logRequest(nil)
if out.String() != "" {
t.Errorf("logRequest expected empty log output received \"%s\"", out.String())
}
client.logRequest(&http.Request{
Method: "GET",
URL: &url.URL{Host: "http://test.com", Path: "/foo/1"},
Body: ioutil.NopCloser(strings.NewReader("request body")),
})
if out.String() != reqExpected {
t.Errorf("doGetHeadersDebug expected stdout \"%s\" received \"%s\"", reqExpected, out)
}
err.Reset()
out.Reset()
client.logResponse(nil)
if out.String() != "" {
t.Errorf("logResponse expected empty log output received \"%s\"", out.String())
}
client.logResponse(&http.Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader("response body")),
})
if out.String() != resExpected {
t.Errorf("doGetHeadersDebug expected stdout \"%s\" received \"%s\"", resExpected, out.String())
}
}