(t *testing.T)
| 420 | } |
| 421 | |
| 422 | func TestCurl(t *testing.T) { |
| 423 | reqTestCases := map[string]struct { |
| 424 | curlReq CurlRequest |
| 425 | tr *recordingTransport |
| 426 | expReqBody string |
| 427 | }{ |
| 428 | "Required only": {CurlRequest{Target: "example.com"}, &recordingTransport{}, `{"target":"example.com","head":false}`}, |
| 429 | "With head": {CurlRequest{Target: "example.com", Head: true}, &recordingTransport{}, `{"target":"example.com","head":true}`}, |
| 430 | "With insecure": {CurlRequest{Target: "example.com", Insecure: true}, &recordingTransport{}, `{"target":"example.com","head":false,"insecure":true}`}, |
| 431 | "With HTTP2": {CurlRequest{Target: "example.com", HTTP2: true}, &recordingTransport{}, `{"target":"example.com","head":false,"http2":true}`}, |
| 432 | "With location": {CurlRequest{Target: "example.com", Location: "Asia"}, &recordingTransport{}, `{"target":"example.com","head":false,"location":"Asia"}`}, |
| 433 | } |
| 434 | ctx := context.Background() |
| 435 | for name, tc := range reqTestCases { |
| 436 | t.Run(name, func(t *testing.T) { |
| 437 | c, err := newTestClient(tc.tr) |
| 438 | if err != nil { |
| 439 | t.Fatalf("unexpected error %v", err) |
| 440 | } |
| 441 | c.Run.Curl(ctx, &tc.curlReq) |
| 442 | if got := reqBody(tc.tr.req); tc.expReqBody != "" && tc.expReqBody != got { |
| 443 | t.Fatalf("expected %v; got %v", tc.expReqBody, got) |
| 444 | } |
| 445 | }) |
| 446 | } |
| 447 | |
| 448 | testCases := map[string]struct { |
| 449 | target string |
| 450 | head bool |
| 451 | insecure bool |
| 452 | http2 bool |
| 453 | limit int |
| 454 | testID string |
| 455 | tr *respondingTransport |
| 456 | err error |
| 457 | }{ |
| 458 | "Invalid target": {"meep", true, true, false, 1, "", &respondingTransport{resp: dummyResp(201, "POST", `{"id": "135"}`)}, &argError{"target"}}, |
| 459 | "Invalid limit": {"example.com", true, true, false, freeMaxNodeCap + 1, "", &respondingTransport{resp: dummyResp(201, "POST", `{"id": "135"}`)}, &argError{"limit"}}, |
| 460 | "HTTP error": {"example.com", true, true, false, 1, "", &respondingTransport{resp: dummyResp(400, "POST", `{"Error": "an error"}`)}, errors.New(`400: {"Error": "an error"}`)}, |
| 461 | "Failed": {"example.com", true, true, false, 1, "", &respondingTransport{resp: dummyResp(201, "POST", `{"Error": "an error"}`)}, errors.New("an error")}, |
| 462 | "Created": {"example.com", true, true, false, 1, "0123456789abcdefghij", &respondingTransport{resp: dummyResp(201, "POST", `{"id": "0123456789abcdefghij"}`)}, nil}, |
| 463 | } |
| 464 | for name, tc := range testCases { |
| 465 | t.Run(name, func(t *testing.T) { |
| 466 | c, err := newTestClient(tc.tr) |
| 467 | if err != nil { |
| 468 | t.Fatalf("unexpected error %v", err) |
| 469 | } |
| 470 | got, err := c.Run.Curl(ctx, &CurlRequest{Target: tc.target, Head: tc.head, Insecure: tc.insecure, HTTP2: tc.http2, Limit: tc.limit}) |
| 471 | if !cmpError(err, tc.err) { |
| 472 | t.Fatalf("expected %v; got %v", tc.err, err) |
| 473 | } |
| 474 | if string(got) != tc.testID { |
| 475 | t.Fatalf("expected %v; got %v", tc.testID, got) |
| 476 | } |
| 477 | }) |
| 478 | } |
| 479 | } |
nothing calls this directly
no test coverage detected