清理代码 & 支持 ignore
This commit is contained in:
@@ -19,7 +19,7 @@ type ProviderDummy struct {
|
||||
}
|
||||
|
||||
func NewDummy() (*ProviderDummy, error) {
|
||||
temp, err := os.MkdirTemp("", "dummy")
|
||||
temp, err := os.MkdirTemp("", "dummy-*")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
"gopkg.d7z.net/gitea-pages/pkg"
|
||||
)
|
||||
@@ -20,7 +21,9 @@ type TestServer struct {
|
||||
type SvcOpts func(options *pkg.ServerOptions)
|
||||
|
||||
func NewDefaultTestServer() *TestServer {
|
||||
return NewTestServer("example.com")
|
||||
return NewTestServer("example.com", func(options *pkg.ServerOptions) {
|
||||
options.MetaTTL = 0
|
||||
})
|
||||
}
|
||||
|
||||
func NewTestServer(domain string, opts ...SvcOpts) *TestServer {
|
||||
@@ -47,13 +50,13 @@ func NewTestServer(domain string, opts ...SvcOpts) *TestServer {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TestServer) AddFile(path, data string) {
|
||||
func (t *TestServer) AddFile(path, data string, args ...interface{}) {
|
||||
join := filepath.Join(t.dummy.BaseDir, path)
|
||||
err := os.MkdirAll(filepath.Dir(join), 0o755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = os.WriteFile(join, []byte(data), 0o644)
|
||||
err = os.WriteFile(join, []byte(fmt.Sprintf(data, args...)), 0o644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -67,7 +70,7 @@ func (t *TestServer) OpenFile(url string) ([]byte, *http.Response, error) {
|
||||
defer response.Body.Close()
|
||||
}
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, response, fmt.Errorf(response.Status)
|
||||
return nil, response, errors.New(response.Status)
|
||||
}
|
||||
all, _ := io.ReadAll(response.Body)
|
||||
return all, response, nil
|
||||
|
||||
46
tests/core/vhserver.go
Normal file
46
tests/core/vhserver.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user