(ctx context.Context, method, path string, args ...any)
| 206 | } |
| 207 | |
| 208 | func (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 | |
| 260 | func (a *testAPI) Get(path string, args ...any) *httptest.ResponseRecorder { |
| 261 | a.tb.Helper() |
no test coverage detected