(t *testing.T)
| 479 | } |
| 480 | |
| 481 | func TestCustomCommand(t *testing.T) { |
| 482 | // Start a test server |
| 483 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 484 | w.Write([]byte("ok")) |
| 485 | })) |
| 486 | defer srv.Close() |
| 487 | |
| 488 | // Define the raw URLs to enqueue |
| 489 | cases := []string{srv.URL + "/a", srv.URL + "/b"} |
| 490 | |
| 491 | // Start the Fetcher |
| 492 | sh := &spyHandler{} |
| 493 | f := New(sh) |
| 494 | f.CrawlDelay = 0 |
| 495 | q := f.Start() |
| 496 | for i, c := range cases { |
| 497 | parsed, err := url.Parse(c) |
| 498 | if err != nil { |
| 499 | t.Fatal(err) |
| 500 | } |
| 501 | q.Send(&IDCmd{&Cmd{U: parsed, M: "GET"}, i}) |
| 502 | } |
| 503 | // Stop to wait for all commands to be processed |
| 504 | q.Close() |
| 505 | // Assert that the handler got called with the right values |
| 506 | if ok := sh.CalledWithExactly(cases...); !ok { |
| 507 | t.Error("expected handler to be called with all cases") |
| 508 | } |
| 509 | // Assert that there was no error |
| 510 | if cnt := sh.Errors(); cnt > 0 { |
| 511 | t.Errorf("expected no errors, got %d", cnt) |
| 512 | } |
| 513 | // Assert that all commands got passed with the correct custom information |
| 514 | for i, c := range cases { |
| 515 | cmd := sh.CommandFor(c) |
| 516 | if idc, ok := cmd.(*IDCmd); !ok { |
| 517 | t.Errorf("expected command for %s to be an *IDCmd, got %T", c, cmd) |
| 518 | } else if idc.ID != i { |
| 519 | t.Errorf("expected command ID for %s to be %d, got %d", c, i, idc.ID) |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | func TestFreeIdleHost(t *testing.T) { |
| 525 | // Start 2 test servers |
nothing calls this directly
no test coverage detected