-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpsnusk.go
96 lines (75 loc) · 2.04 KB
/
httpsnusk.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
package httpsnusk
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/http/httputil"
"os"
)
// Out is the writer where requests and responses are written
var Out io.Writer = os.Stdout
// RoundTripper is a http.RoundTripper
type RoundTripper struct {
Transport http.RoundTripper
DumpRequest func(*http.Request)
DumpResponse func(*http.Response)
}
// DefaultRoundTripper is a RoundTripper that prints the request/response to Out
var DefaultRoundTripper = &RoundTripper{
Transport: http.DefaultTransport,
DumpRequest: func(req *http.Request) {
doPrint(httputil.DumpRequestOut(req, true))
},
DumpResponse: func(resp *http.Response) {
doPrint(httputil.DumpResponse(resp, true))
},
}
// RoundTrip implements the http.RoundTripper interface
func (rt *RoundTripper) RoundTrip(req *http.Request) (resp *http.Response, err error) {
rt.DumpRequest(req)
defer func() {
if err == nil {
rt.DumpResponse(resp)
}
}()
return rt.Transport.RoundTrip(req)
}
// Handler func is a http.HandlerFunc
type HandlerFunc struct {
Handler http.HandlerFunc
DumpRequest func(*http.Request)
DumpResponse func(*http.Response)
}
// HandlerFunc404 is a HandlerFunc that that prints the request/respones to Out
var HandlerFunc404 = HandlerFunc{
Handler: http.NotFound,
DumpRequest: func(req *http.Request) {
doPrint(httputil.DumpRequest(req, true))
},
DumpResponse: func(resp *http.Response) {
doPrint(httputil.DumpResponse(resp, true))
},
}
// ServeHTTP implements the HandlerFunc interface
func (hf HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
hf.DumpRequest(r)
recorder := httptest.NewRecorder()
hf.Handler(recorder, r)
hf.DumpResponse(recorder.Result())
copyResponseWriter(w, recorder)
}
func copyResponseWriter(dst http.ResponseWriter, src *httptest.ResponseRecorder) error {
dst.WriteHeader(src.Code)
for k, v := range src.Header() {
dst.Header()[k] = v
}
_, err := io.Copy(dst, src.Body)
return err
}
func doPrint(b []byte, err error) {
if err != nil {
fmt.Fprintln(Out, err)
}
fmt.Fprintln(Out, string(b))
}