MCPcopy
hub / github.com/assetnote/kiterunner / TestDoClientSimpleRequest

Function TestDoClientSimpleRequest

pkg/http/client_test.go:243–286  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

241}
242
243func TestDoClientSimpleRequest(t *testing.T) {
244 simpleServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
245 w.Header().Set("x-custom-header", "key")
246 w.Header().Set("x-request-url", r.URL.String())
247 w.Header().Set("x-host", r.Host)
248
249 w.WriteHeader(201)
250 body, err := httputil.DumpRequest(r, true)
251 assert.Nil(t, err)
252 w.Write(body)
253 }))
254
255 var (
256 path = "/foo"
257 client = NewHTTPClient(simpleServer.Listener.Addr().String(), false)
258 hostname = strings.SplitN(simpleServer.Listener.Addr().String(), ":", 2)[0]
259 port, _ = strconv.Atoi(strings.SplitN(simpleServer.Listener.Addr().String(), ":", 2)[1])
260 req = Request{Route: &Route{Path: []byte(path)}, Target: &Target{Hostname: hostname, Port: port}}
261 resp = Response{}
262 config = Config{
263 Timeout: 1 * time.Second,
264 ReadHeaders: true,
265 ReadBody: true,
266 }
267 )
268 req.Target.ParseHostHeader()
269 resp, err := DoClient(client, req, &config)
270 assert.Nil(t, err)
271
272 // we expect the request uri for the first request to be empty
273 _ = fmt.Sprintf("%s%s", simpleServer.URL, path)
274 assert.Equal(t, "", string(resp.URI))
275
276 // we also expect our headers and body to come back as expected
277 expected := []*Header{
278 {"X-Custom-Header", "key"},
279 {"X-Request-Url", "/foo"},
280 }
281 for _, v := range expected {
282 assert.Contains(t, resp.Headers, v)
283 }
284
285 assert.NotEqual(t, "", string(resp.Body))
286}
287
288func TestDoClientRedirectRequest(t *testing.T) {
289 simpleServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Callers

nothing calls this directly

Calls 6

NewHTTPClientFunction · 0.85
DoClientFunction · 0.85
ParseHostHeaderMethod · 0.80
EqualMethod · 0.65
StringMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected