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

Method DoCtx

humatest/humatest.go:208–258  ·  view source on GitHub ↗
(ctx context.Context, method, path string, args ...any)

Source from the content-addressed store, hash-verified

206}
207
208func (a *testAPI) DoCtx(ctx context.Context, method, path string, args ...any) *httptest.ResponseRecorder {
209 a.tb.Helper()
210 var b io.Reader
211 isJSON := false
212 for _, arg := range args {
213 kind := reflect.Indirect(reflect.ValueOf(arg)).Kind()
214 if reader, ok := arg.(io.Reader); ok {
215 b = reader
216 break
217 } else if _, ok := arg.(string); ok {
218 // do nothing
219 } else if kind == reflect.Struct || kind == reflect.Map || kind == reflect.Slice {
220 encoded, err := json.Marshal(arg)
221 if err != nil {
222 panic(err)
223 }
224 b = bytes.NewReader(encoded)
225 isJSON = true
226 } else {
227 panic("unsupported argument type, expected string header or io.Reader/slice/map/struct body")
228 }
229 }
230
231 req, _ := http.NewRequestWithContext(ctx, method, path, b)
232 req.RequestURI = path
233 req.RemoteAddr = "127.0.0.1:12345"
234 if isJSON {
235 req.Header.Set("Content-Type", "application/json")
236 }
237 for _, arg := range args {
238 if s, ok := arg.(string); ok {
239 parts := strings.Split(s, ":")
240 req.Header.Set(parts[0], strings.TrimSpace(strings.Join(parts[1:], ":")))
241
242 if strings.ToLower(parts[0]) == "host" {
243 req.Host = strings.TrimSpace(parts[1])
244 }
245 }
246 }
247 resp := httptest.NewRecorder()
248
249 bytes, _ := DumpRequest(req)
250 a.tb.Log("Making request:\n" + strings.TrimSpace(string(bytes)))
251
252 a.Adapter().ServeHTTP(resp, req)
253
254 bytes, _ = DumpResponse(resp.Result())
255 a.tb.Log("Got response:\n" + strings.TrimSpace(string(bytes)))
256
257 return resp
258}
259
260func (a *testAPI) Get(path string, args ...any) *httptest.ResponseRecorder {
261 a.tb.Helper()

Callers 6

DoMethod · 0.95
GetCtxMethod · 0.95
PostCtxMethod · 0.95
PutCtxMethod · 0.95
PatchCtxMethod · 0.95
DeleteCtxMethod · 0.95

Calls 7

DumpRequestFunction · 0.85
DumpResponseFunction · 0.85
HelperMethod · 0.80
LogMethod · 0.80
MarshalMethod · 0.65
ServeHTTPMethod · 0.65
AdapterMethod · 0.65

Tested by

no test coverage detected