fix codes due to static codes analysis (#2935)

This commit is contained in:
海亮
2023-09-07 20:22:20 +08:00
committed by GitHub
parent d49dccb147
commit 7e16d9b63e
11 changed files with 56 additions and 63 deletions

View File

@ -63,11 +63,6 @@ type Request struct {
originUrlPath string // Original URL path that passed from client.
}
type handlerResponse struct {
Object interface{}
Error error
}
// staticFile is the file struct for static file service.
type staticFile struct {
File *gres.File // Resource file object.

View File

@ -101,9 +101,9 @@ func (m *middleware) Next() {
loop = false
}
}, func(ctx context.Context, exception error) {
if v, ok := exception.(error); ok && gerror.HasStack(v) {
if gerror.HasStack(exception) {
// It's already an error that has stack info.
m.request.error = v
m.request.error = exception
} else {
// Create a new error with stack info.
// Note that there's a skip pointing the start stacktrace

View File

@ -232,7 +232,7 @@ func (r *Request) mergeInTagStructValue(data map[string]interface{}, pointer int
)
for k, v := range r.Header {
if v != nil && len(v) > 0 {
if len(v) > 0 {
headerMap[k] = v[0]
}
}

View File

@ -97,11 +97,6 @@ func (s *gracefulServer) Fd() uintptr {
return 0
}
// setFd sets the file descriptor for current server.
func (s *gracefulServer) setFd(fd int) {
s.fd = uintptr(fd)
}
// CreateListener creates listener on configured address.
func (s *gracefulServer) CreateListener() error {
ln, err := s.getNetListener()

View File

@ -159,6 +159,8 @@ func (s *Server) doSetHandler(
switch item.Type {
case HandlerTypeHandler, HandlerTypeObject:
duplicatedHandler = items[i]
}
if duplicatedHandler != nil {
break
}
}

View File

@ -14,7 +14,7 @@ import (
func Benchmark_TrimRightCharWithStrings(b *testing.B) {
for i := 0; i < b.N; i++ {
path := "//////////"
path = strings.TrimRight(path, "/")
strings.TrimRight(path, "/")
}
}

View File

@ -205,8 +205,8 @@ func Test_Middleware_Status(t *testing.T) {
t.Assert(client.GetContent(ctx, "/user/list"), "200")
resp, err := client.Get(ctx, "/")
defer resp.Close()
t.AssertNil(err)
defer resp.Close()
t.Assert(resp.StatusCode, 404)
})
}
@ -664,7 +664,7 @@ func Test_Middleware_Panic(t *testing.T) {
group.Middleware(func(r *ghttp.Request) {
i++
panic("error")
r.Middleware.Next()
// r.Middleware.Next()
}, func(r *ghttp.Request) {
i++
r.Middleware.Next()

View File

@ -9,11 +9,11 @@ package ghttp_test
import (
"context"
"fmt"
"github.com/gogf/gf/v2/encoding/gbase64"
"net/http"
"testing"
"time"
"github.com/gogf/gf/v2/encoding/gbase64"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/test/gtest"
@ -213,15 +213,17 @@ func Test_Request_BasicAuth(t *testing.T) {
}
func Test_Request_SetCtx(t *testing.T) {
type ctxKey string
const testkey ctxKey = "test"
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.Middleware(func(r *ghttp.Request) {
ctx := context.WithValue(r.Context(), "test", 1)
ctx := context.WithValue(r.Context(), testkey, 1)
r.SetCtx(ctx)
r.Middleware.Next()
})
group.ALL("/", func(r *ghttp.Request) {
r.Response.Write(r.Context().Value("test"))
r.Response.Write(r.Context().Value(testkey))
})
})
s.SetDumpRouterMap(false)
@ -238,15 +240,17 @@ func Test_Request_SetCtx(t *testing.T) {
}
func Test_Request_GetCtx(t *testing.T) {
type ctxKey string
const testkey ctxKey = "test"
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.Middleware(func(r *ghttp.Request) {
ctx := context.WithValue(r.GetCtx(), "test", 1)
ctx := context.WithValue(r.GetCtx(), testkey, 1)
r.SetCtx(ctx)
r.Middleware.Next()
})
group.ALL("/", func(r *ghttp.Request) {
r.Response.Write(r.Context().Value("test"))
r.Response.Write(r.Context().Value(testkey))
})
})
s.SetDumpRouterMap(false)
@ -290,9 +294,6 @@ func Test_Request_Form(t *testing.T) {
Id int
Name string
}
type Default struct {
D string
}
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.ALL("/", func(r *ghttp.Request) {

View File

@ -122,23 +122,23 @@ func Test_Router_Method(t *testing.T) {
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
resp1, err := client.Get(ctx, "/get")
defer resp1.Close()
t.AssertNil(err)
defer resp1.Close()
t.Assert(resp1.StatusCode, 200)
resp2, err := client.Post(ctx, "/get")
defer resp2.Close()
t.AssertNil(err)
defer resp2.Close()
t.Assert(resp2.StatusCode, 404)
resp3, err := client.Get(ctx, "/post")
defer resp3.Close()
t.AssertNil(err)
defer resp3.Close()
t.Assert(resp3.StatusCode, 404)
resp4, err := client.Post(ctx, "/post")
defer resp4.Close()
t.AssertNil(err)
defer resp4.Close()
t.Assert(resp4.StatusCode, 200)
})
}
@ -194,28 +194,28 @@ func Test_Router_Status(t *testing.T) {
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
resp1, err := client.Get(ctx, "/200")
defer resp1.Close()
t.AssertNil(err)
defer resp1.Close()
t.Assert(resp1.StatusCode, 200)
resp2, err := client.Get(ctx, "/300")
defer resp2.Close()
t.AssertNil(err)
defer resp2.Close()
t.Assert(resp2.StatusCode, 300)
resp3, err := client.Get(ctx, "/400")
defer resp3.Close()
t.AssertNil(err)
defer resp3.Close()
t.Assert(resp3.StatusCode, 400)
resp4, err := client.Get(ctx, "/500")
defer resp4.Close()
t.AssertNil(err)
defer resp4.Close()
t.Assert(resp4.StatusCode, 500)
resp5, err := client.Get(ctx, "/404")
defer resp5.Close()
t.AssertNil(err)
defer resp5.Close()
t.Assert(resp5.StatusCode, 404)
})
}
@ -239,8 +239,8 @@ func Test_Router_CustomStatusHandler(t *testing.T) {
t.Assert(client.GetContent(ctx, "/"), "hello")
resp, err := client.Get(ctx, "/ThisDoesNotExist")
defer resp.Close()
t.AssertNil(err)
defer resp.Close()
t.Assert(resp.StatusCode, 404)
t.Assert(resp.ReadAllString(), "404 page")
})
@ -263,8 +263,8 @@ func Test_Router_404(t *testing.T) {
t.Assert(client.GetContent(ctx, "/"), "hello")
resp, err := client.Get(ctx, "/ThisDoesNotExist")
defer resp.Close()
t.AssertNil(err)
defer resp.Close()
t.Assert(resp.StatusCode, 404)
})
}

View File

@ -86,23 +86,23 @@ func Test_Router_DomainMethod(t *testing.T) {
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
resp1, err := client.Get(ctx, "/get")
defer resp1.Close()
t.AssertNil(err)
defer resp1.Close()
t.Assert(resp1.StatusCode, 404)
resp2, err := client.Post(ctx, "/get")
defer resp2.Close()
t.AssertNil(err)
defer resp2.Close()
t.Assert(resp2.StatusCode, 404)
resp3, err := client.Get(ctx, "/post")
defer resp3.Close()
t.AssertNil(err)
defer resp3.Close()
t.Assert(resp3.StatusCode, 404)
resp4, err := client.Post(ctx, "/post")
defer resp4.Close()
t.AssertNil(err)
defer resp4.Close()
t.Assert(resp4.StatusCode, 404)
})
@ -111,23 +111,23 @@ func Test_Router_DomainMethod(t *testing.T) {
client.SetPrefix(fmt.Sprintf("http://localhost:%d", s.GetListenedPort()))
resp1, err := client.Get(ctx, "/get")
defer resp1.Close()
t.AssertNil(err)
defer resp1.Close()
t.Assert(resp1.StatusCode, 200)
resp2, err := client.Post(ctx, "/get")
defer resp2.Close()
t.AssertNil(err)
defer resp2.Close()
t.Assert(resp2.StatusCode, 404)
resp3, err := client.Get(ctx, "/post")
defer resp3.Close()
t.AssertNil(err)
defer resp3.Close()
t.Assert(resp3.StatusCode, 404)
resp4, err := client.Post(ctx, "/post")
defer resp4.Close()
t.AssertNil(err)
defer resp4.Close()
t.Assert(resp4.StatusCode, 200)
})
@ -136,23 +136,23 @@ func Test_Router_DomainMethod(t *testing.T) {
client.SetPrefix(fmt.Sprintf("http://local:%d", s.GetListenedPort()))
resp1, err := client.Get(ctx, "/get")
defer resp1.Close()
t.AssertNil(err)
defer resp1.Close()
t.Assert(resp1.StatusCode, 200)
resp2, err := client.Post(ctx, "/get")
defer resp2.Close()
t.AssertNil(err)
defer resp2.Close()
t.Assert(resp2.StatusCode, 404)
resp3, err := client.Get(ctx, "/post")
defer resp3.Close()
t.AssertNil(err)
defer resp3.Close()
t.Assert(resp3.StatusCode, 404)
resp4, err := client.Post(ctx, "/post")
defer resp4.Close()
t.AssertNil(err)
defer resp4.Close()
t.Assert(resp4.StatusCode, 200)
})
}
@ -182,23 +182,23 @@ func Test_Router_DomainStatus(t *testing.T) {
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
resp1, err := client.Get(ctx, "/200")
defer resp1.Close()
t.AssertNil(err)
defer resp1.Close()
t.Assert(resp1.StatusCode, 404)
resp2, err := client.Get(ctx, "/300")
defer resp2.Close()
t.AssertNil(err)
defer resp2.Close()
t.Assert(resp2.StatusCode, 404)
resp3, err := client.Get(ctx, "/400")
defer resp3.Close()
t.AssertNil(err)
defer resp3.Close()
t.Assert(resp3.StatusCode, 404)
resp4, err := client.Get(ctx, "/500")
defer resp4.Close()
t.AssertNil(err)
defer resp4.Close()
t.Assert(resp4.StatusCode, 404)
})
gtest.C(t, func(t *gtest.T) {
@ -206,23 +206,23 @@ func Test_Router_DomainStatus(t *testing.T) {
client.SetPrefix(fmt.Sprintf("http://localhost:%d", s.GetListenedPort()))
resp1, err := client.Get(ctx, "/200")
defer resp1.Close()
t.AssertNil(err)
defer resp1.Close()
t.Assert(resp1.StatusCode, 200)
resp2, err := client.Get(ctx, "/300")
defer resp2.Close()
t.AssertNil(err)
defer resp2.Close()
t.Assert(resp2.StatusCode, 300)
resp3, err := client.Get(ctx, "/400")
defer resp3.Close()
t.AssertNil(err)
defer resp3.Close()
t.Assert(resp3.StatusCode, 400)
resp4, err := client.Get(ctx, "/500")
defer resp4.Close()
t.AssertNil(err)
defer resp4.Close()
t.Assert(resp4.StatusCode, 500)
})
gtest.C(t, func(t *gtest.T) {
@ -230,23 +230,23 @@ func Test_Router_DomainStatus(t *testing.T) {
client.SetPrefix(fmt.Sprintf("http://local:%d", s.GetListenedPort()))
resp1, err := client.Get(ctx, "/200")
defer resp1.Close()
t.AssertNil(err)
defer resp1.Close()
t.Assert(resp1.StatusCode, 200)
resp2, err := client.Get(ctx, "/300")
defer resp2.Close()
t.AssertNil(err)
defer resp2.Close()
t.Assert(resp2.StatusCode, 300)
resp3, err := client.Get(ctx, "/400")
defer resp3.Close()
t.AssertNil(err)
defer resp3.Close()
t.Assert(resp3.StatusCode, 400)
resp4, err := client.Get(ctx, "/500")
defer resp4.Close()
t.AssertNil(err)
defer resp4.Close()
t.Assert(resp4.StatusCode, 500)
})
}

View File

@ -54,15 +54,15 @@ func Test_Server_Wrap_Handler(t *testing.T) {
client := g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d/api", s.GetListenedPort()))
response, er1 := client.Get(ctx, "/wrapf")
response, err := client.Get(ctx, "/wrapf")
t.AssertNil(err)
defer response.Close()
t.Assert(er1, nil)
t.Assert(response.StatusCode, http.StatusBadRequest)
t.Assert(response.ReadAllString(), str1)
response2, er2 := client.Post(ctx, "/wraph")
response2, err := client.Post(ctx, "/wraph")
t.AssertNil(err)
defer response2.Close()
t.Assert(er2, nil)
t.Assert(response2.StatusCode, http.StatusInternalServerError)
t.Assert(response2.ReadAllString(), str2)
})