Skip to content

Commit b9686a6

Browse files
committed
update: 查看路由列表和请求响应
1 parent 43753b4 commit b9686a6

File tree

4 files changed

+410
-12
lines changed

4 files changed

+410
-12
lines changed

server.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package requests
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"net/http"
78
"net/http/pprof"
89
"net/url"
@@ -83,17 +84,17 @@ func (node *Node) paths() []string {
8384
}
8485

8586
// Print print trie tree struct
86-
func (node *Node) Print() {
87-
node.print(0)
87+
func (node *Node) Print(w io.Writer) {
88+
node.print(0, w)
8889
}
8990

90-
func (node *Node) print(m int) {
91+
func (node *Node) print(m int, w io.Writer) {
9192
paths := node.paths()
9293
for method, handler := range node.methods {
93-
fmt.Printf("%spath=%s, method=%s, handler=%v, next=%#v\n", strings.Repeat(" ", m), node.path, method, handler, paths)
94+
fmt.Fprintf(w, "%spath=%s, method=%s, handler=%v, next=%#v\n", strings.Repeat(" ", m), node.path, method, handler, paths)
9495
}
9596
for _, p := range paths {
96-
node.next[p].print(m + 1)
97+
node.next[p].print(m+1, w)
9798
}
9899
}
99100

@@ -112,8 +113,8 @@ func NewServeMux(opts ...Option) *ServeMux {
112113
}
113114

114115
// Print print trie tree struct.
115-
func (mux *ServeMux) Print() {
116-
mux.root.Print()
116+
func (mux *ServeMux) Print(w io.Writer) {
117+
mux.root.Print(w)
117118
}
118119

119120
// HandleFunc set func pattern path to handle

server_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func Test_Use(t *testing.T) {
260260
rec := httptest.NewRecorder()
261261
req := httptest.NewRequest("GET", "/test", nil)
262262
mux.ServeHTTP(rec, req)
263-
mux.Print()
263+
mux.Print(os.Stdout)
264264
expected := []string{
265265
"before_global1", "before_global2", "before_global3", "before_local",
266266
"handler",
@@ -285,7 +285,7 @@ func Test_Node(t *testing.T) {
285285
r.Add("/abc/def/", f)
286286
r.Add("/abc/def/", f)
287287
r.Add("/", f)
288-
r.Print()
288+
r.Print(os.Stdout)
289289
//go ListenAndServe(context.Background(), r, URL("0.0.0.0:1234"))
290290
//fmt.Println(r)
291291
}
@@ -566,7 +566,7 @@ func TestNode_PathsAndPrint(t *testing.T) {
566566

567567
// 测试打印功能
568568
// 因为打印到标准输出,这里只验证不会 panic
569-
node.Print()
569+
node.Print(os.Stdout)
570570
}
571571

572572
func Test_Methods(t *testing.T) {

stat.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,17 @@ type Stat struct {
4545

4646
// String implement fmt.Stringer interface.
4747
func (stat *Stat) String() string {
48-
b, _ := json.Marshal(stat)
49-
return string(b)
48+
return a2s(stat)
49+
}
50+
51+
// RequestBody return request body as string
52+
func (stat *Stat) RequestBody() string {
53+
return a2s(stat.Request.Body)
54+
}
55+
56+
// ResponseBody return response body as string
57+
func (stat *Stat) ResponseBody() string {
58+
return a2s(stat.Response.Body)
5059
}
5160

5261
// Print is used for server side
@@ -160,3 +169,11 @@ func serveLoad(w *ResponseWriter, r *http.Request, start time.Time, buf *bytes.B
160169
stat.Response.Body = w.Content.String()
161170
return stat
162171
}
172+
173+
func a2s(v any) string {
174+
b, err := json.Marshal(v)
175+
if err != nil {
176+
return fmt.Sprintf("%v", v)
177+
}
178+
return string(b)
179+
}

0 commit comments

Comments
 (0)