| 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 | } |