-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
63 lines (54 loc) · 1.15 KB
/
parse.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
package config
import (
"strings"
"gopkg.in/yaml.v2"
)
// Parse []byte config to TestSpec.
func Parse(b []byte) (*TestSpec, error) {
var t TestSpec
err := yaml.Unmarshal(b, &t)
if err != nil {
return nil, err
}
// Default to 3 seconds timeout.
if t.TimeoutSeconds == 0 {
t.TimeoutSeconds = 3
}
return &t, nil
}
// Update returns the generated config from the TestSpec.
func (t *TestSpec) Update() ([]byte, error) {
// Remove headers that changes between requests.
for k := range t.Response.Headers {
switch strings.ToLower(k) {
case "date":
delete(t.Response.Headers, k)
case "last-modified":
delete(t.Response.Headers, k)
case "expires":
delete(t.Response.Headers, k)
}
}
return yaml.Marshal(t)
}
// GenerateExample generates example config YAML output.
func GenerateExample() ([]byte, error) {
test := &TestSpec{
TimeoutSeconds: 3,
Request: Request{
Method: "GET",
Path: "/",
Headers: map[string]string{
"Host": "example.com",
},
},
Response: Response{
Headers: map[string]string{
"Content-Type": "encoding/json",
},
Body: "Example body",
Code: 200,
},
}
return yaml.Marshal(test)
}