fix bind test errors (#1950)

* test(transport): fix assertion condition error

* test(transport): it need a unparsed request
pull/1951/head
张东龙 3 years ago committed by GitHub
parent 4472cdbf49
commit 259cd84653
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      transport/http/binding/bind_test.go

@ -1,9 +1,11 @@
package binding package binding
import ( import (
"io"
"net/http" "net/http"
"net/url" "net/url"
"reflect" "reflect"
"strings"
"testing" "testing"
) )
@ -30,7 +32,7 @@ func TestBindQuery(t *testing.T) {
target: &p1, target: &p1,
}, },
wantErr: false, wantErr: false,
want: TestBind{"kratos", "https://go-kratos.dev/"}, want: &TestBind{"kratos", "https://go-kratos.dev/"},
}, },
} }
for _, tt := range tests { 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 { if err := BindQuery(tt.args.vars, tt.args.target); (err != nil) != tt.wantErr {
t.Errorf("BindQuery() error = %v, wantErr %v", err, 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) 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", name: "error is nil",
args: args{ 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, target: &p1,
}, },
wantErr: false, wantErr: false,
want: TestBind{"kratos", "https://go-kratos.dev/"}, want: &TestBind{"kratos", "https://go-kratos.dev/"},
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -86,8 +92,8 @@ func TestBindForm(t *testing.T) {
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("BindForm() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("BindForm() error = %v, wantErr %v", err, tt.wantErr)
} }
if err != nil && 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) t.Errorf("BindForm() target = %v, want %v", tt.args.target, tt.want)
} }
}) })
} }

Loading…
Cancel
Save