forked from firizki/pimock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.go
124 lines (107 loc) · 3.64 KB
/
response_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
package main
import (
"testing"
)
func TestNewResponses(t *testing.T) {
type unit struct {
mapdcvr map[string][]string
method string
path string
header map[string][]string
urlq map[string][]string
result int
}
cases := []unit{
unit{map[string][]string{"healthz": []string{}},"GET", "healthz", map[string][]string{}, map[string][]string{"key": []string{"value"}}, 0},
unit{map[string][]string{"healthz": []string{}},"GET", "healthz", map[string][]string{"Pimock-Sleep": []string{"1"}}, map[string][]string{"key": []string{"value"}}, 0},
unit{map[string][]string{"healthz": []string{}},"GET", "healthz/", map[string][]string{}, map[string][]string{"key": []string{"value"}}, 0},
unit{map[string][]string{"hallo": []string{}},"GET", "healthz", map[string][]string{}, map[string][]string{"key": []string{"value"}}, 1},
unit{map[string][]string{"healthz/(?!1)": []string{}},"GET", "healthz", map[string][]string{}, map[string][]string{"key": []string{"value"}}, 2},
unit{map[string][]string{"healthz": []string{}},"GET", "healthz", map[string][]string{"Pimock-Sleep": []string{"Value","Key"}}, map[string][]string{"key": []string{"value"}}, 2},
}
for _, c := range cases {
switch c.result {
case 0:
res := NewResponse(c.method, c.path, c.header, c.urlq, c.mapdcvr)
if res == nil {
t.Errorf("The code did not give correct result")
}
case 1:
res := NewResponse(c.method, c.path, c.header, c.urlq, c.mapdcvr)
if res != nil {
t.Errorf("The code did not give correct result")
}
case 2:
t.Run("Panic Test", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
NewResponse(c.method, c.path, c.header, c.urlq, c.mapdcvr)
})
}
}
}
func TestHeaderStatus(t *testing.T) {
type unit struct {
test []string
result int
}
cases := []unit{
unit{[]string{"HTTP/1.1 200 OK"}, 200},
unit{[]string{"HTTP/1.1 OK"}, 0},
}
for _, c := range cases {
res := Response{resp: c.test}
if c.result != 0 {
if res.getHeaderStatus() != c.result {
t.Errorf("The code did not give correct result")
}
} else {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
res.getHeaderStatus()
}
}
}
func TestHeaderData(t *testing.T) {
type unit struct {
test []string
result map[string]string
}
cases := []unit{
unit{[]string{"HTTP/1.1 200 OK", "Content-Type: text/plain; charset=utf-8", ""}, map[string]string{"Content-Type": "text/plain; charset=utf-8"}},
unit{[]string{"HTTP/1.1 200 OK", "Content-Type: application/json", ""}, map[string]string{"Content-Type": "application/json"}},
}
for _, c := range cases {
res := Response{resp: c.test}
headerData := res.getHeaderData()
for i, v := range headerData {
if c.result[i] != v {
t.Errorf("The code did not give correct result")
}
}
}
}
func TestBody(t *testing.T) {
type unit struct {
test []string
vars map[string]string
result string
}
cases := []unit{
unit{[]string{"", "{{request.path.[0]}}"}, map[string]string{"{{request.path.[0]}}": "healthz"}, "healthz"},
unit{[]string{"", "OK"}, map[string]string{"{{request.path.[0]}}": "healthz"}, "OK"},
unit{[]string{"OK"}, map[string]string{}, ""},
}
for _, c := range cases {
res := Response{resp: c.test, variables: c.vars}
if res.getBody() != c.result {
t.Errorf("The code did not give correct result")
}
}
}