From 65e5d680b143ed0940635fd6906db636c6dd0bb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=B8=9C=E9=BE=99?= Date: Mon, 25 Apr 2022 15:10:41 +0800 Subject: [PATCH] fix bind test errors (#1950) * test(transport): fix assertion condition error * test(transport): it need a unparsed request --- transport/http/binding/bind_test.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/transport/http/binding/bind_test.go b/transport/http/binding/bind_test.go index dae7faa8d..5e2552f90 100644 --- a/transport/http/binding/bind_test.go +++ b/transport/http/binding/bind_test.go @@ -1,9 +1,11 @@ package binding import ( + "io" "net/http" "net/url" "reflect" + "strings" "testing" ) @@ -30,7 +32,7 @@ func TestBindQuery(t *testing.T) { target: &p1, }, wantErr: false, - want: TestBind{"kratos", "https://go-kratos.dev/"}, + want: &TestBind{"kratos", "https://go-kratos.dev/"}, }, } for _, tt := range tests { @@ -38,7 +40,7 @@ func TestBindQuery(t *testing.T) { if err := BindQuery(tt.args.vars, tt.args.target); (err != nil) != tt.wantErr { t.Errorf("BindQuery() error = %v, wantErr %v", err, tt.wantErr) } - if reflect.DeepEqual(tt.args.target, tt.want) { + if !tt.wantErr && !reflect.DeepEqual(tt.args.target, tt.want) { t.Errorf("BindQuery() target = %v, want %v", tt.args.target, tt.want) } }) @@ -73,11 +75,15 @@ func TestBindForm(t *testing.T) { { name: "error is nil", args: args{ - req: &http.Request{Form: map[string][]string{"name": {"kratos"}, "url": {"https://go-kratos.dev/"}}}, + req: &http.Request{ + Method: "POST", + Header: http.Header{"Content-Type": {"application/x-www-form-urlencoded; param=value"}}, + Body: io.NopCloser(strings.NewReader("name=kratos&url=https://go-kratos.dev/")), + }, target: &p1, }, wantErr: false, - want: TestBind{"kratos", "https://go-kratos.dev/"}, + want: &TestBind{"kratos", "https://go-kratos.dev/"}, }, } for _, tt := range tests { @@ -86,8 +92,8 @@ func TestBindForm(t *testing.T) { if (err != nil) != tt.wantErr { t.Errorf("BindForm() error = %v, wantErr %v", err, tt.wantErr) } - if err != nil && reflect.DeepEqual(tt.args.target, tt.want) { - t.Errorf("BindQuery() target = %v, want %v", tt.args.target, tt.want) + if !tt.wantErr && !reflect.DeepEqual(tt.args.target, tt.want) { + t.Errorf("BindForm() target = %v, want %v", tt.args.target, tt.want) } }) }