This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrouplogger_test.go
83 lines (68 loc) · 1.6 KB
/
grouplogger_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
package grouplogger
import (
"net/http"
"testing"
"time"
"cloud.google.com/go/logging"
)
const fake_uuid = "fake_uuid"
func mockUUIDFunc() string {
return fake_uuid
}
func TestGetGroupIDWithRequestWithHeader(t *testing.T) {
r, _ := http.NewRequest("GET", "http://www.vimeo.com", nil)
r.Header.Set("X-Cloud-Trace-Context", "123")
id := getGroupID(r, mockUUIDFunc)
if id != "123" {
t.Fatal(id)
}
}
func TestGetGroupIDWithRequestWithoutHeader(t *testing.T) {
id := getGroupID(&http.Request{}, mockUUIDFunc)
if id != fake_uuid {
t.Fatal(id)
}
}
func TestGetGroupIDWithoutRequest(t *testing.T) {
id := getGroupID(nil, mockUUIDFunc)
if id != fake_uuid {
t.Fatal(id)
}
}
func TestCloseWith(t *testing.T) {
var outerEntry logging.Entry
r, _ := http.NewRequest("GET", "https://www.vimeo.com", nil)
gl := GroupLogger{
Req: r,
GroupID: "fake_GroupID",
OuterLogger: &mockLogger{
LogFunc: func(e logging.Entry) {
outerEntry = e
},
},
InnerEntries: []logging.Entry{
logging.Entry{
Severity: logging.ParseSeverity("Info"),
},
logging.Entry{
Severity: logging.ParseSeverity("Alert"),
},
logging.Entry{
Severity: logging.ParseSeverity("Error"),
},
},
}
stats := logging.HTTPRequest{
Latency: 1 * time.Second,
}
gl.CloseWith(&stats)
if outerEntry.Severity.String() != "Alert" {
t.Fatal(outerEntry.Severity.String())
}
if outerEntry.HTTPRequest.Latency != 1*time.Second {
t.Fatal(outerEntry.HTTPRequest.Latency)
}
if outerEntry.HTTPRequest.Request.URL.String() != "https://www.vimeo.com" {
t.Fatal(outerEntry.HTTPRequest.Request.URL.String())
}
}