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