-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
180 lines (155 loc) · 4.71 KB
/
ui.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package apidiff
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strconv"
"github.com/dnaeon/go-vcr/cassette"
"github.com/olekukonko/tablewriter"
)
// UI contains helpers for drawing CLI interface
type UI struct {
out io.Writer
}
// NewUI returns instance
func NewUI(w io.Writer) *UI {
return &UI{
out: w,
}
}
// ListSessions draws table of existing recorded sessions
func (ui *UI) ListSessions(sessions []RecordedSession, showCaption bool) {
rows := [][]string{}
for _, session := range sessions {
rows = append(rows, []string{
session.Name,
session.Path,
strconv.Itoa(len(session.Interactions)),
session.Created.Format("2006-01-02 15:04:05"),
})
}
fmt.Fprintln(ui.out)
table := tablewriter.NewWriter(ui.out)
table.SetCenterSeparator("|")
table.SetHeader([]string{"Name", "Path", "# Interactions", "Created"})
table.AppendBulk(rows)
table.SetCaption(showCaption, fmt.Sprintf(" Total %d session(s)", len(rows)))
table.Render()
fmt.Fprintln(ui.out)
}
// ShowSession displays detail of selected session
func (ui *UI) ShowSession(session RecordedSession) {
if len(session.Interactions) == 0 {
fmt.Fprintf(ui.out, "No recorded session interactions found")
} else {
rows := [][]string{}
for i, interaction := range session.Interactions {
rows = append(rows, []string{
strconv.Itoa(i + 1),
interaction.Method,
interaction.URL,
strconv.Itoa(interaction.StatusCode),
ui.formatMS(interaction.Stats.ServerProcessing),
ui.formatMS(interaction.Stats.Duration()),
})
}
fmt.Fprintln(ui.out)
table := tablewriter.NewWriter(ui.out)
table.SetCenterSeparator("|")
table.SetHeader([]string{
"#",
"Method",
"URI",
"Status",
"Duration",
"Processing",
})
table.SetCaption(true, fmt.Sprintf(" Total %d interaction(s)", len(session.Interactions)))
table.AppendBulk(rows)
table.Render()
fmt.Fprintln(ui.out)
}
}
// ShowInteractionDetail displays recorded session interaction by given index
func (ui *UI) ShowInteractionDetail(interaction *cassette.Interaction, stats *RequestStats) {
fmt.Fprintln(ui.out)
req := interaction.Request
resp := interaction.Response
table := tablewriter.NewWriter(ui.out)
table.SetCenterSeparator("|")
table.SetAutoMergeCells(true)
table.SetRowLine(true)
table.SetHeader([]string{"Type", "Field", "Value"})
table.AppendBulk([][]string{
// request
[]string{"Request", "URL", req.URL},
[]string{"Request", "Method", req.Method},
[]string{"Request", "Headers", fmt.Sprintf("%+v", req.Headers)},
[]string{"Request", "Params", fmt.Sprintf("%+v", req.Form)},
[]string{"Request", "Payload", ui.formatJSON(req.Body)},
// response
[]string{"Response", "Headers", fmt.Sprintf("%+v", resp.Headers)},
[]string{"Response", "Status", resp.Status},
[]string{"Response", "Body", ui.formatJSON(resp.Body)},
// metrics
[]string{"Metrics", "DNS Lookup", ui.formatMS(stats.DNSLookup)},
[]string{"Metrics", "TCP Connection", ui.formatMS(stats.TCPConnection)},
[]string{"Metrics", "TLS Handshake", ui.formatMS(stats.TLSHandshake)},
[]string{"Metrics", "Server Processing", ui.formatMS(stats.ServerProcessing)},
[]string{"Metrics", "Content Transfer", ui.formatMS(stats.ContentTransfer)},
[]string{"Metrics", "Total duration", ui.formatMS(stats.Duration())},
})
table.Render()
fmt.Fprintln(ui.out)
}
// ShowComparisonResults displays result of comparing source and target
// sessions
func (ui *UI) ShowComparisonResults(source RecordedSession, errors map[int]Differences) {
if len(source.Interactions) == 0 {
fmt.Fprintf(ui.out, "No recorded session interactions found")
} else {
total := 0
rows := [][]string{}
for i := range source.Interactions {
err := errors[i]
for headerKey, headerValue := range err.Headers {
rows = append(rows, []string{
source.Name,
strconv.Itoa(i),
fmt.Sprintf("Header %s", headerKey),
headerValue.Error(),
})
total++
}
for _, bodyValue := range err.Body {
rows = append(rows, []string{
source.Name,
strconv.Itoa(i),
"Body",
bodyValue.Error(),
})
total++
}
}
fmt.Fprintln(ui.out)
table := tablewriter.NewWriter(ui.out)
table.SetRowLine(true)
table.SetAutoWrapText(false)
table.SetCenterSeparator("|")
table.SetHeader([]string{"Session", "# Interaction", "Type", "Difference"})
table.SetCaption(true, fmt.Sprintf(" Total %d errors(s)", total))
table.AppendBulk(rows)
table.Render()
fmt.Fprintln(ui.out)
}
}
func (ui *UI) formatMS(duration int) string {
return fmt.Sprintf("%d ms", duration)
}
//TODO: pretty printing does not seem to be working correctly
func (ui *UI) formatJSON(body string) string {
var prettyJSON bytes.Buffer
json.Indent(&prettyJSON, []byte(body), "", " ")
return string(prettyJSON.Bytes())
}