Skip to content

Commit a8e9d47

Browse files
author
Qi Xiao
committed
Add test for utils/log/debug package
1 parent 1d346be commit a8e9d47

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

utils/log/debug/handler_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2019 The CovenantSQL Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package debug_test
18+
19+
import (
20+
"encoding/json"
21+
"net"
22+
"net/http"
23+
"testing"
24+
25+
"github.com/jmoiron/jsonq"
26+
. "github.com/smartystreets/goconvey/convey"
27+
28+
"github.com/CovenantSQL/CovenantSQL/utils/log"
29+
)
30+
31+
func parseResponse(resp *http.Response, r error) (result *jsonq.JsonQuery, err error) {
32+
if r != nil {
33+
err = r
34+
return
35+
}
36+
37+
var res map[string]interface{}
38+
err = json.NewDecoder(resp.Body).Decode(&res)
39+
if err != nil {
40+
return
41+
}
42+
43+
result = jsonq.NewQuery(res)
44+
return
45+
}
46+
47+
func mustJSONQ(c C) func(interface{}, error) interface{} {
48+
return func(i interface{}, e error) interface{} {
49+
c.So(e, ShouldBeNil)
50+
return i
51+
}
52+
}
53+
54+
func 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+
}

0 commit comments

Comments
 (0)