MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / TestDebugHandler

Function TestDebugHandler

utils/log/debug/handler_test.go:54–105  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

52}
53
54func TestDebugHandler(t *testing.T) {
55 Convey("test debug handler", t, func(c C) {
56 server := http.Server{}
57 listener, err := net.Listen("tcp", ":0")
58 So(err, ShouldBeNil)
59 defer func() {
60 _ = listener.Close()
61 }()
62 go func() {
63 _ = server.Serve(listener)
64 }()
65 log.SetLevel(log.DebugLevel)
66 url := "http://" + listener.Addr().String() + "/debug/covenantsql/loglevel"
67 resp, err := parseResponse(http.Get(url))
68 So(err, ShouldBeNil)
69 So(mustJSONQ(c)(resp.String("level")), ShouldEqual, log.GetLevel().String())
70 resp, err = parseResponse(http.PostForm(url, map[string][]string{"level": {"fatal"}}))
71 So(err, ShouldBeNil)
72 So(mustJSONQ(c)(resp.String("level")), ShouldEqual, log.GetLevel().String())
73 So(log.GetLevel().String(), ShouldEqual, "fatal")
74 So(mustJSONQ(c)(resp.String("orig")), ShouldEqual, "debug")
75 So(mustJSONQ(c)(resp.String("want")), ShouldEqual, "fatal")
76 resp, err = parseResponse(http.PostForm(url, map[string][]string{"level": {"info"}}))
77 So(err, ShouldBeNil)
78 So(mustJSONQ(c)(resp.String("level")), ShouldEqual, log.GetLevel().String())
79 So(log.GetLevel().String(), ShouldEqual, "info")
80 So(mustJSONQ(c)(resp.String("orig")), ShouldEqual, "fatal")
81 So(mustJSONQ(c)(resp.String("want")), ShouldEqual, "info")
82
83 // test invalid level
84 resp, err = parseResponse(http.PostForm(url, map[string][]string{"level": {"happy"}}))
85 So(err, ShouldBeNil)
86 So(mustJSONQ(c)(resp.String("level")), ShouldEqual, log.GetLevel().String())
87 So(log.GetLevel().String(), ShouldEqual, "info")
88 So(mustJSONQ(c)(resp.String("orig")), ShouldEqual, "info")
89 So(mustJSONQ(c)(resp.String("want")), ShouldEqual, "happy")
90 So(mustJSONQ(c)(resp.String("err")), ShouldNotBeEmpty)
91
92 // test empty level
93 resp, err = parseResponse(http.PostForm(url, nil))
94 So(err, ShouldBeNil)
95 So(mustJSONQ(c)(resp.String("level")), ShouldEqual, log.GetLevel().String())
96 So(log.GetLevel().String(), ShouldEqual, "info")
97 So(mustJSONQ(c)(resp.String("orig")), ShouldEqual, "info")
98
99 // test invalid query
100 rawResp, err := http.Head(url)
101 So(err, ShouldBeNil)
102 So(rawResp.StatusCode, ShouldEqual, http.StatusBadRequest)
103
104 })
105}

Callers

nothing calls this directly

Calls 9

ServeMethod · 0.95
SetLevelFunction · 0.92
GetLevelFunction · 0.92
parseResponseFunction · 0.85
mustJSONQFunction · 0.85
AddrMethod · 0.80
CloseMethod · 0.65
GetMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected