-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
printer.go
172 lines (147 loc) · 4.41 KB
/
printer.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
package httpexpect
import (
"bytes"
"fmt"
"net/http"
"net/http/httputil"
"strings"
"time"
"github.com/gorilla/websocket"
"moul.io/http2curl/v2"
)
// Printer is used to print requests and responses.
// CompactPrinter, DebugPrinter, and CurlPrinter implement this interface.
type Printer interface {
// Request is called before request is sent.
// It is allowed to read and close request body, or ignore it.
Request(*http.Request)
// Response is called after response is received.
// It is allowed to read and close response body, or ignore it.
Response(*http.Response, time.Duration)
}
// WebsocketPrinter is used to print writes and reads of WebSocket connection.
//
// If WebSocket connection is used, all Printers that also implement WebsocketPrinter
// are invoked on every WebSocket message read or written.
//
// DebugPrinter implements this interface.
type WebsocketPrinter interface {
Printer
// WebsocketWrite is called before writes to WebSocket connection.
WebsocketWrite(typ int, content []byte, closeCode int)
// WebsocketRead is called after reads from WebSocket connection.
WebsocketRead(typ int, content []byte, closeCode int)
}
// CompactPrinter implements Printer.
// Prints requests in compact form. Does not print responses.
type CompactPrinter struct {
logger Logger
}
// NewCompactPrinter returns a new CompactPrinter given a logger.
func NewCompactPrinter(logger Logger) CompactPrinter {
return CompactPrinter{logger}
}
// Request implements Printer.Request.
func (p CompactPrinter) Request(req *http.Request) {
if req != nil {
p.logger.Logf("%s %s", req.Method, req.URL)
}
}
// Response implements Printer.Response.
func (CompactPrinter) Response(*http.Response, time.Duration) {
}
// CurlPrinter implements Printer.
// Uses http2curl to dump requests as curl commands that can be inserted
// into terminal.
type CurlPrinter struct {
logger Logger
}
// NewCurlPrinter returns a new CurlPrinter given a logger.
func NewCurlPrinter(logger Logger) CurlPrinter {
return CurlPrinter{logger}
}
// Request implements Printer.Request.
func (p CurlPrinter) Request(req *http.Request) {
if req != nil {
cmd, err := http2curl.GetCurlCommand(req)
if err != nil {
panic(err)
}
p.logger.Logf("%s", cmd.String())
}
}
// Response implements Printer.Response.
func (CurlPrinter) Response(*http.Response, time.Duration) {
}
// DebugPrinter implements Printer and WebsocketPrinter.
// Uses net/http/httputil to dump both requests and responses.
// Also prints all websocket messages.
type DebugPrinter struct {
logger Logger
body bool
}
// NewDebugPrinter returns a new DebugPrinter given a logger and body
// flag. If body is true, request and response body is also printed.
func NewDebugPrinter(logger Logger, body bool) DebugPrinter {
return DebugPrinter{logger, body}
}
// Request implements Printer.Request.
func (p DebugPrinter) Request(req *http.Request) {
if req == nil {
return
}
dump, err := httputil.DumpRequest(req, p.body)
if err != nil {
panic(err)
}
p.logger.Logf("%s", dump)
}
// Response implements Printer.Response.
func (p DebugPrinter) Response(resp *http.Response, duration time.Duration) {
if resp == nil {
return
}
dump, err := httputil.DumpResponse(resp, p.body)
if err != nil {
panic(err)
}
text := strings.Replace(string(dump), "\r\n", "\n", -1)
lines := strings.SplitN(text, "\n", 2)
p.logger.Logf("%s %s\n%s", lines[0], duration, lines[1])
}
// WebsocketWrite implements WebsocketPrinter.WebsocketWrite.
func (p DebugPrinter) WebsocketWrite(typ int, content []byte, closeCode int) {
b := &bytes.Buffer{}
fmt.Fprintf(b, "-> Sent: %s", wsMessageType(typ))
if typ == websocket.CloseMessage {
fmt.Fprintf(b, " %s", wsCloseCode(closeCode))
}
fmt.Fprint(b, "\n")
if len(content) > 0 {
if typ == websocket.BinaryMessage {
fmt.Fprintf(b, "%v\n", content)
} else {
fmt.Fprintf(b, "%s\n", content)
}
}
fmt.Fprintf(b, "\n")
p.logger.Logf(b.String())
}
// WebsocketRead implements WebsocketPrinter.WebsocketRead.
func (p DebugPrinter) WebsocketRead(typ int, content []byte, closeCode int) {
b := &bytes.Buffer{}
fmt.Fprintf(b, "<- Received: %s", wsMessageType(typ))
if typ == websocket.CloseMessage {
fmt.Fprintf(b, " %s", wsCloseCode(closeCode))
}
fmt.Fprint(b, "\n")
if len(content) > 0 {
if typ == websocket.BinaryMessage {
fmt.Fprintf(b, "%v\n", content)
} else {
fmt.Fprintf(b, "%s\n", content)
}
}
fmt.Fprintf(b, "\n")
p.logger.Logf(b.String())
}