-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttping_test.go
149 lines (133 loc) · 3.37 KB
/
httping_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
package httping_test
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/rnemeth90/httping"
)
func TestParseURL(t *testing.T) {
testCases := []struct {
name string
url string
insecure bool
expect string
}{
{name: "insecure", url: "http://www.google.com", insecure: true, expect: "http://www.google.com"},
{name: "secure", url: "https://www.google.com", insecure: false, expect: "https://www.google.com"},
{name: "no-protocol-insecure", url: "www.google.com", insecure: true, expect: "http://www.google.com"},
{name: "no-protocol-secure", url: "www.google.com", insecure: false, expect: "https://www.google.com"},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
got := httping.ParseURL(test.url, test.insecure)
if got != test.expect {
t.Errorf("expected %s\n, got %s\n", test.expect, got)
}
})
}
}
func TestParseHeader(t *testing.T) {
testCases := []struct {
name string
headers map[string]string
expect string
}{
{name: "ManyHeaders",
headers: map[string]string{
"Content-Type": "application/json",
"host": "server01",
},
expect: " {Content-Type:application/json} {host:server01} ",
},
{name: "OneHeader",
headers: map[string]string{
"{host": "server01}",
},
expect: " {host:server01} ",
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
got := httping.ParseHeader(&test.headers)
if test.expect != got {
t.Errorf("expected %s\n, got %s\n", test.expect, got)
}
})
}
}
func TestCalculateStatistics(t *testing.T) {
testCases := []struct {
name string
responses []*httping.HttpResponse
expect *httping.HTTPStatistics
}{
{name: "Test200s",
responses: []*httping.HttpResponse{
{
Status: 200,
Host: "localhost",
ResponseHeaders: nil,
Latency: 123,
},
{
Status: 302,
Host: "localhost",
ResponseHeaders: nil,
Latency: 100,
},
},
expect: &httping.HTTPStatistics{
Count200: 1,
Count204: 0,
Count201: 0,
Count301: 0,
Count302: 1,
Count304: 0,
Count400: 0,
Count401: 0,
Count403: 0,
Count404: 0,
Count500: 0,
Count502: 0,
Count503: 0,
Count504: 0,
Other: 0,
AverageLatency: 111,
MaxLatency: 123,
MinLatency: 100,
}},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
got := httping.CalculateStatistics(test.responses)
if !reflect.DeepEqual(test.expect, got) {
t.Errorf("expected %q\n, got %q\n", test.expect, got)
}
})
}
}
func TestMakeRequest(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("host", "tester")
w.WriteHeader(http.StatusTeapot)
}))
client := http.Client{}
resp, err := client.Get(server.URL)
if err != nil {
t.Fatal(err)
}
expected := httping.HttpResponse{
Status: resp.StatusCode,
Host: "tester",
ResponseHeaders: nil,
Latency: 0,
}
got, err := httping.MakeRequest(false, "", server.URL, "host")
if err != nil {
t.Fatal(err)
}
if reflect.DeepEqual(got, expected) {
t.Errorf("expected response %q\n, but got %q\n", expected.Host, got)
}
}