| 295 | } |
| 296 | |
| 297 | func TestFullCmd(t *testing.T) { |
| 298 | creds := base64.StdEncoding.EncodeToString([]byte("me:you")) |
| 299 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 300 | // Basic auth |
| 301 | auth := req.Header.Get("Authorization") |
| 302 | if auth != "Basic "+creds { |
| 303 | w.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"") |
| 304 | http.Error(w, "Unauthorized", http.StatusUnauthorized) |
| 305 | return |
| 306 | } |
| 307 | // Cookies |
| 308 | for i, c := range req.Cookies() { |
| 309 | if i > 0 { |
| 310 | w.Write([]byte{'&'}) |
| 311 | } |
| 312 | w.Write([]byte(c.Name)) |
| 313 | } |
| 314 | // Header |
| 315 | for k, v := range req.Header { |
| 316 | if len(k) == 1 { |
| 317 | w.Write([]byte(fmt.Sprintf("%s:%s\n", k, v[0]))) |
| 318 | } |
| 319 | } |
| 320 | // Body |
| 321 | b, err := ioutil.ReadAll(req.Body) |
| 322 | if err != nil { |
| 323 | t.Fatal(err) |
| 324 | } |
| 325 | w.Write(b) |
| 326 | })) |
| 327 | defer srv.Close() |
| 328 | |
| 329 | sh := &spyHandler{} |
| 330 | f := New(sh) |
| 331 | f.CrawlDelay = 0 |
| 332 | q := f.Start() |
| 333 | cmd := &fullCmd{ |
| 334 | &Cmd{U: mustParse(t, srv.URL+"/a"), M: "POST"}, |
| 335 | "me", "you", |
| 336 | strings.NewReader("body"), |
| 337 | url.Values{"ignored": {"val"}}, |
| 338 | []*http.Cookie{&http.Cookie{Name: "a"}}, |
| 339 | http.Header{"A": {"a"}}, |
| 340 | } |
| 341 | if err := q.Send(cmd); err != nil { |
| 342 | t.Fatal(err) |
| 343 | } |
| 344 | q.Close() |
| 345 | // Assert 200 status |
| 346 | if st := sh.StatusFor(cmd.URL().String()); st != 200 { |
| 347 | t.Errorf("expected status %d, got %d", 200, st) |
| 348 | } |
| 349 | // Assert body (Cookies + Header) |
| 350 | exp := "aA:a\nbody" |
| 351 | if b := sh.BodyFor(cmd.URL().String()); b != exp { |
| 352 | t.Errorf("expected body '%s', got '%s'", exp, b) |
| 353 | } |
| 354 | } |