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

Function TestFetchDisallowed

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

Source from the content-addressed store, hash-verified

318}
319
320func 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(`
325User-agent: *
326Disallow: /
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(`
336User-agent: Googlebot
337Disallow: /
338
339User-agent: Fetchbot
340Disallow: /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}

Callers

nothing calls this directly

Calls 7

CalledWithExactlyMethod · 0.95
ErrorsMethod · 0.95
ErrorForMethod · 0.95
NewFunction · 0.85
CloseMethod · 0.80
StartMethod · 0.80
SendStringMethod · 0.80

Tested by

no test coverage detected