-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_test.go
146 lines (125 loc) · 3.7 KB
/
logging_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package servex_test
import (
"errors"
"net/http"
"testing"
"time"
"github.com/maxbolgarin/servex"
)
// MockLogger is a mock implementation of the Logger interface.
type MockLogger struct {
Messages []string
Fields [][]any
LastMessage string
}
func (m *MockLogger) Debug(msg string, fields ...any) {
m.Messages = append(m.Messages, msg)
m.Fields = append(m.Fields, fields)
m.LastMessage = msg
}
func (m *MockLogger) Info(msg string, fields ...any) {
m.Messages = append(m.Messages, msg)
m.Fields = append(m.Fields, fields)
m.LastMessage = msg
}
func (m *MockLogger) Error(msg string, fields ...any) {
m.Messages = append(m.Messages, msg)
m.Fields = append(m.Fields, fields)
m.LastMessage = msg
}
func TestLogFields(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
ctx := servex.C(nil, req)
fields := ctx.LogFields(servex.UserAgentLogField, servex.URLLogField, servex.MethodLogField, servex.ProtoLogField)
expectedFields := []any{
"user_agent", "",
"url", "http://example.com",
"method", "GET",
"proto", "HTTP/1.1",
}
// Check if all expected fields are present in the result
for i := 0; i < len(expectedFields); i += 2 {
found := false
for j := 0; j < len(fields); j += 2 {
if expectedFields[i] == fields[j] && expectedFields[i+1] == fields[j+1] {
found = true
break
}
}
if !found {
t.Errorf("Expected field %v with value %v not found", expectedFields[i], expectedFields[i+1])
}
}
fields = ctx.LogFields()
expectedFields = []any{
"request_id", fields[1], // This is rand request id so we cant prepare it
"ip", fields[3], // This is rand ip so we cant prepare it
"user_agent", "",
"url", "http://example.com",
"method", "GET",
"proto", "HTTP/1.1",
}
// Check if all expected fields are present in the result
for i := 0; i < len(expectedFields); i += 2 {
found := false
for j := 0; j < len(fields); j += 2 {
if expectedFields[i] == fields[j] && expectedFields[i+1] == fields[j+1] {
found = true
break
}
}
if !found {
t.Errorf("Expected field %v with value %v not found", expectedFields[i], expectedFields[i+1])
}
}
}
func TestRequestLogger_Log(t *testing.T) {
mockLogger := &MockLogger{}
rLogger := servex.BaseRequestLogger{Logger: mockLogger}
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
bundle := servex.RequestLogBundle{
Request: req,
RequestID: "12345",
Error: errors.New("some error"),
ErrorMessage: "error occurred",
StatusCode: 500,
StartTime: time.Now().Add(-5 * time.Second), // Simulating that request started 5 seconds ago
}
rLogger.Log(bundle)
if len(mockLogger.Messages) != 1 {
t.Fatalf("Expected one log message, got %d", len(mockLogger.Messages))
}
if mockLogger.Messages[0] != "http" {
t.Errorf("Expected log message to be 'http', got '%s'", mockLogger.Messages[0])
}
expectedFields := []any{
"error", bundle.Error,
"error_message", bundle.ErrorMessage,
"request_id", bundle.RequestID,
"status", bundle.StatusCode,
"duration_ms", int64(5000), // Approximate match due to time function
"ip", req.RemoteAddr,
"user_agent", req.UserAgent(),
"url", req.URL.String(),
"method", req.Method,
"proto", req.Proto,
}
for i := 0; i < len(expectedFields); i += 2 {
found := false
for j := 0; j < len(mockLogger.Fields[0]); j += 2 {
if expectedFields[i] == mockLogger.Fields[0][j] && expectedFields[i+1] == mockLogger.Fields[0][j+1] {
found = true
break
}
}
if !found {
t.Errorf("Expected log field %v with value %v not found", expectedFields[i], expectedFields[i+1])
}
}
}