chore: add more tests (#3299)

This commit is contained in:
Kevin Wan
2023-05-29 23:44:36 +08:00
committed by GitHub
parent e751736516
commit 8a4cc4f98d
4 changed files with 52 additions and 18 deletions

View File

@@ -2,6 +2,7 @@ package rest
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net/http"
@@ -15,6 +16,7 @@ import (
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/fs"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/router"
)
const (
@@ -200,6 +202,7 @@ Verbose: true
},
}
var index int32
for _, yaml := range yamls {
yaml := yaml
for _, route := range routes {
@@ -208,6 +211,11 @@ Verbose: true
var cnf RestConf
assert.Nil(t, conf.LoadFromYamlBytes([]byte(yaml), &cnf))
ng := newEngine(cnf)
if atomic.AddInt32(&index, 1)%2 == 0 {
ng.setUnsignedCallback(func(w http.ResponseWriter, r *http.Request,
next http.Handler, strict bool, code int) {
})
}
ng.addRoutes(route)
ng.use(func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@@ -398,6 +406,29 @@ func TestEngine_withTimeout(t *testing.T) {
}
}
func TestEngine_start(t *testing.T) {
logx.Disable()
t.Run("http", func(t *testing.T) {
ng := newEngine(RestConf{
Host: "localhost",
Port: -1,
})
assert.Error(t, ng.start(router.NewRouter()))
})
t.Run("https", func(t *testing.T) {
ng := newEngine(RestConf{
Host: "localhost",
Port: -1,
CertFile: "foo",
KeyFile: "bar",
})
ng.tlsConfig = &tls.Config{}
assert.Error(t, ng.start(router.NewRouter()))
})
}
type mockedRouter struct {
}