Skip to content

Commit

Permalink
优化下测试代码.把每种可能都测试下 (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong authored Apr 22, 2021
1 parent b5f3cb2 commit aaf9565
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions dataflow/dataflow_form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,33 +111,22 @@ func TestSetFormStruct(t *testing.T) {
}

func TestSetForm_NoAutoContentType(t *testing.T) {
needValue := "x-www-form-urlencoded; charset=UTF-8"
needValueDefault := "x-www-form-urlencoded"
needContentType := "x-www-form-urlencoded; charset=UTF-8"
contentTypeDefault := "application/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"`
NeedValue string `header:"needValue"`
}

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 {
assert.Equal(t, header.ContentType, header.NeedValue)
if header.ContentType == header.NeedValue {
c.String(200, "ok")
} else {
c.String(500, "fail")
Expand All @@ -149,10 +138,26 @@ func TestSetForm_NoAutoContentType(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
defer ts.Close()

// 1.告知gout,不加默认的content-type, 然后使用SetHeader 设置的contentType
code := 0
err := New().POST(ts.URL + "/test.form").
NoAutoContentType().
SetHeader(core.A{"content-type", needValue}).
SetHeader(core.A{"content-type", needContentType, "needValue", needContentType}).
SetWWWForm(
core.A{
"elapsed_time", 0,
"user_choice_index", -1,
"ts", 1608191572029,
},
).Code(&code).Do()

assert.NoError(t, err)
assert.Equal(t, code, 200)

// 2.告知gout,不加默认的content-type, 然后不设置新的contentType,该字段应该为空
err = New().POST(ts.URL + "/test.form").
NoAutoContentType().
//SetHeader(core.A{"content-type", needContentType}).
SetWWWForm(
core.A{
"elapsed_time", 0,
Expand All @@ -164,9 +169,10 @@ func TestSetForm_NoAutoContentType(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, code, 200)

// 3.默认形为
code = 0
err = New().POST(ts.URL + "/test.form/default").
SetHeader(core.A{"content-type", needValue}).
err = New().POST(ts.URL + "/test.form").
SetHeader(core.A{"needValue", contentTypeDefault}).
SetWWWForm(
core.A{
"elapsed_time", 0,
Expand All @@ -176,5 +182,23 @@ func TestSetForm_NoAutoContentType(t *testing.T) {
).Code(&code).Do()

assert.NoError(t, err)
assert.Equal(t, code, 500)
assert.Equal(t, code, 200)

// 4.直接设置contentType
// 这里会有两个content-Type, 有的服务器可能会报错,尽量避免这种情况发生.
// 如果要自定义content-Type, 先使用NoAutoContentType,取消自动加的content-Type,
// 然后再通过SetHeader添加,可看第1种测试情况
code = 0
err = New().POST(ts.URL + "/test.form").
SetHeader(core.A{"needValue", needContentType, "content-type", needContentType}).
SetWWWForm(
core.A{
"elapsed_time", 0,
"user_choice_index", -1,
"ts", 1608191572029,
},
).Code(&code).Do()

assert.NoError(t, err)
assert.Equal(t, code, 200)
}

0 comments on commit aaf9565

Please sign in to comment.