(t *testing.T)
| 318 | } |
| 319 | |
| 320 | func TestFetchDisallowed(t *testing.T) { |
| 321 | // Start 2 test servers |
| 322 | srvDisAll := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 323 | if r.URL.Path == "/robots.txt" { |
| 324 | w.Write([]byte(` |
| 325 | User-agent: * |
| 326 | Disallow: / |
| 327 | `)) |
| 328 | return |
| 329 | } |
| 330 | w.Write([]byte("ok")) |
| 331 | })) |
| 332 | defer srvDisAll.Close() |
| 333 | srvAllSome := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 334 | if r.URL.Path == "/robots.txt" { |
| 335 | w.Write([]byte(` |
| 336 | User-agent: Googlebot |
| 337 | Disallow: / |
| 338 | |
| 339 | User-agent: Fetchbot |
| 340 | Disallow: /a |
| 341 | `)) |
| 342 | return |
| 343 | } |
| 344 | w.Write([]byte("ok")) |
| 345 | })) |
| 346 | defer srvAllSome.Close() |
| 347 | |
| 348 | // Define the raw URLs to enqueue |
| 349 | cases := []string{srvDisAll.URL + "/a", srvDisAll.URL + "/b", srvAllSome.URL + "/a", srvAllSome.URL + "/b"} |
| 350 | |
| 351 | // Start the Fetcher |
| 352 | sh := &spyHandler{} |
| 353 | f := New(sh) |
| 354 | f.CrawlDelay = 0 |
| 355 | q := f.Start() |
| 356 | for _, c := range cases { |
| 357 | _, err := q.SendString("GET", c) |
| 358 | if err != nil { |
| 359 | t.Fatal(err) |
| 360 | } |
| 361 | } |
| 362 | // Stop to wait for all commands to be processed |
| 363 | q.Close() |
| 364 | // Assert that the handler got called with the right values |
| 365 | if ok := sh.CalledWithExactly(cases...); !ok { |
| 366 | t.Error("expected handler to be called with all cases") |
| 367 | } |
| 368 | // Assert that there was the correct number of expected errors |
| 369 | if cnt := sh.Errors(); cnt != 3 { |
| 370 | t.Errorf("expected 3 errors, got %d", cnt) |
| 371 | } |
| 372 | for i := 0; i < 3; i++ { |
| 373 | if err := sh.ErrorFor(cases[i]); err != ErrDisallowed { |
| 374 | t.Errorf("expected error %s for %s, got %v", ErrDisallowed, cases[i], err) |
| 375 | } |
| 376 | } |
| 377 | } |
nothing calls this directly
no test coverage detected