-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect.go
94 lines (72 loc) · 1.7 KB
/
expect.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
package main
import (
"fmt"
"net/http"
"regexp"
"github.com/sirupsen/logrus"
)
type Expected struct {
Name string
StatusCode string `yaml:"status_code_re"`
Headers map[string]string `yaml:"headers_re"`
Body string `yaml:"body_re"`
}
func (e *Expected) Evaluate(name string, r *http.Response, b string) error {
e.Name = name
e.EvaluateStatusCode(r.StatusCode)
e.EvaluateHeaders(&r.Header)
e.EvaluateBody(b)
return nil
}
func (e *Expected) EvaluateStatusCode(s int) error {
counter := ExpectedResponseCounter.WithLabelValues(e.Name, "status_code")
if e.StatusCode == "" {
return nil
}
if match(e.StatusCode, fmt.Sprintf("%d", s)) {
counter.Inc()
return nil
}
return fmt.Errorf("Status code %d, did not match %s", s, e.StatusCode)
}
func (e *Expected) EvaluateHeaders(h *http.Header) error {
counter := ExpectedResponseCounter.WithLabelValues(e.Name, "headers")
errors := 0
if len(e.Headers) == 0 {
return nil
}
for k, v := range e.Headers {
if match(v, h.Get(k)) {
counter.Inc()
} else {
errors++
}
}
if errors > 0 {
return fmt.Errorf("Headers did not match")
}
return nil
}
func (e *Expected) EvaluateBody(b string) error {
counter := ExpectedResponseCounter.WithLabelValues(e.Name, "body")
if e.Body == "" {
return nil
}
if match(e.Body, b) {
counter.Inc()
return nil
}
return fmt.Errorf("Body did not match %s", e.Body)
}
func match(exp, target string) bool {
re, err := regexp.Compile(exp)
if err != nil {
ExpectReCompileError.Inc()
logrus.
WithError(err).
WithField("regexp", exp).
Error("Could not compile regular expression for expected evaluation")
return false
}
return re.MatchString(target)
}