| 119 | } |
| 120 | |
| 121 | func TestBody(t *testing.T) { |
| 122 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 123 | cooks := req.Cookies() |
| 124 | if len(cooks) == 0 { |
| 125 | b, err := ioutil.ReadAll(req.Body) |
| 126 | if err != nil { |
| 127 | w.WriteHeader(http.StatusInternalServerError) |
| 128 | w.Write([]byte(err.Error())) |
| 129 | return |
| 130 | } |
| 131 | w.Write(b) |
| 132 | } else { |
| 133 | for i, c := range cooks { |
| 134 | if i > 0 { |
| 135 | w.Write([]byte{'&'}) |
| 136 | } |
| 137 | w.Write([]byte(c.Name)) |
| 138 | } |
| 139 | } |
| 140 | })) |
| 141 | defer srv.Close() |
| 142 | cases := []struct { |
| 143 | cmd Command |
| 144 | body string |
| 145 | }{ |
| 146 | 0: { |
| 147 | &readerCmd{&Cmd{U: mustParse(t, srv.URL+"/a"), M: "POST"}, |
| 148 | strings.NewReader("a")}, |
| 149 | "a", |
| 150 | }, |
| 151 | 1: { |
| 152 | &valuesCmd{&Cmd{U: mustParse(t, srv.URL+"/b"), M: "POST"}, |
| 153 | url.Values{"k": {"v"}}}, |
| 154 | "k=v", |
| 155 | }, |
| 156 | 2: { |
| 157 | &Cmd{U: mustParse(t, srv.URL+"/c"), M: "POST"}, |
| 158 | "", |
| 159 | }, |
| 160 | 3: { |
| 161 | &basicAuthCmd{&Cmd{U: mustParse(t, srv.URL+"/d"), M: "POST"}, "me", "you"}, |
| 162 | "", |
| 163 | }, |
| 164 | 4: { |
| 165 | &cookiesCmd{&Cmd{U: mustParse(t, srv.URL+"/e"), M: "GET"}, |
| 166 | []*http.Cookie{&http.Cookie{Name: "e"}}}, |
| 167 | "e", |
| 168 | }, |
| 169 | 5: { |
| 170 | &cookiesCmd{&Cmd{U: mustParse(t, srv.URL+"/f"), M: "GET"}, |
| 171 | []*http.Cookie{&http.Cookie{Name: "f1"}, &http.Cookie{Name: "f2"}}}, |
| 172 | "f1&f2", |
| 173 | }, |
| 174 | } |
| 175 | sh := &spyHandler{} |
| 176 | f := New(sh) |
| 177 | f.CrawlDelay = 0 |
| 178 | q := f.Start() |