-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathdebug.go
More file actions
61 lines (47 loc) · 1.49 KB
/
Copy pathdebug.go
File metadata and controls
61 lines (47 loc) · 1.49 KB
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
package surf
import (
"github.com/enetx/g"
"github.com/enetx/http/httputil"
)
// Debug is a struct that holds debugging information for an HTTP response.
type Debug struct {
print g.Builder // Debug information text.
resp Response // Associated Response.
}
// Debug returns a debug instance associated with a Response.
func (resp Response) Debug() *Debug { return &Debug{resp: resp} }
// Print prints the debug information.
func (d *Debug) Print() { g.Println(d.print.String()) }
// Request appends the request details to the debug information.
func (d *Debug) Request(verbos ...bool) *Debug {
body, err := httputil.DumpRequestOut(d.resp.request.request, false)
if err != nil {
return d
}
if d.print.Len() != 0 {
_ = g.Write(&d.print, "\n")
}
_ = g.Writeln(&d.print, "{}", g.String(body).Trim())
if len(verbos) != 0 && verbos[0] && d.resp.request.bodyBytes != nil {
reqBody := g.Bytes(d.resp.request.bodyBytes).Trim()
_ = g.Writeln(&d.print, "\n{}", reqBody.String())
}
return d
}
// Response appends the response details to the debug information.
func (d *Debug) Response(verbos ...bool) *Debug {
body, err := httputil.DumpResponse(d.resp.response, false)
if err != nil {
return d
}
if d.print.Len() != 0 {
_ = g.Write(&d.print, "\n")
}
_ = g.Write(&d.print, g.String(body).Trim())
if len(verbos) != 0 && verbos[0] && d.resp.Body != nil {
if bodyStr := d.resp.Body.String(); bodyStr.IsOk() {
_ = g.Write(&d.print, bodyStr.Ok().Trim().Prepend("\n\n"))
}
}
return d
}