Skip to content

Commit

Permalink
fix:395 (#396)
Browse files Browse the repository at this point in the history
* fix:395

* 修改ci/cd

* 修改最低版本
  • Loading branch information
guonaihong authored Jan 7, 2025
1 parent 404fba6 commit 63f56c3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 11 additions & 4 deletions dataflow/dataflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"net/http"
"net/url"
"strings"
"text/template"
"time"

"github.com/guonaihong/gout/debug"
Expand Down Expand Up @@ -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
}

Expand Down
26 changes: 26 additions & 0 deletions dataflow/dataflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -216,6 +217,7 @@ func setupForm(t *testing.T, reqTestForm testForm) *gin.Engine {
assert.Equal(t, reqTestForm.Text, t2.Text)
*/
})

return router
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/guonaihong/gout

go 1.17
go 1.18

require (
github.com/andybalholm/brotli v1.0.4
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 63f56c3

Please sign in to comment.