代码拉取完成,页面将自动刷新
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// static service testing.
package ghttp_test
import (
"fmt"
"testing"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/guid"
)
func Test_Log(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
logDir := gfile.Temp(gtime.TimestampNanoStr())
s := g.Server(guid.S())
s.BindHandler("/hello", func(r *ghttp.Request) {
r.Response.Write("hello")
})
s.BindHandler("/error", func(r *ghttp.Request) {
panic("custom error")
})
s.SetLogPath(logDir)
s.SetAccessLogEnabled(true)
s.SetErrorLogEnabled(true)
s.SetLogStdout(false)
s.Start()
defer s.Shutdown()
defer gfile.Remove(logDir)
time.Sleep(100 * time.Millisecond)
client := g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
t.Assert(client.GetContent(ctx, "/hello"), "hello")
t.Assert(client.GetContent(ctx, "/error"), "exception recovered: custom error")
var (
logPath1 = gfile.Join(logDir, gtime.Now().Format("Y-m-d")+".log")
content = gfile.GetContents(logPath1)
)
t.Assert(gstr.Contains(content, "http server started listening on"), true)
t.Assert(gstr.Contains(content, "HANDLER"), true)
logPath2 := gfile.Join(logDir, "access-"+gtime.Now().Format("Ymd")+".log")
// fmt.Println(gfile.GetContents(logPath2))
t.Assert(gstr.Contains(gfile.GetContents(logPath2), " /hello "), true)
logPath3 := gfile.Join(logDir, "error-"+gtime.Now().Format("Ymd")+".log")
// fmt.Println(gfile.GetContents(logPath3))
t.Assert(gstr.Contains(gfile.GetContents(logPath3), "custom error"), true)
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。