/
http.go
65 lines (57 loc) · 2.14 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
package resource
import (
"github.com/aelsabbahy/goss/system"
"github.com/aelsabbahy/goss/util"
)
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"`
}
func (u *HTTP) ID() string { return u.HTTP }
func (u *HTTP) SetID(id string) { u.HTTP = id }
// FIXME: Can this be refactored?
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
if u.Timeout == 0 {
u.Timeout = 5000
}
sysHTTP := sys.NewHTTP(u.HTTP, sys, util.Config{
AllowInsecure: u.AllowInsecure, NoFollowRedirects: u.NoFollowRedirects,
Timeout: u.Timeout, Username: u.Username, Password: u.Password})
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))
}
return results
}
func NewHTTP(sysHTTP system.HTTP, config util.Config) (*HTTP, error) {
http := sysHTTP.HTTP()
status, err := sysHTTP.Status()
u := &HTTP{
HTTP: http,
Status: status,
Body: []string{},
AllowInsecure: config.AllowInsecure,
NoFollowRedirects: config.NoFollowRedirects,
Timeout: config.Timeout,
Username: config.Username,
Password: config.Password,
}
return u, err
}