/
testing.go
89 lines (76 loc) · 1.78 KB
/
testing.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
package testing
import (
"bytes"
"fmt"
"text/template"
"github.com/lamoda/gonkey/models"
)
type TestingOutput struct{}
func NewOutput() *TestingOutput {
return &TestingOutput{}
}
func (o *TestingOutput) Process(t models.TestInterface, result *models.Result) error {
if !result.Passed() {
text, err := renderResult(result)
if err != nil {
return err
}
fmt.Println(text)
}
return nil
}
func renderResult(result *models.Result) (string, error) {
text := `
Name: {{ .Test.GetName }}
File: {{ .Test.GetFileName }}
Request:
Method: {{ .Test.GetMethod }}
Path: {{ .Test.Path }}
Query: {{ .Test.ToQuery }}
{{- if .Test.Headers }}
Headers:
{{- range $key, $value := .Test.Headers }}
{{ $key }}: {{ $value }}
{{- end }}
{{- end }}
{{- if .Test.Cookies }}
Cookies:
{{- range $key, $value := .Test.Cookies }}
{{ $key }}: {{ $value }}
{{- end }}
{{- end }}
Body:
{{ if .RequestBody }}{{ .RequestBody }}{{ else }}{{ "<no body>" }}{{ end }}
Response:
Status: {{ .ResponseStatus }}
Body:
{{ if .ResponseBody }}{{ .ResponseBody }}{{ else }}{{ "<no body>" }}{{ end }}
{{ range $i, $dbr := .DatabaseResult }}
{{ if $dbr.Query }}
Db Request #{{ inc $i }}:
{{ $dbr.Query }}
Db Response #{{ inc $i }}:
{{ range $value := $dbr.Response }}
{{ $value }}{{ end }}
{{ end }}
{{ end }}
{{ if .Errors }}
Result: {{ "ERRORS!" }}
Errors:
{{ range $i, $e := .Errors }}
{{ inc $i }}) {{ $e.Error }}
{{ end }}
{{ else }}
Result: {{ "OK" }}
{{ end }}
`
funcMap := template.FuncMap{
"inc": func(i int) int { return i + 1 },
}
var buffer bytes.Buffer
t := template.Must(template.New("letter").Funcs(funcMap).Parse(text))
if err := t.Execute(&buffer, result); err != nil {
return "", err
}
return buffer.String(), nil
}