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

Function TestHeader

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

Source from the content-addressed store, hash-verified

207}
208
209func TestHeader(t *testing.T) {
210 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
211 // Write headers in lexical order so that result is predictable
212 keys := make([]string, 0, len(req.Header))
213 for k := range req.Header {
214 if len(k) == 1 {
215 keys = append(keys, k)
216 }
217 }
218 sort.Strings(keys)
219 for _, k := range keys {
220 w.Write([]byte(fmt.Sprintf("%s:%s\n", k, req.Header[k][0])))
221 }
222 }))
223 defer srv.Close()
224 cases := []struct {
225 cmd Command
226 body string
227 }{
228 0: {
229 &headerCmd{&Cmd{U: mustParse(t, srv.URL+"/a"), M: "GET"},
230 http.Header{"A": {"a"}}},
231 "A:a\n",
232 },
233 1: {
234 &Cmd{U: mustParse(t, srv.URL+"/b"), M: "GET"},
235 "",
236 },
237 2: {
238 &headerCmd{&Cmd{U: mustParse(t, srv.URL+"/c"), M: "GET"},
239 http.Header{"C": {"c"}, "D": {"d"}}},
240 "C:c\nD:d\n",
241 },
242 }
243 sh := &spyHandler{}
244 f := New(sh)
245 f.CrawlDelay = 0
246 q := f.Start()
247 for i, c := range cases {
248 if err := q.Send(c.cmd); err != nil {
249 t.Errorf("%d: error sending command: %s", i, err)
250 }
251 }
252 q.Close()
253 var urls []string
254 for i, c := range cases {
255 urls = append(urls, c.cmd.URL().String())
256 if b := sh.BodyFor(c.cmd.URL().String()); b != c.body {
257 t.Errorf("%d: expected body '%s', got '%s'", i, c.body, b)
258 }
259 }
260 if !sh.CalledWithExactly(urls...) {
261 t.Error("expected handler to be called for all cases")
262 }
263 if cnt := sh.Errors(); cnt > 0 {
264 t.Errorf("expected no error, got %d", cnt)
265 }
266}

Callers

nothing calls this directly

Calls 9

BodyForMethod · 0.95
CalledWithExactlyMethod · 0.95
ErrorsMethod · 0.95
mustParseFunction · 0.85
NewFunction · 0.85
CloseMethod · 0.80
StartMethod · 0.80
SendMethod · 0.80
URLMethod · 0.65

Tested by

no test coverage detected