Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/mayask/sprout. A type-safe HTTP router for Go that provides automatic validation and parameter bind
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294package sprout
import ( "context" "encoding/json" "fmt" "net/http" "net/http/httptest" "reflect" "strconv" "strings" "testing")
type typeValidationStatus string
const ( typeValidationStatusActive typeValidationStatus = "active" typeValidationStatusInactive typeValidationStatus = "inactive")
func registerTypeValidationStatus(router *Sprout) { router.RegisterTypeValidationFunc(func(value reflect.Value) error { if value.Type() != reflect.TypeOf(typeValidationStatus("")) { return nil } if value.String() == "" { return nil } switch typeValidationStatus(value.String()) { case typeValidationStatusActive, typeValidationStatusInactive: return nil default: return fmt.Errorf("must be a known status") } })}
func TestTypeValidationFuncValidatesRequestWithoutValidationTag(t *testing.T) { router := New() registerTypeValidationStatus(router)
type request struct { Status typeValidationStatus `json:"status" validate:"required"` Optional typeValidationStatus `json:"optional,omitempty"` Items []typeValidationStatus `json:"items"` } type response struct { Status typeValidationStatus `json:"status"` }
called := false POST(router, "/statuses", func(ctx context.Context, req *request) (*response, error) { called = true return &response{Status: req.Status}, nil })
recorder := httptest.NewRecorder() router.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/statuses", strings.NewReader(`{"status":"active","items":["inactive"]}`))) if recorder.Code != http.StatusOK { t.Fatalf("expected status 200, got %d with body %s", recorder.Code, recorder.Body.String()) } if !called { t.Fatalf("expected handler to be called") }
called = false recorder = httptest.NewRecorder() router.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/statuses", strings.NewReader(`{"status":"bogus"}`))) if recorder.Code != http.StatusBadRequest { t.Fatalf("expected status 400, got %d with body %s", recorder.Code, recorder.Body.String()) } if called { t.Fatalf("expected handler not to be called for invalid enum") }}
func TestTypeValidationFuncIsSharedWithMountedRouters(t *testing.T) { router := New() registerTypeValidationStatus(router) mounted := router.Mount("/api", nil)
type request struct { Status typeValidationStatus `query:"status" validate:"required"` } type response struct { Status typeValidationStatus `json:"status"` }
called := false GET(mounted, "/statuses", func(ctx context.Context, req *request) (*response, error) { called = true return &response{Status: req.Status}, nil })
recorder := httptest.NewRecorder() router.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/statuses?status=bogus", nil)) if recorder.Code != http.StatusBadRequest { t.Fatalf("expected status 400, got %d with body %s", recorder.Code, recorder.Body.String()) } if called { t.Fatalf("expected mounted handler not to be called for invalid enum") }}
func TestTypeValidationFuncValidatesResponseWithoutValidationTag(t *testing.T) { router := New() registerTypeValidationStatus(router)
type request struct{} type response struct { Status typeValidationStatus `json:"status"` }
GET(router, "/statuses", func(ctx context.Context, req *request) (*response, error) { return &response{Status: typeValidationStatus("bogus")}, nil })
recorder := httptest.NewRecorder() router.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/statuses", nil)) if recorder.Code != http.StatusInternalServerError { t.Fatalf("expected status 500, got %d with body %s", recorder.Code, recorder.Body.String()) }}
type exampleAmount struct { value string}
func (a *exampleAmount) UnmarshalJSON(data []byte) error { var raw string if err := json.Unmarshal(data, &raw); err != nil { return err } a.value = raw return nil}
type exampleCurrency string
type exampleFXRate struct { value string}
func (r *exampleFXRate) UnmarshalJSON(data []byte) error { var raw string if err := json.Unmarshal(data, &raw); err != nil { return err } r.value = raw return nil}
type exampleCountry string
type exampleJurisdiction struct { kind string value string}
func (j *exampleJurisdiction) UnmarshalJSON(data []byte) error { var raw string if err := json.Unmarshal(data, &raw); err != nil { return err } if strings.HasPrefix(raw, "SPECIAL:") { j.kind = "special" j.value = strings.TrimPrefix(raw, "SPECIAL:") return nil } j.kind = "country" j.value = raw return nil}
func registerExampleCustomTypeValidators(router *Sprout) { router.RegisterTypeValidationFunc(func(value reflect.Value) error { switch value.Type() { case reflect.TypeOf(exampleAmount{}): amount := value.Interface().(exampleAmount) if amount.value == "" { return nil } parsed, err := strconv.ParseFloat(amount.value, 64) if err != nil || parsed < 0 { return fmt.Errorf("amount must be non-negative") } case reflect.TypeOf(exampleCurrency("")): currency := value.String() if currency == "" { return nil } switch currency { case "GBP", "EUR", "USD": return nil default: return fmt.Errorf("currency must be supported") } case reflect.TypeOf(exampleFXRate{}): rate := value.Interface().(exampleFXRate) if rate.value == "" { return nil } parsed, err := strconv.ParseFloat(rate.value, 64) if err != nil || parsed <= 0 { return fmt.Errorf("fx rate must be positive") } case reflect.TypeOf(exampleCountry("")): country := value.String() if country == "" { return nil } switch country { case "GB", "NL", "US": return nil default: return fmt.Errorf("country must be supported") } case reflect.TypeOf(exampleJurisdiction{}): jurisdiction := value.Interface().(exampleJurisdiction) if jurisdiction.value == "" { return nil } switch jurisdiction.kind { case "country": switch jurisdiction.value { case "GB", "NL", "US": return nil } case "special": switch jurisdiction.value { case "KOSOVO", "DARFUR": return nil } } return fmt.Errorf("jurisdiction must be supported") } return nil })}
func TestTypeValidationFuncHandlesCommonCustomTypeShapes(t *testing.T) { router := New() registerExampleCustomTypeValidators(router)
type request struct { Amount exampleAmount `json:"amount"` Currency exampleCurrency `json:"currency"` FXRate *exampleFXRate `json:"fx_rate"` Countries []exampleCountry `json:"countries"` Jurisdictions map[string]exampleJurisdiction `json:"jurisdictions"` } type response struct { Currency exampleCurrency `json:"currency"` }
called := false POST(router, "/custom-types", func(ctx context.Context, req *request) (*response, error) { called = true return &response{Currency: req.Currency}, nil })
recorder := httptest.NewRecorder() router.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/custom-types", strings.NewReader(`{ "amount":"10.50", "currency":"GBP", "fx_rate":"1.25", "countries":["GB","NL"], "jurisdictions":{"registered":"GB","special":"SPECIAL:KOSOVO"} }`))) if recorder.Code != http.StatusOK { t.Fatalf("expected status 200, got %d with body %s", recorder.Code, recorder.Body.String()) } if !called { t.Fatalf("expected handler to be called") }
called = false recorder = httptest.NewRecorder() router.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/custom-types", strings.NewReader(`{ "amount":"10.50", "currency":"GBP", "fx_rate":"0", "countries":["GB","ZZ"], "jurisdictions":{"unknown":"SPECIAL:UNKNOWN"} }`))) if recorder.Code != http.StatusBadRequest { t.Fatalf("expected status 400, got %d with body %s", recorder.Code, recorder.Body.String()) } if called { t.Fatalf("expected handler not to be called for invalid custom types") }}