-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdebug.go
45 lines (38 loc) · 977 Bytes
/
debug.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
package grafton
import (
"log"
"net/http"
"net/http/httputil"
"os"
)
// debugRoundTripper implements http.RoundTripper, dumping both request and
// response to stdout
type debugRoundTripper struct {
rt http.RoundTripper
logger *log.Logger
}
// newDebugRoundTripper returns an http.RoundTripper that will dump requests
// and responses before passing them on to the given RoundTripper.
func newDebugRoundTripper(rt http.RoundTripper) *debugRoundTripper {
return &debugRoundTripper{
rt: rt,
logger: log.New(os.Stdout, "", log.LstdFlags),
}
}
func (rt *debugRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
dreq, err := httputil.DumpRequest(req, true)
if err != nil {
return nil, err
}
rt.logger.Println(string(dreq))
res, err := rt.rt.RoundTrip(req)
if err != nil {
return nil, err
}
dres, err := httputil.DumpResponse(res, true)
if err != nil {
return nil, err
}
rt.logger.Println(string(dres))
return res, nil
}