MCPcopy Index your code
hub / github.com/danielgtaylor/huma / TestNonJSONValidation

Function TestNonJSONValidation

huma_test.go:4017–4054  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

4015}
4016
4017func TestNonJSONValidation(t *testing.T) {
4018 // Test that validation works when only non-JSON content type is specified.
4019 // This tests the fix for supporting validation schemas from non-JSON content types.
4020 type CBORInput struct {
4021 Body struct {
4022 Name string `json:"name" minLength:"2" doc:"User name"`
4023 } `contentType:"application/cbor"`
4024 }
4025
4026 // Use default config which includes CBOR via the import above.
4027 _, api := humatest.New(t, huma.DefaultConfig("Test", "1.0.0"))
4028
4029 huma.Post(api, "/cbor-test", func(ctx context.Context, input *CBORInput) (*struct{}, error) {
4030 return &struct{}{}, nil
4031 })
4032
4033 // Check that CBOR is in the OpenAPI spec (not JSON).
4034 op := api.OpenAPI().Paths["/cbor-test"].Post
4035 assert.NotNil(t, op.RequestBody)
4036 assert.Contains(t, op.RequestBody.Content, "application/cbor")
4037 assert.NotContains(t, op.RequestBody.Content, "application/json")
4038
4039 // Marshal valid data as CBOR.
4040 var validBuf bytes.Buffer
4041 huma.DefaultFormats["application/cbor"].Marshal(&validBuf, map[string]any{"name": "John"})
4042
4043 // Valid CBOR request should work (name >= 2 chars).
4044 w := api.Post("/cbor-test", "Content-Type: application/cbor", &validBuf)
4045 assert.Equal(t, 204, w.Code)
4046
4047 // Marshal invalid data as CBOR (name < 2 chars).
4048 var invalidBuf bytes.Buffer
4049 huma.DefaultFormats["application/cbor"].Marshal(&invalidBuf, map[string]any{"name": "J"})
4050
4051 // Invalid CBOR request should fail validation.
4052 w = api.Post("/cbor-test", "Content-Type: application/cbor", &invalidBuf)
4053 assert.Equal(t, 422, w.Code)
4054}
4055
4056func TestFieldsOptionalByDefault(t *testing.T) {
4057 type MyInput struct {

Callers

nothing calls this directly

Calls 4

NewFunction · 0.92
PostMethod · 0.65
OpenAPIMethod · 0.65
MarshalMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…