Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

add DefaultBinder negative-path tests (multipart & non-struct) #2844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking "Sign up for GitHub", you agree to our terms of service and privacy statement. We'll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
miladev95 wants to merge 1 commit into labstack:master
base: master
Choose a base branch
Loading
from miladev95:test/bind-negative-multipart-nonstruct
Open

add DefaultBinder negative-path tests (multipart & non-struct) #2844

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions bind_test.go
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,7 @@ func TestTimeFormatBinding(t *testing.T) {
DateTimeLocal time.Time `form:"datetime_local" format:"2006-01-02T15:04"`
Date time.Time `query:"date" format:"2006-01-02"`
CustomFormat time.Time `form:"custom" format:"01/02/2006 15:04:05"`
DefaultTime time.Time `form:"default_time"` // No format tag - should use default parsing
DefaultTime time.Time `form:"default_time"` // No format tag - should use default parsing
PtrTime *time.Time `query:"ptr_time" format:"2006-01-02"`
}

Expand Down Expand Up @@ -1623,7 +1623,7 @@ func TestTimeFormatBinding(t *testing.T) {
{
name: "nok, wrong format should fail",
contentType: MIMEApplicationForm,
data: "datetime_local=2023-12-25", // Missing time part
data: "datetime_local=2023-12-25", // Missing time part
expectError: true,
},
}
Expand Down Expand Up @@ -1684,3 +1684,40 @@ func TestTimeFormatBinding(t *testing.T) {
})
}
}

func TestIsFieldMultipartFile_NonPointer_FileHeader_ReturnsError( t *testing.T) {
ok, err := isFieldMultipartFile(reflect.TypeOf(multipart.FileHeader{}))
assert.True(t, ok)
assert.Error(t, err)
assert.Contains(t, err.Error(), "binding to multipart.FileHeader struct is not supported")

// pointer type should be ok and no error
ok, err = isFieldMultipartFile(reflect.TypeOf((*multipart.FileHeader)(nil)))
assert.True(t, ok)
assert.NoError(t, err)
}

func TestSetMultipartFileHeaderTypes_NoFiles_ReturnsFalse(t *testing.T) {
var s struct {
Files []multipart.FileHeader
}
v := reflect.ValueOf(&s).Elem().Field(0)
ok := setMultipartFileHeaderTypes(v, "files", map[string][]*multipart.FileHeader{})
assert.False(t, ok)
}

func TestDefaultBinder_bindData_NonStruct_ReturnsError(t *testing.T) {
b := &DefaultBinder{}
// destination is pointer to slice (non-struct), tag is form -> should return error
err := b.bindData(&[]int{}, map[string][]string{"a": {"1"}}, "form", nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "binding element must be a struct")

// for param/query/header tag should return nil (handled earlier)
err = b.bindData(&[]int{}, map[string][]string{"a": {"1"}}, "param", nil)
assert.NoError(t, err)

// also header
err = b.bindData(&[]int{}, map[string][]string{"a": {"1"}}, "header", nil)
assert.NoError(t, err)
}