chore: add unit tests (#1615)

* test: add more tests

* test: add more tests
This commit is contained in:
Kevin Wan
2022-03-04 17:54:09 +08:00
committed by GitHub
parent 60760b52ab
commit 3b7ca86e4f
19 changed files with 108 additions and 69 deletions

View File

@@ -56,22 +56,22 @@ Port: 54321
}
for _, test := range tests {
var srv *Server
var svr *Server
var err error
if test.fail {
_, err = NewServer(test.c, test.opts...)
assert.NotNil(t, err)
continue
} else {
srv = MustNewServer(test.c, test.opts...)
svr = MustNewServer(test.c, test.opts...)
}
srv.Use(ToMiddleware(func(next http.Handler) http.Handler {
svr.Use(ToMiddleware(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
}))
srv.AddRoute(Route{
svr.AddRoute(Route{
Method: http.MethodGet,
Path: "/",
Handler: nil,
@@ -89,8 +89,8 @@ Port: 54321
}
}()
srv.Start()
srv.Stop()
svr.Start()
svr.Stop()
}()
}
}
@@ -290,9 +290,9 @@ Port: 54321
}
for _, testCase := range testCases {
srv, err := NewServer(testCase.c, testCase.opts...)
svr, err := NewServer(testCase.c, testCase.opts...)
assert.Nil(t, err)
assert.Equal(t, srv.ngin.tlsConfig, testCase.res)
assert.Equal(t, svr.ngin.tlsConfig, testCase.res)
}
}
@@ -304,11 +304,11 @@ Port: 54321
var cnf RestConf
assert.Nil(t, conf.LoadConfigFromYamlBytes([]byte(configYaml), &cnf))
rt := router.NewRouter()
srv, err := NewServer(cnf, WithRouter(rt))
svr, err := NewServer(cnf, WithRouter(rt))
assert.Nil(t, err)
opt := WithCors("local")
opt(srv)
opt(svr)
}
func TestWithCustomCors(t *testing.T) {
@@ -319,7 +319,7 @@ Port: 54321
var cnf RestConf
assert.Nil(t, conf.LoadConfigFromYamlBytes([]byte(configYaml), &cnf))
rt := router.NewRouter()
srv, err := NewServer(cnf, WithRouter(rt))
svr, err := NewServer(cnf, WithRouter(rt))
assert.Nil(t, err)
opt := WithCustomCors(func(header http.Header) {
@@ -327,5 +327,5 @@ Port: 54321
}, func(w http.ResponseWriter) {
w.WriteHeader(http.StatusOK)
}, "local")
opt(srv)
opt(svr)
}