forked from steinfletcher/apitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
120 lines (100 loc) · 2.2 KB
/
report.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
package apitest
import (
"errors"
"net/http"
"time"
)
type (
ReportFormatter interface {
Format(*Recorder)
}
Event interface {
GetTime() time.Time
}
Recorder struct {
Title string
SubTitle string
Meta map[string]interface{}
Events []Event
}
MessageRequest struct {
Source string
Target string
Header string
Body string
Timestamp time.Time
}
MessageResponse struct {
Source string
Target string
Header string
Body string
Timestamp time.Time
}
HttpRequest struct {
Source string
Target string
Value *http.Request
Timestamp time.Time
}
HttpResponse struct {
Source string
Target string
Value *http.Response
Timestamp time.Time
}
)
func (r HttpRequest) GetTime() time.Time { return r.Timestamp }
func (r HttpResponse) GetTime() time.Time { return r.Timestamp }
func (r MessageRequest) GetTime() time.Time { return r.Timestamp }
func (r MessageResponse) GetTime() time.Time { return r.Timestamp }
func NewTestRecorder() *Recorder {
return &Recorder{}
}
func (r *Recorder) AddHttpRequest(req HttpRequest) *Recorder {
r.Events = append(r.Events, req)
return r
}
func (r *Recorder) AddHttpResponse(req HttpResponse) *Recorder {
r.Events = append(r.Events, req)
return r
}
func (r *Recorder) AddMessageRequest(m MessageRequest) *Recorder {
r.Events = append(r.Events, m)
return r
}
func (r *Recorder) AddMessageResponse(m MessageResponse) *Recorder {
r.Events = append(r.Events, m)
return r
}
func (r *Recorder) AddTitle(title string) *Recorder {
r.Title = title
return r
}
func (r *Recorder) AddSubTitle(subTitle string) *Recorder {
r.SubTitle = subTitle
return r
}
func (r *Recorder) AddMeta(meta map[string]interface{}) *Recorder {
r.Meta = meta
return r
}
func (r *Recorder) ResponseStatus() (int, error) {
if len(r.Events) == 0 {
return -1, errors.New("no events are defined")
}
switch v := r.Events[len(r.Events)-1].(type) {
case HttpResponse:
return v.Value.StatusCode, nil
case MessageResponse:
return -1, nil
default:
return -1, errors.New("final event should be a response type")
}
}
func (r *Recorder) Reset() {
r.Title = ""
r.SubTitle = ""
r.Events = nil
r.Meta = nil
}