Files
godoxy-yusing/internal/serialization/gin_binding_test.go
yusing 1579f490c0 refactor: replace gperr with standard errors package and simplify string parsing
- Replace gperr.Error return types with standard error across test files
- Replace gperr.New with errors.New in validation and serialization tests
- Update API documentation in README files to use error instead of gperr.Error
- Simplify string parsing using strings.Cut in docker/label.go
- Update benchmarks to use NewTestEntrypoint and remove task package dependency
2026-02-10 16:59:19 +08:00

51 lines
1.1 KiB
Go

package serialization_test
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/yusing/godoxy/internal/serialization"
)
type TestStruct struct {
Value string `json:"value"`
Value2 int `json:"value2"`
}
func (t *TestStruct) Validate() error {
if t.Value == "" {
return errors.New("value is required")
}
if t.Value2 != 0 && (t.Value2 < 5 || t.Value2 > 10) {
return errors.New("value2 must be between 5 and 10")
}
return nil
}
func TestGinBinding(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
}{
{"valid1", `{"value": "test", "value2": 7}`, false},
{"valid2", `{"value": "test"}`, false},
{"invalid1", `{"value2": 7}`, true},
{"invalid2", `{"value": "test", "value2": 3}`, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var dst TestStruct
body := bytes.NewBufferString(tt.input)
req := httptest.NewRequest(http.MethodPost, "/", body)
err := serialization.GinJSONBinding{}.Bind(req, &dst)
if (err != nil) != tt.wantErr {
t.Errorf("%s: Bind() error = %v, wantErr %v", tt.name, err, tt.wantErr)
}
})
}
}