-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<feat>(blog): add function for handling response and test
- Added pagination , covert and app which process response result in JSON format and set sets the http status code. - Added some test for `global.Logger.SetTraceInfo` and `app.NewResponse(c).ToResponse` - Added a ping router name `test/ping` to testing logger and ensure the service is alive. the router responds with a simple message . - Replaced `interface` with `any` in some parts of the code. resolves issue #12
- Loading branch information
Showing
10 changed files
with
259 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ping | ||
|
||
import ( | ||
"blog-server/global" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type Ping struct{} | ||
|
||
func NewPing() Ping { | ||
return Ping{} | ||
} | ||
func (p *Ping) Pong(c *gin.Context) { | ||
global.Logger.SetTraceInfo(c).Infof(c, "%s for test ping,path:%s", c.HandlerName(), c.Request.URL.Path) | ||
|
||
c.JSON(http.StatusOK, map[string]string{ | ||
"msg": "pong", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-playground/assert/v2" | ||
"github.com/lc-1010/OneBlogService/global" | ||
"github.com/lc-1010/OneBlogService/pkg/app" | ||
) | ||
|
||
func TestPing(t *testing.T) { | ||
r := gin.Default() | ||
r.GET("/ping", func(ctx *gin.Context) { | ||
global.Logger.SetTraceInfo(ctx).Infof(ctx, "%s for test ping,path:%s", ctx.HandlerName(), ctx.Request.URL.Path) | ||
ctx.JSON(http.StatusOK, gin.H{"message": "pong"}) | ||
}) | ||
r.Run() | ||
} | ||
|
||
func TestResponse(t *testing.T) { | ||
r := gin.New() | ||
r.GET("/test/ping", func(c *gin.Context) { | ||
app.NewResponse(c).ToResponse(map[string]string{"msg": "ping pong is ok"}) | ||
}) | ||
|
||
req := httptest.NewRequest("GET", "/test/ping", nil) | ||
w := httptest.NewRecorder() | ||
c, _ := gin.CreateTestContext(w) | ||
c.Request = req | ||
r.HandleContext(c) | ||
assert.Equal(t, w.Code, http.StatusOK) | ||
|
||
expcted := `{"msg":"ping pong is ok"}` | ||
assert.Equal(t, w.Body.String(), expcted) | ||
|
||
} | ||
func TestMultiHandler(t *testing.T) { | ||
r := gin.Default() | ||
|
||
// 在路由中间件中设置消息 | ||
r.Use(func(c *gin.Context) { | ||
c.Set("message", "Hello, world!") | ||
c.Next() | ||
}) | ||
|
||
// 注册一个路由,将多个处理函数关联到这个路由上 | ||
r.GET("/hello", func(c *gin.Context) { | ||
// message := c.MustGet("message").(string) | ||
// c.String(http.StatusOK, message) | ||
// c.Set("message", message) | ||
c.Next() | ||
}, func(c *gin.Context) { | ||
message := c.MustGet("message").(string) | ||
c.String(http.StatusOK, message+" from world") | ||
}) | ||
|
||
// 创建一个新的 HTTP 请求 | ||
req, err := http.NewRequest("GET", "/hello", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// 创建一个新的 HTTP 响应 | ||
w := httptest.NewRecorder() | ||
|
||
// 发送 HTTP 请求到路由处理函数 | ||
r.ServeHTTP(w, req) | ||
|
||
// 检查 HTTP 响应的状态码和内容 | ||
if w.Code != http.StatusOK { | ||
t.Errorf("Unexpected status code %d", w.Code) | ||
} | ||
exp := "Hello, world! from world" | ||
//msg := fmt.Sprintf("\nUnexpected body: %s", exp) | ||
assert.Equal(t, w.Body.String(), exp) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 返回处理 c.JSON respone | ||
// 内容列表整合 pagination | ||
package app | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/lc-1010/OneBlogService/pkg/errcode" | ||
) | ||
|
||
type Response struct { | ||
Ctx *gin.Context | ||
} | ||
|
||
type Pager struct { | ||
Page int `json:"page,omitempty"` | ||
PageSize int `json:"page_size,omitempty"` | ||
TotalRows int `json:"total_rows,omitempty"` | ||
} | ||
|
||
func NewResponse(ctx *gin.Context) *Response { | ||
return &Response{Ctx: ctx} | ||
} | ||
|
||
func (r *Response) ToResponse(data any) { | ||
if data == nil { | ||
data = gin.H{} | ||
} | ||
r.Ctx.JSON(http.StatusOK, data) | ||
} | ||
|
||
func (r *Response) ToResponseList(list any, totalRows int) { | ||
r.Ctx.JSON(http.StatusOK, gin.H{ | ||
"list": list, | ||
"pager": Pager{ | ||
Page: GetPage(r.Ctx), | ||
PageSize: GetPageSize(r.Ctx), | ||
TotalRows: totalRows, | ||
}, | ||
}) | ||
} | ||
|
||
func (r *Response) ToErrorResponse(err *errcode.Error) { | ||
response := gin.H{"code": err.Code(), "msg": err.Msg()} | ||
details := err.Details() | ||
if len(details) > 0 { | ||
response["details"] = details | ||
} | ||
r.Ctx.JSON(err.StatusCode(), response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// 分页处理方法 | ||
// 页面数 | ||
package app | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/lc-1010/OneBlogService/convert" | ||
"github.com/lc-1010/OneBlogService/global" | ||
) | ||
|
||
func GetPage(c *gin.Context) int { | ||
page := convert.StrTo(c.Query("page")).MustInt() | ||
if page <= 0 { | ||
return 1 | ||
} | ||
return page | ||
} | ||
|
||
func GetPageSize(c *gin.Context) int { | ||
pageSize := convert.StrTo(c.Query("page_size")).MustInt() | ||
if pageSize <= 0 { | ||
return global.AppSetting.DefaultPageSize | ||
} | ||
if pageSize > global.AppSetting.MaxPageSize { | ||
return global.AppSetting.MaxPageSize | ||
} | ||
|
||
return pageSize | ||
} | ||
|
||
func GetPageOffset(page, pageSize int) int { | ||
reuslt := 0 | ||
if page > 0 { | ||
reuslt = (page - 1) * pageSize | ||
} | ||
|
||
return reuslt | ||
} | ||
|
||
/************************************/ | ||
/*********** ***********/ | ||
/************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package convert | ||
|
||
import "strconv" | ||
|
||
type StrTo string | ||
|
||
func (s StrTo) String() string { | ||
return string(s) | ||
} | ||
|
||
func (s StrTo) Int() (int, error) { | ||
v, err := strconv.Atoi(s.String()) | ||
return v, err | ||
} | ||
|
||
func (s StrTo) MustInt() int { | ||
v, _ := s.Int() | ||
return v | ||
} | ||
|
||
func (s StrTo) UInt32() (uint32, error) { | ||
v, err := strconv.Atoi(s.String()) | ||
return uint32(v), err | ||
} | ||
|
||
func (s StrTo) MustUIn32() uint32 { | ||
v, _ := s.UInt32() | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters