@@ -123,68 +123,92 @@ func TestBindDataEmptyBody(t *testing.T) {
123123 r .ServeHTTP (w , c .Request )
124124}
125125
126- func TestUUIDFromString (t * testing.T ) {
126+ // TestBindDataJsonUnmarshalTypeError verifies that BindData returns the correct error on a type error
127+ func TestBindDataJsonUnmarshalTypeError (t * testing.T ) {
127128 w := httptest .NewRecorder ()
128129 c , r := gin .CreateTestContext (w )
129130
130131 r .GET ("/" , func (ctx * gin.Context ) {
131132 var o struct {
132- UUID string `form:"id "`
133+ Name string `json:"name "`
133134 }
134135
135- _ = c .Bind (& o )
136- _ , ok := httputil .UUIDFromStringHandleErrors (c , o .UUID )
137- if ! ok {
138- c .AbortWithStatus (http .StatusBadRequest )
139- }
140- c .Status (http .StatusOK )
136+ err := httputil .BindData (c , & o )
137+ assert .Equal (t , "json: cannot unmarshal number into Go struct field .name of type string" , err .Error ())
141138 })
142139
143- c .Request , _ = http .NewRequest (http .MethodGet , "https://example.com/?id=4e743e94-6a4b-44d6-aba5-d77c82103fa7 " , bytes .NewBuffer ([]byte ("" )))
140+ c .Request , _ = http .NewRequest (http .MethodGet , "https://example.com/" , bytes .NewBuffer ([]byte (`{ "name": 2 }` )))
144141 r .ServeHTTP (w , c .Request )
145- assert .Equal (t , http .StatusOK , w .Code )
146142}
147143
148- func TestUUIDFromStringInvalid (t * testing.T ) {
149- w := httptest .NewRecorder ()
150- c , r := gin .CreateTestContext (w )
151-
152- r .GET ("/" , func (ctx * gin.Context ) {
153- var o struct {
154- UUID string `form:"id"`
155- }
156-
157- _ = c .Bind (& o )
158- _ , ok := httputil .UUIDFromStringHandleErrors (c , o .UUID )
159- if ! ok {
160- c .AbortWithStatus (http .StatusBadRequest )
161- }
162- c .Status (http .StatusOK )
163- })
164-
165- c .Request , _ = http .NewRequest (http .MethodGet , "https://example.com/?id=not-a-valid-uuid" , bytes .NewBuffer ([]byte ("" )))
166- r .ServeHTTP (w , c .Request )
167- assert .Equal (t , http .StatusBadRequest , w .Code )
144+ func TestUUIDFromStringHandleErrors (t * testing.T ) {
145+ tests := []struct {
146+ name string
147+ url string
148+ status int // the expected http status
149+ }{
150+ {"Success" , "https://example.com/?id=4e743e94-6a4b-44d6-aba5-d77c82103fa7" , http .StatusOK },
151+ {"Invalid UUID" , "https://example.com/?id=not-a-valid-uuid" , http .StatusBadRequest },
152+ {"Empty UUID" , "https://example.com/?id=" , http .StatusOK },
153+ }
154+
155+ for _ , tt := range tests {
156+ t .Run (tt .name , func (t * testing.T ) {
157+ w := httptest .NewRecorder ()
158+ c , r := gin .CreateTestContext (w )
159+
160+ r .GET ("/" , func (ctx * gin.Context ) {
161+ var o struct {
162+ UUID string `form:"id"`
163+ }
164+
165+ _ = c .Bind (& o )
166+ _ , ok := httputil .UUIDFromStringHandleErrors (c , o .UUID )
167+ if ! ok {
168+ c .AbortWithStatus (http .StatusBadRequest )
169+ }
170+ c .Status (http .StatusOK )
171+ })
172+
173+ c .Request , _ = http .NewRequest (http .MethodGet , tt .url , bytes .NewBuffer ([]byte ("" )))
174+ r .ServeHTTP (w , c .Request )
175+ assert .Equal (t , tt .status , w .Code )
176+ })
177+ }
168178}
169179
170- func TestUUIDFromStringEmpty (t * testing.T ) {
171- w := httptest .NewRecorder ()
172- c , r := gin .CreateTestContext (w )
173-
174- r .GET ("/" , func (ctx * gin.Context ) {
175- var o struct {
176- UUID string `form:"id"`
177- }
178-
179- _ = c .Bind (& o )
180- _ , ok := httputil .UUIDFromStringHandleErrors (c , o .UUID )
181- if ! ok {
182- c .AbortWithStatus (http .StatusBadRequest )
183- }
184- c .Status (http .StatusOK )
185- })
186-
187- c .Request , _ = http .NewRequest (http .MethodGet , "https://example.com/?id=" , bytes .NewBuffer ([]byte ("" )))
188- r .ServeHTTP (w , c .Request )
189- assert .Equal (t , http .StatusOK , w .Code )
180+ func TestUUIDFromString (t * testing.T ) {
181+ tests := []struct {
182+ name string
183+ url string
184+ status int // the expected http status
185+ }{
186+ {"Success" , "https://example.com/?id=4e743e94-6a4b-44d6-aba5-d77c82103fa7" , http .StatusOK },
187+ {"Invalid UUID" , "https://example.com/?id=not-a-valid-uuid" , http .StatusBadRequest },
188+ {"Empty UUID" , "https://example.com/?id=" , http .StatusOK },
189+ }
190+
191+ for _ , tt := range tests {
192+ t .Run (tt .name , func (t * testing.T ) {
193+ w := httptest .NewRecorder ()
194+ c , r := gin .CreateTestContext (w )
195+
196+ r .GET ("/" , func (ctx * gin.Context ) {
197+ var o struct {
198+ UUID string `form:"id"`
199+ }
200+
201+ _ = c .Bind (& o )
202+ _ , err := httputil .UUIDFromString (o .UUID )
203+ if ! err .Nil () {
204+ c .AbortWithStatus (http .StatusBadRequest )
205+ }
206+ c .Status (http .StatusOK )
207+ })
208+
209+ c .Request , _ = http .NewRequest (http .MethodGet , tt .url , bytes .NewBuffer ([]byte ("" )))
210+ r .ServeHTTP (w , c .Request )
211+ assert .Equal (t , tt .status , w .Code )
212+ })
213+ }
190214}
0 commit comments