feat: 添加 dump request 中间件和处理程序 curl
This commit is contained in:
74
middlewares/dump/req_test.go
Normal file
74
middlewares/dump/req_test.go
Normal file
@ -0,0 +1,74 @@
|
||||
package dump
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"gitea.loveuer.com/yizhisec/packages/logger"
|
||||
"gitea.loveuer.com/yizhisec/packages/tool"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRequest(t *testing.T) {
|
||||
ready := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
app := gin.Default()
|
||||
app.Use(Request(t.Context(), RequestHandlerCurl))
|
||||
|
||||
app.GET("/hello", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"name": c.Query("name")})
|
||||
})
|
||||
|
||||
app.POST("/hello", func(c *gin.Context) {
|
||||
type Req struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = new(Req)
|
||||
)
|
||||
|
||||
if err = c.BindJSON(req); err != nil {
|
||||
c.JSON(200, gin.H{"err": err})
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"id": req.Id, "name": req.Name})
|
||||
})
|
||||
|
||||
logger.Fatal(app.Run(":18080").Error())
|
||||
}()
|
||||
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
for _ = range 10 {
|
||||
_, err := http.Get("http://localhost:18080/hello?name=" + tool.RandomName())
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
for _ = range 5 {
|
||||
bs, _ := json.Marshal(map[string]interface{}{"id": tool.RandomInt(30), "name": tool.RandomName()})
|
||||
req, err := http.NewRequest(http.MethodPost, "http://localhost:18080/hello", bytes.NewReader(bs))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
_, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
ready <- struct{}{}
|
||||
}()
|
||||
|
||||
<-ready
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
Reference in New Issue
Block a user