From 63f56c3a23d1b48af1cf1be17d0bad13238716e0 Mon Sep 17 00:00:00 2001 From: guonaihong Date: Tue, 7 Jan 2025 13:47:13 +0800 Subject: [PATCH] fix:395 (#396) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix:395 * 修改ci/cd * 修改最低版本 --- .github/workflows/go.yml | 2 +- dataflow/dataflow.go | 15 +++++++++++---- dataflow/dataflow_test.go | 26 ++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 1 - 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 15b0e8f..cb7b116 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go: ['1.15', '1.16', '1.17', '1.18', '1.19', '1.20'] + go: ['1.18', '1.19', '1.20', '1.21', '1.22', '1.23'] name: Go ${{ matrix.go }} sample steps: diff --git a/dataflow/dataflow.go b/dataflow/dataflow.go index d9e499f..ade82ed 100644 --- a/dataflow/dataflow.go +++ b/dataflow/dataflow.go @@ -5,6 +5,8 @@ import ( "errors" "net/http" "net/url" + "strings" + "text/template" "time" "github.com/guonaihong/gout/debug" @@ -132,13 +134,18 @@ func (df *DataFlow) SetURL(url string, urlStruct ...interface{}) *DataFlow { return df } - if df.Req.url == "" && df.Req.req == nil { - df.Req, df.Err = reqDef(df.method, cleanPaths(url), df.out, urlStruct...) - return df + if len(urlStruct) > 0 { + var out strings.Builder + tpl := template.Must(template.New(url).Parse(url)) + err := tpl.Execute(&out, urlStruct[0]) + if err != nil { + df.Err = err + return df + } + url = out.String() } df.Req.url = modifyURL(cleanPaths(url)) - return df } diff --git a/dataflow/dataflow_test.go b/dataflow/dataflow_test.go index 4c6c9ce..feafee1 100644 --- a/dataflow/dataflow_test.go +++ b/dataflow/dataflow_test.go @@ -18,6 +18,7 @@ import ( "github.com/gin-gonic/gin" "github.com/guonaihong/gout/core" + "github.com/guonaihong/gout/middler" "github.com/stretchr/testify/assert" ) @@ -216,6 +217,7 @@ func setupForm(t *testing.T, reqTestForm testForm) *gin.Engine { assert.Equal(t, reqTestForm.Text, t2.Text) */ }) + return router } @@ -616,6 +618,29 @@ func Test_DataFlow_SetURL(t *testing.T) { err = New().GET("123456").SetURL(ts.URL).SetBody(body).BindBody(&s).Do() assert.NoError(t, err) assert.Equal(t, body, s) + + // New test case to check if the body can be read when SetBody is called before SetMethod + s = "" + ts2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + bodyBytes, _ := io.ReadAll(r.Body) + w.Write(bodyBytes) + })) + defer ts2.Close() + + err = New(). + WithContext(context.Background()). + SetTimeout(time.Second * 100). + SetURL(ts2.URL). + SetBody("22222"). + SetMethod("POST"). + RequestUse(middler.WithRequestMiddlerFunc(func(req *http.Request) error { + bytes, _ := io.ReadAll(req.Body) + assert.Equal(t, "22222", string(bytes)) + req.Body = io.NopCloser(strings.NewReader("22222")) + return nil + })). + Do() + assert.NoError(t, err) } func Test_DataFlow_SetHost(t *testing.T) { @@ -695,6 +720,7 @@ func Test_DataFlow_Bind(t *testing.T) { // 测试错误的情况 router := func() *gin.Engine { router := gin.New() + router.GET("/", func(c *gin.Context) { c.String(200, "test SetDecod3") }) diff --git a/go.mod b/go.mod index a64feac..a2bda6a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/guonaihong/gout -go 1.17 +go 1.18 require ( github.com/andybalholm/brotli v1.0.4 diff --git a/go.sum b/go.sum index 8c25b7c..d13d60f 100644 --- a/go.sum +++ b/go.sum @@ -90,7 +90,6 @@ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= -github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=