forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_dumper.go
49 lines (41 loc) · 1.45 KB
/
request_dumper.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
package net
import (
"net/http"
"net/http/httputil"
"strings"
"time"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/terminal"
"code.cloudfoundry.org/cli/cf/trace"
)
//go:generate counterfeiter . RequestDumperInterface
type RequestDumperInterface interface {
DumpRequest(*http.Request)
DumpResponse(*http.Response)
}
type RequestDumper struct {
printer trace.Printer
}
func NewRequestDumper(printer trace.Printer) RequestDumper {
return RequestDumper{printer: printer}
}
func (p RequestDumper) DumpRequest(req *http.Request) {
shouldDisplayBody := !strings.Contains(req.Header.Get("Content-Type"), "multipart/form-data")
dumpedRequest, err := httputil.DumpRequest(req, shouldDisplayBody)
if err != nil {
p.printer.Printf(T("Error dumping request\n{{.Err}}\n", map[string]interface{}{"Err": err}))
} else {
p.printer.Printf("\n%s [%s]\n%s\n", terminal.HeaderColor(T("REQUEST:")), time.Now().Format(time.RFC3339), trace.Sanitize(string(dumpedRequest)))
if !shouldDisplayBody {
p.printer.Println(T("[MULTIPART/FORM-DATA CONTENT HIDDEN]"))
}
}
}
func (p RequestDumper) DumpResponse(res *http.Response) {
dumpedResponse, err := httputil.DumpResponse(res, true)
if err != nil {
p.printer.Printf(T("Error dumping response\n{{.Err}}\n", map[string]interface{}{"Err": err}))
} else {
p.printer.Printf("\n%s [%s]\n%s\n", terminal.HeaderColor(T("RESPONSE:")), time.Now().Format(time.RFC3339), trace.Sanitize(string(dumpedResponse)))
}
}