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

Function TestHandlerCmd

cmd_test.go:356–392  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

354}
355
356func TestHandlerCmd(t *testing.T) {
357 var result int32
358 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {}))
359 defer srv.Close()
360
361 cases := []struct {
362 cmd Command
363 want int32
364 }{
365 0: {
366 mustCmd(NewHandlerCmd("GET", srv.URL+"/a", func(ctx *Context, res *http.Response, err error) {
367 atomic.AddInt32(&result, 1)
368 })), 1,
369 },
370 1: {
371 &Cmd{U: mustParse(t, srv.URL+"/b"), M: "GET"}, -1,
372 },
373 }
374
375 f := New(HandlerFunc(func(ctx *Context, res *http.Response, err error) {
376 atomic.AddInt32(&result, -1)
377 }))
378 f.CrawlDelay = 0
379
380 for i, c := range cases {
381 result = 0
382 q := f.Start()
383 if err := q.Send(c.cmd); err != nil {
384 t.Errorf("%d: error sending command: %s", i, err)
385 }
386 q.Close()
387
388 if result != c.want {
389 t.Errorf("%d: want %d, got %d", i, c.want, result)
390 }
391 }
392}
393
394func mustCmd(cmd Command, err error) Command {
395 if err != nil {

Callers

nothing calls this directly

Calls 8

mustCmdFunction · 0.85
NewHandlerCmdFunction · 0.85
mustParseFunction · 0.85
NewFunction · 0.85
HandlerFuncFuncType · 0.85
CloseMethod · 0.80
StartMethod · 0.80
SendMethod · 0.80

Tested by

no test coverage detected