This commit is contained in:
Kevin Wan
2022-08-24 20:19:53 +08:00
committed by GitHub
parent 847a396f1c
commit 4cb68a034a
2 changed files with 39 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/chain"
"github.com/zeromicro/go-zero/rest/httpx"
"github.com/zeromicro/go-zero/rest/internal/cors"
"github.com/zeromicro/go-zero/rest/router"
)
@@ -515,3 +516,23 @@ func TestServer_WithChain(t *testing.T) {
rt.ServeHTTP(httptest.NewRecorder(), req)
assert.Equal(t, int32(5), atomic.LoadInt32(&called))
}
func TestServer_WithCors(t *testing.T) {
var called int32
middleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&called, 1)
next.ServeHTTP(w, r)
})
}
r := router.NewRouter()
assert.Nil(t, r.Handle(http.MethodOptions, "/", middleware(http.NotFoundHandler())))
cr := &corsRouter{
Router: r,
middleware: cors.Middleware(nil, "*"),
}
req := httptest.NewRequest(http.MethodOptions, "/", nil)
cr.ServeHTTP(httptest.NewRecorder(), req)
assert.Equal(t, int32(0), atomic.LoadInt32(&called))
}