-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.go
197 lines (173 loc) · 5.29 KB
/
http.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package resource
import (
"crypto/tls"
"github.com/SimonBaeumer/goss/system"
"github.com/SimonBaeumer/goss/util"
"log"
"reflect"
"strings"
"time"
)
const TimeoutMS = 5000
type HTTP struct {
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
HTTP string `json:"-" yaml:"-"`
Status matcher `json:"status" yaml:"status"`
AllowInsecure bool `json:"allow-insecure" yaml:"allow-insecure"`
NoFollowRedirects bool `json:"no-follow-redirects" yaml:"no-follow-redirects"`
Timeout int `json:"timeout" yaml:"timeout"`
Body []string `json:"body" yaml:"body"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
Headers map[string][]string `json:"headers,omitempty" yaml:"headers,omitempty"`
RequestHeaders map[string][]string `json:"request-headers,omitempty" yaml:"request-headers,omitempty"`
Cert string `json:"cert,omitempty" yaml:"cert,omitempty"`
Key string `json:"key,omitempty" yaml:"key,omitempty"`
}
func (u *HTTP) ID() string { return u.HTTP }
func (u *HTTP) SetID(id string) { u.HTTP = id }
func (r *HTTP) GetTitle() string { return r.Title }
func (r *HTTP) GetMeta() meta { return r.Meta }
func (u *HTTP) Validate(sys *system.System) []TestResult {
skip := false
conf := util.Config{
AllowInsecure: u.AllowInsecure,
NoFollowRedirects: u.NoFollowRedirects,
Timeout: u.Timeout,
Username: u.Username,
Password: u.Password,
RequestHeaders: u.RequestHeaders,
Certificate: u.loadClientCertificate(),
}
sysHTTP := sys.NewHTTP(
u.HTTP,
sys,
conf,
)
sysHTTP.SetAllowInsecure(u.AllowInsecure)
sysHTTP.SetNoFollowRedirects(u.NoFollowRedirects)
var results []TestResult
results = append(results, ValidateValue(u, "status", u.Status, sysHTTP.Status, skip))
if shouldSkip(results) {
skip = true
}
if len(u.Body) > 0 {
results = append(results, ValidateContains(u, "Body", u.Body, sysHTTP.Body, skip))
}
if len(u.Headers) > 0 {
headers, _ := sysHTTP.Headers()
results = append(results, validateHeader(u, "Headers", u.Headers, headers, skip))
}
return results
}
func (u *HTTP) loadClientCertificate() tls.Certificate {
if u.Cert == "" || u.Key == "" {
return tls.Certificate{}
}
cert, err := tls.LoadX509KeyPair(u.Cert, u.Key)
if err != nil {
log.Fatal(err)
}
return cert
}
func NewHTTP(sysHTTP system.HTTP, config util.Config) (*HTTP, error) {
if config.Timeout == 0 {
config.Timeout = TimeoutMS
}
http := sysHTTP.HTTP()
status, err := sysHTTP.Status()
headers, _ := sysHTTP.Headers()
u := &HTTP{
HTTP: http,
Status: status,
Body: []string{},
AllowInsecure: config.AllowInsecure,
NoFollowRedirects: config.NoFollowRedirects,
Timeout: config.Timeout,
Username: config.Username,
Password: config.Password,
Headers: headers,
}
return u, err
}
func validateHeader(res ResourceRead, property string, expectedHeaders map[string][]string, actualHeaders system.Header, skip bool) TestResult {
id := res.ID()
title := res.GetTitle()
meta := res.GetMeta()
typ := reflect.TypeOf(res)
typeS := strings.Split(typ.String(), ".")[1]
startTime := time.Now()
if skip {
return skipResult(
typeS,
Values,
id,
title,
meta,
property,
startTime,
)
}
actualString := convertHeaderMapToString(actualHeaders)
for expectedKey, expectedValues := range expectedHeaders {
if _, ok := actualHeaders[expectedKey]; !ok {
return TestResult{
Successful: false,
Result: FAIL,
Title: title,
ResourceType: typeS,
ResourceId: id,
TestType: Header,
Property: property,
Err: nil,
Human: "Did not find header " + expectedKey + " got \n" + actualString,
Expected: []string{expectedKey},
Found: []string{actualString},
}
}
actualValues := actualHeaders[expectedKey]
for _, expectedValue := range expectedValues {
if !isInStringSlice(actualValues, expectedValue) {
return TestResult{
Successful: false,
Result: FAIL,
ResourceType: typeS,
ResourceId: id,
TestType: Header,
Title: title,
Property: property,
Err: nil,
Human: "Did not find header " + expectedKey + ": " + expectedValue,
Expected: []string{expectedValue},
Found: []string{actualString},
}
}
}
}
return TestResult{
Successful: true,
Title: title,
ResourceId: id,
Result: SUCCESS,
ResourceType: typeS,
TestType: Header,
}
}
func convertHeaderMapToString(actualHeaders system.Header) string {
var actualString string
for k, values := range actualHeaders {
for _, v := range values {
actualString += k + ": " + v + "\n"
}
}
return actualString
}
func isInStringSlice(haystack []string, needle string) bool {
for _, value := range haystack {
if needle == value {
return true
}
}
return false
}