Skip to content

Commit

Permalink
fixbug (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong authored Jan 30, 2023
1 parent 13ff215 commit 78be976
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 12 deletions.
12 changes: 7 additions & 5 deletions dataflow/req_url_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,26 @@ func Test_URL_Template(t *testing.T) {
} {
body := ""
body2 := ""
code := 0
switch tc.Method {
case "get":
New().GET("{{.Host}}/{{.Method}}", tc).BindBody(&body).Do()
New().GET("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do()
case "post":
New().POST("{{.Host}}/{{.Method}}", tc).BindBody(&body).Do()
New().POST("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do()
case "put":
New().PUT("{{.Host}}/{{.Method}}", tc).BindBody(&body).Do()
New().PUT("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do()
case "patch":
New().PATCH("{{.Host}}/{{.Method}}", tc).BindBody(&body).Do()
New().PATCH("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do()
case "options":
New().OPTIONS("{{.Host}}/{{.Method}}", tc).BindBody(&body).Do()
New().OPTIONS("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do()
case "head":
code := 0
New().HEAD("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do()
New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Code(&code).Do()
assert.Equal(t, code, 200)
continue
}
assert.Equal(t, code, 200)

New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Do()
assert.Equal(t, body, tc.Method)
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.18
go 1.17

require (
github.com/andybalholm/brotli v1.0.4
Expand Down
12 changes: 6 additions & 6 deletions gout.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,32 @@ func GET(url string, urlStruct ...interface{}) *dataflow.DataFlow {

// POST send HTTP POST method
func POST(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().POST(url, urlStruct)
return dataflow.New().POST(url, urlStruct...)
}

// PUT send HTTP PUT method
func PUT(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().PUT(url, urlStruct)
return dataflow.New().PUT(url, urlStruct...)
}

// DELETE send HTTP DELETE method
func DELETE(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().DELETE(url, urlStruct)
return dataflow.New().DELETE(url, urlStruct...)
}

// PATCH send HTTP PATCH method
func PATCH(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().PATCH(url, urlStruct)
return dataflow.New().PATCH(url, urlStruct...)
}

// HEAD send HTTP HEAD method
func HEAD(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().HEAD(url, urlStruct)
return dataflow.New().HEAD(url, urlStruct...)
}

// OPTIONS send HTTP OPTIONS method
func OPTIONS(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().OPTIONS(url, urlStruct)
return dataflow.New().OPTIONS(url, urlStruct...)
}

// 设置不忽略空值
Expand Down
93 changes: 93 additions & 0 deletions req_url_template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package gout

import (
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)

type testURLTemplateCase struct {
Host string
Method string
}

func createMethodEcho() *httptest.Server {
router := func() *gin.Engine {
router := gin.New()

router.GET("/get", func(c *gin.Context) {
c.String(200, "get")
})

router.POST("/post", func(c *gin.Context) {
c.String(200, "post")
})

router.PUT("/put", func(c *gin.Context) {
c.String(200, "put")
})

router.PATCH("/patch", func(c *gin.Context) {
c.String(200, "patch")
})

router.OPTIONS("/options", func(c *gin.Context) {
c.String(200, "options")
})

router.HEAD("/head", func(c *gin.Context) {
c.String(200, "head")
})

return router
}()

return httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
}

func Test_URL_Template(t *testing.T) {
ts := createMethodEcho()
for _, tc := range []testURLTemplateCase{
{Host: ts.URL, Method: "get"},
{Host: ts.URL, Method: "post"},
{Host: ts.URL, Method: "put"},
{Host: ts.URL, Method: "patch"},
{Host: ts.URL, Method: "options"},
{Host: ts.URL, Method: "head"},
} {
body := ""
body2 := ""
code := 0
switch tc.Method {
case "get":
GET("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do()
case "post":
POST("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do()
case "put":
PUT("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do()
case "patch":
PATCH("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do()
case "options":
OPTIONS("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do()
case "head":
code := 0
HEAD("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do()
New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Code(&code).Do()
assert.Equal(t, code, 200)
continue
}
assert.Equal(t, code, 200)

New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Do()
assert.Equal(t, body, tc.Method)
b := assert.Equal(t, body2, tc.Method)
if !b {
return
}
}

}

0 comments on commit 78be976

Please sign in to comment.