Skip to content

Commit 3151355

Browse files
authored
Implement debugging feature (#43)
1 parent 72a8fd2 commit 3151355

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

debug.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package gohttp
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"net/http"
7+
"net/http/httputil"
8+
"regexp"
9+
)
10+
11+
type debugger struct {
12+
rt http.RoundTripper
13+
w io.Writer
14+
reqBody bool
15+
respBody bool
16+
}
17+
18+
func (t *debugger) RoundTrip(req *http.Request) (*http.Response, error) {
19+
if t.rt == nil {
20+
t.rt = http.DefaultTransport
21+
}
22+
reqBody, err := httputil.DumpRequestOut(req, t.reqBody)
23+
if err != nil {
24+
return nil, err
25+
}
26+
res, err := t.rt.RoundTrip(req)
27+
if err != nil {
28+
return nil, err
29+
}
30+
respBody, err := httputil.DumpResponse(res, t.respBody)
31+
if err != nil {
32+
return nil, err
33+
}
34+
t.Write("-> ", reqBody)
35+
t.Write("<- ", respBody)
36+
return res, nil
37+
}
38+
39+
var lineStart = regexp.MustCompile(`(?m)^`)
40+
41+
func (w *debugger) Write(prefix string, buf []byte) {
42+
w.w.Write(append(lineStart.ReplaceAll(bytes.TrimSpace(buf), []byte(prefix)), '\n', '\n'))
43+
}

gohttp.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gohttp
22

33
import (
44
"context"
5+
"io"
56
"net/http"
67
"time"
78
)
@@ -30,6 +31,10 @@ func SetAgent(agent string) {
3031
}
3132
}
3233

34+
func SetDebug(w io.Writer, reqBody, respBody bool) {
35+
defaultSession.SetDebug(w, reqBody, respBody)
36+
}
37+
3338
// SetProxy sets default client transport proxy.
3439
func SetProxy(proxy string) error {
3540
return defaultSession.SetProxy(proxy)

session.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gohttp
22

33
import (
4+
"io"
45
"net/http"
56
"net/http/cookiejar"
67
"net/url"
@@ -28,21 +29,32 @@ func NewSession() *Session {
2829
return newSession(c)
2930
}
3031

32+
func (s *Session) SetDebug(w io.Writer, reqBody, respBody bool) {
33+
if w != nil {
34+
s.client.Transport = &debugger{s.client.Transport, w, reqBody, respBody}
35+
} else if t, ok := s.client.Transport.(*debugger); ok {
36+
s.client.Transport = t.rt
37+
}
38+
}
39+
3140
func (s *Session) setProxy(fn func(*http.Request) (*url.URL, error)) {
32-
var tr *http.Transport
33-
var ok bool
3441
if s.client.Transport == nil {
35-
if tr, ok = http.DefaultTransport.(*http.Transport); ok {
36-
tr.Proxy = fn
42+
if t, ok := http.DefaultTransport.(*http.Transport); ok {
43+
t.Proxy = fn
44+
return
3745
}
3846
} else {
39-
if tr, ok = s.client.Transport.(*http.Transport); ok {
40-
tr.Proxy = fn
47+
if t, ok := s.client.Transport.(*http.Transport); ok {
48+
t.Proxy = fn
49+
return
50+
} else if t, ok := s.client.Transport.(*debugger); ok {
51+
if t, ok := t.rt.(*http.Transport); ok {
52+
t.Proxy = fn
53+
return
54+
}
4155
}
4256
}
43-
if !ok {
44-
panic("Transport is not *http.Transport type")
45-
}
57+
panic("Transport is not *http.Transport type")
4658
}
4759

4860
// SetProxy sets Session client transport proxy.

session_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestSession(t *testing.T) {
2525
tsURL, _ := url.Parse(ts.URL)
2626

2727
s := NewSession()
28+
s.SetDebug(os.Stdout, true, true)
2829
s.Header.Set("hello", "world")
2930
s.SetCookie(tsURL, "one", "first")
3031
s.SetCookie(tsURL, "two", "second")

0 commit comments

Comments
 (0)