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

Function TestDoClientRedirectRequest

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

Source from the content-addressed store, hash-verified

286}
287
288func TestDoClientRedirectRequest(t *testing.T) {
289 simpleServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
290 w.Header().Set("x-custom-header", "key")
291 w.Header().Set("x-request-url", r.URL.String())
292 w.Header().Set("x-host", r.Host)
293
294 if r.URL.Path == "/before" {
295 w.Header().Set("Location", "/after")
296 w.WriteHeader(302)
297 } else if r.URL.Path == "/after" {
298 w.WriteHeader(201)
299 }
300
301 body, err := httputil.DumpRequest(r, true)
302 assert.Nil(t, err)
303 w.Write(body)
304 }))
305
306 var (
307 path = "/before"
308 client = NewHTTPClient(simpleServer.Listener.Addr().String(), false)
309 hostname = strings.SplitN(simpleServer.Listener.Addr().String(), ":", 2)[0]
310 port, _ = strconv.Atoi(strings.SplitN(simpleServer.Listener.Addr().String(), ":", 2)[1])
311 req = Request{Route: &Route{Path: []byte(path)}, Target: &Target{Hostname: hostname, Port: port}}
312 resp = Response{}
313 config = Config{
314 Timeout: 1 * time.Second,
315 ReadHeaders: true,
316 ReadBody: true,
317 MaxRedirects: 2,
318 }
319 )
320 req.Target.ParseHostHeader()
321 resp, err := DoClient(client, req, &config)
322 assert.Nil(t, err)
323
324 // First Request
325 resp = resp
326 // we expect the request uri to be empty
327 assert.Equal(t, "", string(resp.URI))
328 // it should be a redirect
329 assert.Equal(t, 302, resp.StatusCode)
330 // we also expect our headers and body to come back as expected
331 expected := []*Header{
332 {"X-Custom-Header", "key"},
333 {"X-Request-Url", "/before"},
334 }
335 for _, v := range expected {
336 assert.Contains(t, resp.Headers, v)
337 }
338 assert.NotEqual(t, "", string(resp.Body))
339
340 // redirected request
341 nresp := resp.Next
342 // url := fmt.Sprintf("%s/after", simpleServer.URL)
343 // we expect the URI to contain the location header
344 assert.Equal(t, "/after", string(nresp.URI))
345 // it should be a redirect

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