fix sonar cloud problems
This commit is contained in:
parent
f50476e063
commit
d2cfd884c8
@ -24,7 +24,13 @@ import (
|
|||||||
"github.com/beego/beego/v2/server/web/context"
|
"github.com/beego/beego/v2/server/web/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
const exampleBody = "hello world"
|
const (
|
||||||
|
exampleBody = "hello world"
|
||||||
|
|
||||||
|
nsNamespace = "/router"
|
||||||
|
nsPath = "/user"
|
||||||
|
nsNamespacePath = "/router/user"
|
||||||
|
)
|
||||||
|
|
||||||
type ExampleController struct {
|
type ExampleController struct {
|
||||||
Controller
|
Controller
|
||||||
@ -38,7 +44,7 @@ func (m ExampleController) Ping() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m ExampleController) ping() {
|
func (m ExampleController) ping() {
|
||||||
err := m.Ctx.Output.Body([]byte(exampleBody))
|
err := m.Ctx.Output.Body([]byte("ping method"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
@ -189,11 +195,11 @@ func TestNamespaceInside(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceRouterGet(t *testing.T) {
|
func TestNamespaceRouterGet(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodGet, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodGet, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
ns.RouterGet("/user", ExampleController.Ping)
|
ns.RouterGet(nsPath, ExampleController.Ping)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -202,11 +208,11 @@ func TestNamespaceRouterGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceRouterPost(t *testing.T) {
|
func TestNamespaceRouterPost(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodPost, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodPost, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
ns.RouterPost("/user", ExampleController.Ping)
|
ns.RouterPost(nsPath, ExampleController.Ping)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -215,11 +221,11 @@ func TestNamespaceRouterPost(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceRouterDelete(t *testing.T) {
|
func TestNamespaceRouterDelete(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodDelete, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodDelete, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
ns.RouterDelete("/user", ExampleController.Ping)
|
ns.RouterDelete(nsPath, ExampleController.Ping)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -228,11 +234,11 @@ func TestNamespaceRouterDelete(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceRouterPut(t *testing.T) {
|
func TestNamespaceRouterPut(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodPut, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodPut, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
ns.RouterPut("/user", ExampleController.Ping)
|
ns.RouterPut(nsPath, ExampleController.Ping)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -241,11 +247,11 @@ func TestNamespaceRouterPut(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceRouterHead(t *testing.T) {
|
func TestNamespaceRouterHead(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodHead, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodHead, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
ns.RouterHead("/user", ExampleController.Ping)
|
ns.RouterHead(nsPath, ExampleController.Ping)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -254,11 +260,11 @@ func TestNamespaceRouterHead(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceRouterOptions(t *testing.T) {
|
func TestNamespaceRouterOptions(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodOptions, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodOptions, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
ns.RouterOptions("/user", ExampleController.Ping)
|
ns.RouterOptions(nsPath, ExampleController.Ping)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -267,11 +273,11 @@ func TestNamespaceRouterOptions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceRouterPatch(t *testing.T) {
|
func TestNamespaceRouterPatch(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodPatch, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodPatch, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
ns.RouterPatch("/user", ExampleController.Ping)
|
ns.RouterPatch(nsPath, ExampleController.Ping)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -280,13 +286,13 @@ func TestNamespaceRouterPatch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceRouterAny(t *testing.T) {
|
func TestNamespaceRouterAny(t *testing.T) {
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
ns.RouterAny("/user", ExampleController.Ping)
|
ns.RouterAny(nsPath, ExampleController.Ping)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
|
|
||||||
for method := range HTTPMETHOD {
|
for method := range HTTPMETHOD {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
r, _ := http.NewRequest(method, "/router/user", nil)
|
r, _ := http.NewRequest(method, nsNamespacePath, nil)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
t.Errorf("TestNamespaceRouterAny can't run, get the response is " + w.Body.String())
|
t.Errorf("TestNamespaceRouterAny can't run, get the response is " + w.Body.String())
|
||||||
@ -295,11 +301,11 @@ func TestNamespaceRouterAny(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceNSRouterGet(t *testing.T) {
|
func TestNamespaceNSRouterGet(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodGet, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodGet, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
NSRouterGet("/user", ExampleController.Ping)(ns)
|
NSRouterGet(nsPath, ExampleController.Ping)(ns)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -308,11 +314,11 @@ func TestNamespaceNSRouterGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceNSRouterPost(t *testing.T) {
|
func TestNamespaceNSRouterPost(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodPost, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodPost, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace("/router")
|
||||||
NSRouterPost("/user", ExampleController.Ping)(ns)
|
NSRouterPost(nsPath, ExampleController.Ping)(ns)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -321,11 +327,11 @@ func TestNamespaceNSRouterPost(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceNSRouterDelete(t *testing.T) {
|
func TestNamespaceNSRouterDelete(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodDelete, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodDelete, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
NSRouterDelete("/user", ExampleController.Ping)(ns)
|
NSRouterDelete(nsPath, ExampleController.Ping)(ns)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -334,11 +340,11 @@ func TestNamespaceNSRouterDelete(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceNSRouterPut(t *testing.T) {
|
func TestNamespaceNSRouterPut(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodPut, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodPut, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
NSRouterPut("/user", ExampleController.Ping)(ns)
|
NSRouterPut(nsPath, ExampleController.Ping)(ns)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -347,11 +353,11 @@ func TestNamespaceNSRouterPut(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceNSRouterHead(t *testing.T) {
|
func TestNamespaceNSRouterHead(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodHead, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodHead, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
NSRouterHead("/user", ExampleController.Ping)(ns)
|
NSRouterHead(nsPath, ExampleController.Ping)(ns)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -360,11 +366,11 @@ func TestNamespaceNSRouterHead(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceNSRouterOptions(t *testing.T) {
|
func TestNamespaceNSRouterOptions(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodOptions, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodOptions, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
NSRouterOptions("/user", ExampleController.Ping)(ns)
|
NSRouterOptions(nsPath, ExampleController.Ping)(ns)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
@ -373,10 +379,10 @@ func TestNamespaceNSRouterOptions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceNSRouterPatch(t *testing.T) {
|
func TestNamespaceNSRouterPatch(t *testing.T) {
|
||||||
r, _ := http.NewRequest(http.MethodPatch, "/router/user", nil)
|
r, _ := http.NewRequest(http.MethodPatch, nsNamespacePath, nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
NSRouterPatch("/user", ExampleController.Ping)(ns)
|
NSRouterPatch("/user", ExampleController.Ping)(ns)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
@ -386,13 +392,13 @@ func TestNamespaceNSRouterPatch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceNSRouterAny(t *testing.T) {
|
func TestNamespaceNSRouterAny(t *testing.T) {
|
||||||
ns := NewNamespace("/router")
|
ns := NewNamespace(nsNamespace)
|
||||||
NSRouterAny("/user", ExampleController.Ping)(ns)
|
NSRouterAny(nsPath, ExampleController.Ping)(ns)
|
||||||
AddNamespace(ns)
|
AddNamespace(ns)
|
||||||
|
|
||||||
for method := range HTTPMETHOD {
|
for method := range HTTPMETHOD {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
r, _ := http.NewRequest(method, "/router/user", nil)
|
r, _ := http.NewRequest(method, nsNamespacePath, nil)
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
if w.Body.String() != exampleBody {
|
if w.Body.String() != exampleBody {
|
||||||
t.Errorf("TestNamespaceNSRouterAny can't run, get the response is " + w.Body.String())
|
t.Errorf("TestNamespaceNSRouterAny can't run, get the response is " + w.Body.String())
|
||||||
|
|||||||
@ -39,6 +39,7 @@ type TestControllerWithInterface struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m TestControllerWithInterface) Ping() {
|
func (m TestControllerWithInterface) Ping() {
|
||||||
|
fmt.Println("pong")
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestController struct {
|
type TestController struct {
|
||||||
@ -951,7 +952,7 @@ func TestRouterAddRouterMethodPanicNotAMethod(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t.Errorf(fmt.Sprintf("TestRouterAddRouterMethodPanicInvalidMethod failed: %v", err))
|
t.Errorf(fmt.Sprintf("TestRouterAddRouterMethodPanicNotAMethod failed: %v", err))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
handler := NewControllerRegister()
|
handler := NewControllerRegister()
|
||||||
@ -969,7 +970,7 @@ func TestRouterAddRouterMethodPanicNotPublicMethod(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t.Errorf(fmt.Sprintf("TestRouterAddRouterMethodPanicInvalidMethod failed: %v", err))
|
t.Errorf(fmt.Sprintf("TestRouterAddRouterMethodPanicNotPublicMethod failed: %v", err))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
handler := NewControllerRegister()
|
handler := NewControllerRegister()
|
||||||
@ -987,7 +988,7 @@ func TestRouterAddRouterMethodPanicNotImplementInterface(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t.Errorf(fmt.Sprintf("TestRouterAddRouterMethodPanicInvalidNumberParamIn failed: %v", err))
|
t.Errorf(fmt.Sprintf("TestRouterAddRouterMethodPanicNotImplementInterface failed: %v", err))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
handler := NewControllerRegister()
|
handler := NewControllerRegister()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user