mirror of
				https://github.com/labstack/echo.git
				synced 2025-10-30 23:57:38 +02:00 
			
		
		
		
	
				
					committed by
					
						 Vishal Rana
						Vishal Rana
					
				
			
			
				
	
			
			
			
						parent
						
							647c0b4ec6
						
					
				
				
					commit
					08f7147508
				
			
							
								
								
									
										14
									
								
								binder.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								binder.go
									
									
									
									
									
								
							| @@ -21,6 +21,12 @@ type ( | ||||
|  | ||||
| func (b *binder) Bind(i interface{}, c Context) (err error) { | ||||
| 	req := c.Request() | ||||
| 	if req.Method() == GET { | ||||
| 		if err = b.bindData(i, c.QueryParams()); err != nil { | ||||
| 			err = NewHTTPError(http.StatusBadRequest, err.Error()) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
| 	ctype := req.Header().Get(HeaderContentType) | ||||
| 	if req.Body() == nil { | ||||
| 		err = NewHTTPError(http.StatusBadRequest, "request body can't be empty") | ||||
| @@ -37,14 +43,14 @@ func (b *binder) Bind(i interface{}, c Context) (err error) { | ||||
| 			err = NewHTTPError(http.StatusBadRequest, err.Error()) | ||||
| 		} | ||||
| 	case strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm): | ||||
| 		if err = b.bindForm(i, req.FormParams()); err != nil { | ||||
| 		if err = b.bindData(i, req.FormParams()); err != nil { | ||||
| 			err = NewHTTPError(http.StatusBadRequest, err.Error()) | ||||
| 		} | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func (b *binder) bindForm(ptr interface{}, form map[string][]string) error { | ||||
| func (b *binder) bindData(ptr interface{}, data map[string][]string) error { | ||||
| 	typ := reflect.TypeOf(ptr).Elem() | ||||
| 	val := reflect.ValueOf(ptr).Elem() | ||||
|  | ||||
| @@ -65,14 +71,14 @@ func (b *binder) bindForm(ptr interface{}, form map[string][]string) error { | ||||
| 			inputFieldName = typeField.Name | ||||
| 			// If "form" tag is nil, we inspect if the field is a struct. | ||||
| 			if structFieldKind == reflect.Struct { | ||||
| 				err := b.bindForm(structField.Addr().Interface(), form) | ||||
| 				err := b.bindData(structField.Addr().Interface(), data) | ||||
| 				if err != nil { | ||||
| 					return err | ||||
| 				} | ||||
| 				continue | ||||
| 			} | ||||
| 		} | ||||
| 		inputValue, exists := form[inputFieldName] | ||||
| 		inputValue, exists := data[inputFieldName] | ||||
| 		if !exists { | ||||
| 			continue | ||||
| 		} | ||||
|   | ||||
| @@ -68,6 +68,7 @@ func TestBinderXML(t *testing.T) { | ||||
|  | ||||
| func TestBinderForm(t *testing.T) { | ||||
| 	testBinderOkay(t, strings.NewReader(userForm), MIMEApplicationForm) | ||||
| 	testBinderError(t, nil, MIMEApplicationForm) | ||||
| 	e := New() | ||||
| 	req := test.NewRequest(POST, "/", strings.NewReader(userForm)) | ||||
| 	rec := test.NewResponseRecorder() | ||||
| @@ -78,6 +79,19 @@ func TestBinderForm(t *testing.T) { | ||||
| 	assert.Error(t, err) | ||||
| } | ||||
|  | ||||
| func TestBinderQueryParams(t *testing.T) { | ||||
| 	e := New() | ||||
| 	req := test.NewRequest(GET, "/?id=1&name=Jon Snow", nil) | ||||
| 	rec := test.NewResponseRecorder() | ||||
| 	c := e.NewContext(req, rec) | ||||
| 	u := new(user) | ||||
| 	err := c.Bind(u) | ||||
| 	if assert.NoError(t, err) { | ||||
| 		assert.Equal(t, 1, u.ID) | ||||
| 		assert.Equal(t, "Jon Snow", u.Name) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestBinderMultipartForm(t *testing.T) { | ||||
| 	body := new(bytes.Buffer) | ||||
| 	mw := multipart.NewWriter(body) | ||||
| @@ -99,7 +113,7 @@ func TestBinderUnsupportedMediaType(t *testing.T) { | ||||
| func TestBinderbindForm(t *testing.T) { | ||||
| 	ts := new(binderTestStruct) | ||||
| 	b := new(binder) | ||||
| 	b.bindForm(ts, values) | ||||
| 	b.bindData(ts, values) | ||||
| 	assertBinderTestStruct(t, ts) | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user