|
|
|
@ -1,6 +1,7 @@ |
|
|
|
|
package binding |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"errors" |
|
|
|
|
"io" |
|
|
|
|
"net/http" |
|
|
|
|
"net/url" |
|
|
|
@ -8,47 +9,47 @@ import ( |
|
|
|
|
"strings" |
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
"github.com/go-kratos/kratos/v2/errors" |
|
|
|
|
kratoserror "github.com/go-kratos/kratos/v2/errors" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func TestBindQuery(t *testing.T) { |
|
|
|
|
type TestBind struct { |
|
|
|
|
type ( |
|
|
|
|
TestBind struct { |
|
|
|
|
Name string `json:"name"` |
|
|
|
|
URL string `json:"url"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type TestBind2 struct { |
|
|
|
|
TestBind2 struct { |
|
|
|
|
Age int `json:"age"` |
|
|
|
|
} |
|
|
|
|
p1 := TestBind{} |
|
|
|
|
p2 := TestBind2{} |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func TestBindQuery(t *testing.T) { |
|
|
|
|
type args struct { |
|
|
|
|
vars url.Values |
|
|
|
|
target interface{} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tests := []struct { |
|
|
|
|
name string |
|
|
|
|
args args |
|
|
|
|
wantErr bool |
|
|
|
|
err error |
|
|
|
|
want interface{} |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "test", |
|
|
|
|
args: args{ |
|
|
|
|
vars: map[string][]string{"name": {"kratos"}, "url": {"https://go-kratos.dev/"}}, |
|
|
|
|
target: &p1, |
|
|
|
|
target: &TestBind{}, |
|
|
|
|
}, |
|
|
|
|
wantErr: false, |
|
|
|
|
err: nil, |
|
|
|
|
want: &TestBind{"kratos", "https://go-kratos.dev/"}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "test1", |
|
|
|
|
args: args{ |
|
|
|
|
vars: map[string][]string{"age": {"kratos"}, "url": {"https://go-kratos.dev/"}}, |
|
|
|
|
target: &p2, |
|
|
|
|
target: &TestBind2{}, |
|
|
|
|
}, |
|
|
|
|
wantErr: true, |
|
|
|
|
want: errors.BadRequest("CODEC", ""), |
|
|
|
|
err: kratoserror.BadRequest("CODEC", "Field Namespace:age ERROR:Invalid Integer Value 'kratos' Type 'int' Namespace 'age'"), |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "test2", |
|
|
|
@ -56,18 +57,17 @@ func TestBindQuery(t *testing.T) { |
|
|
|
|
vars: map[string][]string{"age": {"1"}, "url": {"https://go-kratos.dev/"}}, |
|
|
|
|
target: &TestBind2{}, |
|
|
|
|
}, |
|
|
|
|
wantErr: false, |
|
|
|
|
err: nil, |
|
|
|
|
want: &TestBind2{Age: 1}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
|
t.Run(tt.name, func(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) |
|
|
|
|
} else { |
|
|
|
|
t.Log(err) |
|
|
|
|
err := BindQuery(tt.args.vars, tt.args.target) |
|
|
|
|
if !kratoserror.Is(err, tt.err) { |
|
|
|
|
t.Fatalf("BindQuery() error = %v, err %v", err, tt.err) |
|
|
|
|
} |
|
|
|
|
if !tt.wantErr && !reflect.DeepEqual(tt.args.target, tt.want) { |
|
|
|
|
if err == nil && !reflect.DeepEqual(tt.args.target, tt.want) { |
|
|
|
|
t.Errorf("BindQuery() target = %v, want %v", tt.args.target, tt.want) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
@ -75,31 +75,24 @@ func TestBindQuery(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestBindForm(t *testing.T) { |
|
|
|
|
type TestBind struct { |
|
|
|
|
Name string `json:"name"` |
|
|
|
|
URL string `json:"url"` |
|
|
|
|
} |
|
|
|
|
type TestBind2 struct { |
|
|
|
|
Age int `json:"age"` |
|
|
|
|
} |
|
|
|
|
p1 := TestBind{} |
|
|
|
|
type args struct { |
|
|
|
|
req *http.Request |
|
|
|
|
target interface{} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tests := []struct { |
|
|
|
|
name string |
|
|
|
|
args args |
|
|
|
|
wantErr bool |
|
|
|
|
want interface{} |
|
|
|
|
err error |
|
|
|
|
want *TestBind |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "error not nil", |
|
|
|
|
args: args{ |
|
|
|
|
req: &http.Request{Method: http.MethodPost}, |
|
|
|
|
target: &p1, |
|
|
|
|
target: &TestBind{}, |
|
|
|
|
}, |
|
|
|
|
wantErr: true, |
|
|
|
|
err: errors.New("missing form body"), |
|
|
|
|
want: nil, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
@ -110,9 +103,9 @@ func TestBindForm(t *testing.T) { |
|
|
|
|
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: &TestBind{}, |
|
|
|
|
}, |
|
|
|
|
wantErr: false, |
|
|
|
|
err: nil, |
|
|
|
|
want: &TestBind{"kratos", "https://go-kratos.dev/"}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
@ -125,18 +118,17 @@ func TestBindForm(t *testing.T) { |
|
|
|
|
}, |
|
|
|
|
target: &TestBind2{}, |
|
|
|
|
}, |
|
|
|
|
wantErr: true, |
|
|
|
|
err: kratoserror.BadRequest("CODEC", "Field Namespace:age ERROR:Invalid Integer Value 'a' Type 'int' Namespace 'age'"), |
|
|
|
|
want: nil, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
|
|
|
err := BindForm(tt.args.req, tt.args.target) |
|
|
|
|
if (err != nil) != tt.wantErr { |
|
|
|
|
t.Errorf("BindForm() error = %v, wantErr %v", err, tt.wantErr) |
|
|
|
|
if !reflect.DeepEqual(err, tt.err) { |
|
|
|
|
t.Fatalf("BindForm() error = %v, err %v", err, tt.err) |
|
|
|
|
} |
|
|
|
|
t.Log(err) |
|
|
|
|
if !tt.wantErr && !reflect.DeepEqual(tt.args.target, tt.want) { |
|
|
|
|
if err == nil && !reflect.DeepEqual(tt.args.target, tt.want) { |
|
|
|
|
t.Errorf("BindForm() target = %v, want %v", tt.args.target, tt.want) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|