MCPcopy Create free account
hub / github.com/PuerkitoBio/fetchbot / TestCustomCommand

Function TestCustomCommand

fetch_test.go:481–522  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

479}
480
481func 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
524func TestFreeIdleHost(t *testing.T) {
525 // Start 2 test servers

Callers

nothing calls this directly

Calls 7

CalledWithExactlyMethod · 0.95
ErrorsMethod · 0.95
CommandForMethod · 0.95
NewFunction · 0.85
CloseMethod · 0.80
StartMethod · 0.80
SendMethod · 0.80

Tested by

no test coverage detected