1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package http
17
18 import "testing"
19
20 func Test_validateStatusCode(t *testing.T) {
21 tcs := []struct {
22 name string
23 criteria string
24 result int
25 expect bool
26 }{
27 {
28 name: "single code, correct result",
29 criteria: "200",
30 result: 200,
31 expect: true,
32 }, {
33 name: "single code, wrong result",
34 criteria: "200",
35 result: 201,
36 expect: false,
37 }, {
38 name: "code range, correct result",
39 criteria: "200-200",
40 result: 200,
41 expect: true,
42 }, {
43 name: "code range, correct result",
44 criteria: "200-400",
45 result: 400,
46 expect: true,
47 }, {
48 name: "code range, wrong result",
49 criteria: "200-400",
50 result: 500,
51 expect: false,
52 }, {
53 name: "illegal criteria",
54 criteria: "200.400",
55 result: 500,
56 expect: false,
57 }, {
58 name: "illegal criteria",
59 criteria: "200-x",
60 result: 500,
61 expect: false,
62 },
63 }
64
65 for _, tc := range tcs {
66 t.Run(tc.name, func(t *testing.T) {
67 ok := validateStatusCode(tc.criteria, response{statusCode: tc.result})
68 if ok != tc.expect {
69 t.Errorf("criteria: %s result: %d expect: %t", tc.criteria, tc.result, tc.expect)
70 }
71 })
72 }
73 }
74