github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_request_xml_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package ghttp_test
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/wangyougui/gf/v2/frame/g"
    15  	"github.com/wangyougui/gf/v2/net/ghttp"
    16  	"github.com/wangyougui/gf/v2/test/gtest"
    17  	"github.com/wangyougui/gf/v2/util/guid"
    18  )
    19  
    20  func Test_Params_Xml_Request(t *testing.T) {
    21  	type User struct {
    22  		Id    int
    23  		Name  string
    24  		Time  *time.Time
    25  		Pass1 string `p:"password1"`
    26  		Pass2 string `p:"password2" v:"password2@required|length:2,20|password3|same:password1#||密码强度不足|两次密码不一致"`
    27  	}
    28  	s := g.Server(guid.S())
    29  	s.BindHandler("/get", func(r *ghttp.Request) {
    30  		r.Response.WriteExit(r.Get("id"), r.Get("name"))
    31  	})
    32  	s.BindHandler("/map", func(r *ghttp.Request) {
    33  		if m := r.GetMap(); len(m) > 0 {
    34  			r.Response.WriteExit(m["id"], m["name"], m["password1"], m["password2"])
    35  		}
    36  	})
    37  	s.BindHandler("/parse", func(r *ghttp.Request) {
    38  		if m := r.GetMap(); len(m) > 0 {
    39  			var user *User
    40  			if err := r.Parse(&user); err != nil {
    41  				r.Response.WriteExit(err)
    42  			}
    43  			r.Response.WriteExit(user.Id, user.Name, user.Pass1, user.Pass2)
    44  		}
    45  	})
    46  	s.SetDumpRouterMap(false)
    47  	s.Start()
    48  	defer s.Shutdown()
    49  
    50  	time.Sleep(100 * time.Millisecond)
    51  	gtest.C(t, func(t *gtest.T) {
    52  		client := g.Client()
    53  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    54  
    55  		content1 := `<doc><id>1</id><name>john</name><password1>123Abc!@#</password1><password2>123Abc!@#</password2></doc>`
    56  		content2 := `<doc><id>1</id><name>john</name><password1>123Abc!@#</password1><password2>123</password2></doc>`
    57  		t.Assert(client.GetContent(ctx, "/get", content1), ``)
    58  		t.Assert(client.PostContent(ctx, "/get", content1), `1john`)
    59  		t.Assert(client.GetContent(ctx, "/map", content1), ``)
    60  		t.Assert(client.PostContent(ctx, "/map", content1), `1john123Abc!@#123Abc!@#`)
    61  		t.Assert(client.PostContent(ctx, "/parse", content1), `1john123Abc!@#123Abc!@#`)
    62  		t.Assert(client.PostContent(ctx, "/parse", content2), `密码强度不足`)
    63  	})
    64  }