mirror of
https://github.com/guonaihong/gout
synced 2025-12-24 12:58:00 +08:00
新增NoAutoContentType接口 (#265)
This commit is contained in:
@@ -367,6 +367,17 @@ func (df *DataFlow) Debug(d ...interface{}) *DataFlow {
|
||||
return df
|
||||
}
|
||||
|
||||
// https://github.com/guonaihong/gout/issues/264
|
||||
// When calling SetWWWForm(), the Content-Type header will be added automatically,
|
||||
// and calling NoAutoContentType() will not add an HTTP header
|
||||
//
|
||||
// SetWWWForm "Content-Type", "application/x-www-form-urlencoded"
|
||||
// SetJSON "Content-Type", "application/json"
|
||||
func (df *DataFlow) NoAutoContentType() *DataFlow {
|
||||
df.Req.noAutoContentType = true
|
||||
return df
|
||||
}
|
||||
|
||||
func (df *DataFlow) IsDebug() bool {
|
||||
return df.out.opt.Debug
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dataflow
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -107,3 +108,69 @@ func TestSetFormStruct(t *testing.T) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetForm_NoAutoContentType(t *testing.T) {
|
||||
needValue := "x-www-form-urlencoded; charset=UTF-8"
|
||||
needValueDefault := "x-www-form-urlencoded"
|
||||
router := func() *gin.Engine {
|
||||
router := gin.New()
|
||||
router.POST("/test.form", func(c *gin.Context) {
|
||||
|
||||
type testFormHeader struct {
|
||||
ContentType string `header:"content-Type"`
|
||||
}
|
||||
|
||||
var header testFormHeader
|
||||
c.ShouldBindHeader(&header)
|
||||
if header.ContentType == needValue {
|
||||
c.String(200, "ok")
|
||||
} else {
|
||||
c.String(500, "fail")
|
||||
}
|
||||
})
|
||||
router.POST("/test.form/default", func(c *gin.Context) {
|
||||
|
||||
type testFormHeader struct {
|
||||
ContentType string `header:"content-Type"`
|
||||
}
|
||||
|
||||
var header testFormHeader
|
||||
c.ShouldBindHeader(&header)
|
||||
if header.ContentType == needValueDefault {
|
||||
c.String(200, "ok")
|
||||
} else {
|
||||
c.String(500, "fail")
|
||||
}
|
||||
})
|
||||
return router
|
||||
}()
|
||||
|
||||
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
|
||||
defer ts.Close()
|
||||
|
||||
code := 0
|
||||
err := New().POST(ts.URL + "/test.form").
|
||||
NoAutoContentType().
|
||||
SetHeader(core.A{"content-type", needValue}).
|
||||
SetWWWForm(
|
||||
core.A{
|
||||
"elapsed_time", 0,
|
||||
"user_choice_index", -1,
|
||||
"ts", 1608191572029,
|
||||
},
|
||||
).Code(&code).Do()
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, code, 200)
|
||||
|
||||
code = 0
|
||||
err = New().POST(ts.URL + "/test.form/default").
|
||||
SetHeader(core.A{"content-type", needValue}).
|
||||
SetWWWForm(
|
||||
core.A{
|
||||
"elapsed_time", 0,
|
||||
"user_choice_index", -1,
|
||||
"ts", 1608191572029,
|
||||
},
|
||||
).Code(&code).Do()
|
||||
}
|
||||
|
||||
@@ -60,8 +60,9 @@ type Req struct {
|
||||
c context.Context
|
||||
Err error
|
||||
|
||||
reqModify []api.RequestMiddler
|
||||
req *http.Request
|
||||
reqModify []api.RequestMiddler
|
||||
req *http.Request
|
||||
noAutoContentType bool
|
||||
}
|
||||
|
||||
// Reset 重置 Req结构体
|
||||
@@ -71,6 +72,7 @@ type Req struct {
|
||||
// 有没有必要,归一化成一种??? TODO:
|
||||
func (r *Req) Reset() {
|
||||
r.index = 0
|
||||
r.noAutoContentType = false
|
||||
r.Err = nil
|
||||
r.cookies = nil
|
||||
r.form = nil
|
||||
@@ -311,7 +313,9 @@ func (r *Req) Request() (req *http.Request, err error) {
|
||||
}
|
||||
|
||||
r.addDefDebug()
|
||||
r.addContextType(req)
|
||||
if !r.noAutoContentType {
|
||||
r.addContextType(req)
|
||||
}
|
||||
//运行请求中间件
|
||||
for _, reqModify := range r.reqModify {
|
||||
if err = reqModify.ModifyRequest(req); err != nil {
|
||||
|
||||
6
go.mod
6
go.mod
@@ -1,10 +1,10 @@
|
||||
module github.com/guonaihong/gout
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.5.0
|
||||
github.com/gin-gonic/gin v1.6.0
|
||||
github.com/google/uuid v1.1.1
|
||||
github.com/mattn/go-isatty v0.0.9
|
||||
github.com/mattn/go-isatty v0.0.12
|
||||
github.com/stretchr/testify v1.4.0
|
||||
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa
|
||||
gopkg.in/yaml.v2 v2.2.2
|
||||
gopkg.in/yaml.v2 v2.2.8
|
||||
)
|
||||
|
||||
19
go.sum
19
go.sum
@@ -6,21 +6,35 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc=
|
||||
github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do=
|
||||
github.com/gin-gonic/gin v1.6.0 h1:Lb3veSYoGaNck69fV2+Vf2juLSsHpMTf3Vk5+X+EDJg=
|
||||
github.com/gin-gonic/gin v1.6.0/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
|
||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc=
|
||||
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
|
||||
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
|
||||
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
|
||||
github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM=
|
||||
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
|
||||
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
|
||||
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
|
||||
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
|
||||
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
|
||||
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
|
||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
|
||||
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8=
|
||||
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
|
||||
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
|
||||
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
||||
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
|
||||
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
||||
@@ -42,7 +56,10 @@ golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLL
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
|
||||
@@ -51,3 +68,5 @@ gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvR
|
||||
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
||||
@@ -46,6 +46,7 @@ func TestDebug(t *testing.T) {
|
||||
assert.Equal(t, fmt.Sprintf("%d test debug.", k), s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
c := &http.Client{}
|
||||
tests := []*dataflow.Gout{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gout
|
||||
|
||||
// Version show version
|
||||
const Version = "v0.1.3"
|
||||
const Version = "v0.1.4"
|
||||
|
||||
Reference in New Issue
Block a user