-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
121 lines (117 loc) · 3.1 KB
/
types_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
package traefik_responsebodyrewrite
import (
"reflect"
"testing"
)
func TestHTTPCodeRanges_Contains(t *testing.T) {
tests := []struct {
desc string
ranges HTTPCodeRanges
statusCode int
expectedRes bool
}{
{
desc: "should return true if status code is within the range",
ranges: HTTPCodeRanges{{200, 299}, {400, 499}},
statusCode: 200,
expectedRes: true,
},
{
desc: "should return true if status code is at the lower bound of the range",
ranges: HTTPCodeRanges{{200, 299}, {400, 499}},
statusCode: 400,
expectedRes: true,
},
{
desc: "should return true if status code is at the upper bound of the range",
ranges: HTTPCodeRanges{{200, 299}, {400, 499}},
statusCode: 499,
expectedRes: true,
},
{
desc: "should return false if status code is outside the range",
ranges: HTTPCodeRanges{{200, 299}, {400, 499}},
statusCode: 300,
expectedRes: false,
},
{
desc: "should return false if status code is below all ranges",
ranges: HTTPCodeRanges{{200, 299}, {400, 499}},
statusCode: 100,
expectedRes: false,
},
{
desc: "should return false if status code is above all ranges",
ranges: HTTPCodeRanges{{200, 299}, {400, 499}},
statusCode: 500,
expectedRes: false,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
res := test.ranges.Contains(test.statusCode)
if res != test.expectedRes {
t.Errorf("got %v, want %v", res, test.expectedRes)
}
})
}
}
func TestNewHTTPCodeRanges(t *testing.T) {
tests := []struct {
desc string
strBlocks []string
expected HTTPCodeRanges
expectErr bool
}{
{
desc: "should create HTTPCodeRanges with single code",
strBlocks: []string{"200"},
expected: HTTPCodeRanges{{200, 200}},
expectErr: false,
},
{
desc: "should create HTTPCodeRanges with code range",
strBlocks: []string{"200-299"},
expected: HTTPCodeRanges{{200, 299}},
expectErr: false,
},
{
desc: "should create HTTPCodeRanges with multiple code ranges",
strBlocks: []string{"200-299", "400-499"},
expected: HTTPCodeRanges{{200, 299}, {400, 499}},
expectErr: false,
},
{
desc: "should create HTTPCodeRanges with multiple code ranges",
strBlocks: []string{"200-299", "400", "450-499"},
expected: HTTPCodeRanges{{200, 299}, {400, 400}, {450, 499}},
expectErr: false,
},
{
desc: "should return error for invalid code",
strBlocks: []string{"200-abc"},
expected: nil,
expectErr: true,
},
{
desc: "should return error for invalid code",
strBlocks: []string{"abc-200"},
expected: nil,
expectErr: true,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
res, err := NewHTTPCodeRanges(test.strBlocks)
if test.expectErr && err == nil {
t.Errorf("expected error, but got nil")
}
if !test.expectErr && err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(res, test.expected) {
t.Errorf("got %v, want %v", res, test.expected)
}
})
}
}