This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathheader_test.go
123 lines (112 loc) · 2.51 KB
/
header_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
package servertiming
import (
"reflect"
"testing"
"time"
)
// headerCases contains test cases for the Server-Timing header. This set
// of test cases is used to test both parsing the header value as well as
// generating the correct header value.
var headerCases = []struct {
Metrics []*Metric
HeaderValue string
}{
{
[]*Metric{
{
Name: "sql-1",
Duration: 100 * time.Millisecond,
Desc: "MySQL lookup Server",
Extra: map[string]string{},
},
},
`sql-1;desc="MySQL lookup Server";dur=100`,
},
// Comma in description
{
[]*Metric{
{
Name: "sql-1",
Duration: 100 * time.Millisecond,
Desc: "MySQL, lookup Server",
Extra: map[string]string{},
},
},
`sql-1;desc="MySQL, lookup Server";dur=100`,
},
// Semicolon in description
{
[]*Metric{
{
Name: "sql-1",
Duration: 100 * time.Millisecond,
Desc: "MySQL; lookup Server",
Extra: map[string]string{},
},
},
`sql-1;desc="MySQL; lookup Server";dur=100`,
},
// Description that contains a number
{
[]*Metric{
{
Name: "sql-1",
Duration: 100 * time.Millisecond,
Desc: "GET 200",
Extra: map[string]string{},
},
},
`sql-1;desc="GET 200";dur=100`,
},
// Number that contains floating point
{
[]*Metric{
{
Name: "sql-1",
Duration: 100100 * time.Microsecond,
Desc: "MySQL; lookup Server",
Extra: map[string]string{},
},
},
`sql-1;desc="MySQL; lookup Server";dur=100.1`,
},
}
func TestParseHeader(t *testing.T) {
for _, tt := range headerCases {
t.Run(tt.HeaderValue, func(t *testing.T) {
h, err := ParseHeader(tt.HeaderValue)
if err != nil {
t.Fatalf("error parsing header: %s", err)
}
if !reflect.DeepEqual(h.Metrics, tt.Metrics) {
t.Fatalf("received, expected:\n\n%#v\n\n%#v", h.Metrics, tt.Metrics)
}
})
}
}
func TestHeaderString(t *testing.T) {
for _, tt := range headerCases {
t.Run(tt.HeaderValue, func(t *testing.T) {
h := &Header{Metrics: tt.Metrics}
actual := h.String()
if actual != tt.HeaderValue {
t.Fatalf("received, expected:\n\n%q\n\n%q", actual, tt.HeaderValue)
}
})
}
}
// Same as TestHeaderString but using the Add method
func TestHeaderAdd(t *testing.T) {
for _, tt := range headerCases {
t.Run(tt.HeaderValue, func(t *testing.T) {
var h Header
for _, m := range tt.Metrics {
h.Add(m)
}
actual := h.String()
if actual != tt.HeaderValue {
t.Fatalf("received, expected:\n\n%q\n\n%q", actual, tt.HeaderValue)
}
})
}
}