MCPcopy
hub / github.com/danielgtaylor/huma / TestHumaTestUtils

Function TestHumaTestUtils

humatest/humatest_test.go:55–104  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

53}
54
55func TestHumaTestUtils(t *testing.T) {
56 _, api := New(t)
57
58 huma.Register(api, huma.Operation{
59 OperationID: "test",
60 Method: http.MethodPut,
61 Path: "/test/{id}",
62 }, func(ctx context.Context, input *struct {
63 ID string `path:"id"`
64 Q string `query:"q"`
65 ContentType string `header:"Content-Type"`
66 Body struct {
67 Value string `json:"value"`
68 }
69 }) (*Response, error) {
70 assert.Equal(t, "abc123", input.ID)
71 assert.Equal(t, "foo", input.Q)
72 assert.Equal(t, "application/json", input.ContentType)
73 assert.Equal(t, "hello", input.Body.Value)
74 resp := &Response{}
75 resp.MyHeader = "my-value"
76 resp.Body.Echo = input.Body.Value
77 return resp, nil
78 })
79
80 w := api.Put("/test/abc123?q=foo",
81 "Content-Type: application/json",
82 "Host: example.com",
83 strings.NewReader(`{"value": "hello"}`))
84
85 assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
86 assert.Equal(t, "my-value", w.Header().Get("My-Header"))
87 assert.JSONEq(t, `{"echo":"hello"}`, w.Body.String())
88
89 // We can also serialize a slice/map/struct and the content type is set
90 // automatically for us.
91 w = api.Put("/test/abc123?q=foo",
92 map[string]any{"value": "hello"})
93
94 assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
95 assert.Equal(t, "my-value", w.Header().Get("My-Header"))
96 assert.JSONEq(t, `{"echo":"hello"}`, w.Body.String())
97
98 assert.Panics(t, func() {
99 // Cannot JSON encode a function.
100 api.Put("/test/abc123?q=foo",
101 "Content-Type: application/json",
102 map[string]any{"value": func() {}})
103 })
104}
105
106func TestContext(t *testing.T) {
107 op := &huma.Operation{}

Callers

nothing calls this directly

Calls 5

NewFunction · 0.70
PutMethod · 0.65
GetMethod · 0.65
HeaderMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…