From 36f6c493d431dbe4ddad3ab179c44257e7307f26 Mon Sep 17 00:00:00 2001 From: guonaihong Date: Mon, 15 Jul 2019 22:58:16 +0800 Subject: [PATCH] add test function --- binding/binding_test.go | 14 ++++++++++++++ binding/form_mapping.go | 3 ++- binding/form_mapping_test.go | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/binding/binding_test.go b/binding/binding_test.go index 806f3ac9..cfafb83a 100644 --- a/binding/binding_test.go +++ b/binding/binding_test.go @@ -1177,6 +1177,20 @@ func testBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, bad assert.Error(t, err) } +type Identifier struct { + Assigner *Reference `json:"assigner,omitempty"` +} + +type Reference struct { + Identifier *Identifier `json:"identifier,omitempty"` +} + +func TestLoopReference(t *testing.T) { + r := Reference{} + err := mapForm(&r, map[string][]string{}) + assert.Error(t, err) +} + func testProtoBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) { assert.Equal(t, name, b.Name()) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index c4543f9a..48db75c2 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -18,6 +18,7 @@ import ( var LimitMappingCallNumber = 10000 var errUnknownType = errors.New("Unknown type") +var ErrMaybeCircularReference = errors.New("Maybe entering a circular reference") func mapUri(ptr interface{}, m map[string][]string) error { return mapFormByTag(ptr, m, "uri") @@ -73,7 +74,7 @@ func mapping(value reflect.Value, field reflect.StructField, setter setter, tag } if callNumber > LimitMappingCallNumber { - return false, errors.New("Maybe entering a circular reference") + return false, ErrMaybeCircularReference } if vKind != reflect.Struct || !field.Anonymous { diff --git a/binding/form_mapping_test.go b/binding/form_mapping_test.go index c9d6111b..04145c21 100644 --- a/binding/form_mapping_test.go +++ b/binding/form_mapping_test.go @@ -52,7 +52,7 @@ func TestMappingBaseTypes(t *testing.T) { field := val.Elem().Type().Field(0) - _, err := mapping(val, emptyField, formSource{field.Name: {tt.form}}, "form") + _, err := mapping(val, emptyField, formSource{field.Name: {tt.form}}, "form", 0) assert.NoError(t, err, testName) actual := val.Elem().Field(0).Interface()