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

Function TestBody

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

Source from the content-addressed store, hash-verified

119}
120
121func 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()

Callers

nothing calls this directly

Calls 10

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

Tested by

no test coverage detected