-
Notifications
You must be signed in to change notification settings - Fork 13
/
helper_test.go
171 lines (146 loc) · 3.71 KB
/
helper_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package mux
import (
"errors"
"fmt"
"math/rand"
"testing"
)
func createRandomStringsCount() (pairs []string, count int) {
randNumber := rand.Intn(100-10) + 10
for i := 0; i < randNumber; i++ {
pairs = append(pairs, "dummy")
}
count = len(pairs)
return
}
func TestIsEvenPairs(t *testing.T) {
for i := 0; i <= 5; i++ {
pairs, count := createRandomStringsCount()
t.Run(fmt.Sprintf("Check pairs test (Count: %v)", count), func(t *testing.T) {
_, err := isEvenPairs(pairs...)
if err == nil && count%2 != 0 {
t.Error("Unexpected pairs count")
} else if err != nil && count%2 == 0 {
t.Error("Unexpected pairs count")
}
})
}
}
func TestConvertStringsToMapString(t *testing.T) {
isEvenPairs := func(pairs ...string) (int, error) {
return 2, nil
}
pairs := []string{"content-type", "application/json"}
m, _ := convertStringsToMapString(isEvenPairs, pairs...)
if value, ok := m["content-type"]; !ok || !value.compare("application/json") {
t.Errorf("Unexpected pair (%s)", string(value.(stringComparison)))
}
}
func TestConvertStringsToMapRegex(t *testing.T) {
isEvenPairs := func(pairs ...string) (int, error) {
return 2, nil
}
pairs := []string{"content-type", "application/json"}
m, _ := convertStringsToMapRegex(isEvenPairs, pairs...)
if value, ok := m["content-type"]; !ok || !value.compare("application/json") {
t.Errorf("Unexpected pair (%s)", value.(regexComparsion))
}
}
func TestGenericConvertStringsToMapFail(t *testing.T) {
isEvenPairs := func(pairs ...string) (int, error) {
return 2, nil
}
buildComparator := func(pair string) (comparison, error) {
return nil, errors.New("Somthing went wrong")
}
pairs := []string{"content-type", "application/json"}
if _, err := genericConvertStringsToMap(isEvenPairs, buildComparator, pairs...); err == nil {
t.Error("Expected a error")
}
}
func TestConvertStringsToMapError(t *testing.T) {
isEvenPairs := func(pairs ...string) (int, error) {
return 3, errors.New("Something went wrong")
}
pairs := make([]string,0)
if _, err := convertStringsToMapString(isEvenPairs, pairs...); err == nil {
t.Error("Unexpected nil error")
}
}
func TestMatchMap(t *testing.T) {
compare := map[string]comparison{
"content-type": stringComparison("application/json"),
}
toCompare := map[string][]string{
"content-type": {
"application/json",
},
}
ok := matchMap(compare, toCompare, false)
if !ok {
t.Error("Unexpected non match")
}
}
func BenchmarkMatchMap(b *testing.B) {
tests := []struct {
title string
compare map[string]comparison
}{
{
title: "MatchMap (String comparison): 1 Value",
compare: map[string]comparison{
"content-type": stringComparison("application/json"),
},
},
{
title: "MatchMap (String comparison): 2 Values",
compare: map[string]comparison{
"accept-encoding": stringComparison("gzip"),
"content-type": stringComparison("application/json"),
},
},
{
title: "MatchMap (String comparison): 3 Values",
compare: map[string]comparison{
"accept-encoding": stringComparison("gzip"),
"content-type": stringComparison("application/json"),
"accept": stringComparison("text/plain"),
},
},
}
toCompare := map[string][]string{
"content-type": {
"applcation/json",
},
"accept-charset": {
"utf-8",
},
"accept-encoding": {
"gzip",
"deflate",
},
"accept-language": {
"en-US",
},
"cache-control": {
"no-cache",
},
"date": {
"Date: Tue, 15 Nov 1994 08:12:31 GMT",
},
"max-Forwards": {
"10",
},
"accept": {
"text/plain",
"text/html",
},
}
for _, test := range tests {
b.Run(test.title, func(b *testing.B) {
for n := 0; n < b.N; n++ {
matchMap(test.compare, toCompare, false)
}
})
}
}