-
Notifications
You must be signed in to change notification settings - Fork 56
/
report.go
145 lines (122 loc) · 3.34 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package apitest
import (
"errors"
"net/http"
"time"
)
type (
// ReportFormatter represents the report formatter
ReportFormatter interface {
// Format formats the events received from the recorder
Format(*Recorder)
}
// Event represents a reporting event
Event interface {
GetTime() time.Time
}
// Recorder represents all of the report data
Recorder struct {
Title string
SubTitle string
Meta map[string]interface{}
Events []Event
}
// MessageRequest represents a request interaction
MessageRequest struct {
Source string
Target string
Header string
Body string
Timestamp time.Time
}
// MessageResponse represents a response interaction
MessageResponse struct {
Source string
Target string
Header string
Body string
Timestamp time.Time
}
// HttpRequest represents an http request
HttpRequest struct {
Source string
Target string
Value *http.Request
Timestamp time.Time
}
// HttpResponse represents an http response
HttpResponse struct {
Source string
Target string
Value *http.Response
Timestamp time.Time
}
)
// GetTime gets the time of the HttpRequest interaction
func (r HttpRequest) GetTime() time.Time { return r.Timestamp }
// GetTime gets the time of the HttpResponse interaction
func (r HttpResponse) GetTime() time.Time { return r.Timestamp }
// GetTime gets the time of the MessageRequest interaction
func (r MessageRequest) GetTime() time.Time { return r.Timestamp }
// GetTime gets the time of the MessageResponse interaction
func (r MessageResponse) GetTime() time.Time { return r.Timestamp }
// NewTestRecorder creates a new TestRecorder
func NewTestRecorder() *Recorder {
return &Recorder{}
}
// AddHttpRequest add an http request to recorder
func (r *Recorder) AddHttpRequest(req HttpRequest) *Recorder {
r.Events = append(r.Events, req)
return r
}
// AddHttpResponse add an HttpResponse to the recorder
func (r *Recorder) AddHttpResponse(req HttpResponse) *Recorder {
r.Events = append(r.Events, req)
return r
}
// AddMessageRequest add a MessageRequest to the recorder
func (r *Recorder) AddMessageRequest(m MessageRequest) *Recorder {
r.Events = append(r.Events, m)
return r
}
// AddMessageResponse add a MessageResponse to the recorder
func (r *Recorder) AddMessageResponse(m MessageResponse) *Recorder {
r.Events = append(r.Events, m)
return r
}
// AddTitle add a Title to the recorder
func (r *Recorder) AddTitle(title string) *Recorder {
r.Title = title
return r
}
// AddSubTitle add a SubTitle to the recorder
func (r *Recorder) AddSubTitle(subTitle string) *Recorder {
r.SubTitle = subTitle
return r
}
// AddMeta add Meta to the recorder
func (r *Recorder) AddMeta(meta map[string]interface{}) *Recorder {
r.Meta = meta
return r
}
// ResponseStatus get response status of the recorder, returning an error when this wasn't possible
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")
}
}
// Reset resets the recorder to default starting state
func (r *Recorder) Reset() {
r.Title = ""
r.SubTitle = ""
r.Events = nil
r.Meta = nil
}