清理代码 & 支持 ignore

This commit is contained in:
dragon
2025-05-09 14:50:10 +08:00
parent 9413188aa9
commit ef31433d91
14 changed files with 405 additions and 118 deletions

46
tests/core/vhserver.go Normal file
View File

@@ -0,0 +1,46 @@
package core
import (
"fmt"
"net"
"net/http"
"go.uber.org/zap"
)
type VServer struct {
URL string
mux *http.ServeMux
listener net.Listener
}
func NewServer() *VServer {
listener, err := net.Listen("tcp", ":0") // ":0" 表示让系统自动选择一个可用的端口
if err != nil {
panic(err)
}
port := listener.Addr().(*net.TCPAddr).Port
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
zap.L().Debug("ServeHTTP", zap.String("url", r.URL.String()))
})
go func() {
_ = http.Serve(listener, mux)
}()
return &VServer{
listener: listener,
URL: fmt.Sprintf("http://127.0.0.1:%d", port),
mux: mux,
}
}
func (v *VServer) Add(path, data string) {
v.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(data))
})
}
func (v *VServer) Close() error {
return v.listener.Close()
}