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

Function TestDoClientRedirectRequestSameFullHost

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

Source from the content-addressed store, hash-verified

360}
361
362func TestDoClientRedirectRequestSameFullHost(t *testing.T) {
363 simpleServer := httptest.NewServer(nil)
364 simpleServer.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
365 w.Header().Set("x-custom-header", "key")
366 w.Header().Set("x-request-url", r.URL.String())
367 w.Header().Set("x-host", r.Host)
368
369 if r.URL.Path == "/before" {
370 w.Header().Set("Location", simpleServer.URL+"/after")
371 w.WriteHeader(302)
372 } else if r.URL.Path == "/after" {
373 w.WriteHeader(201)
374 }
375
376 body, err := httputil.DumpRequest(r, true)
377 assert.Nil(t, err)
378 w.Write(body)
379 })
380
381 var (
382 path = "/before"
383 client = NewHTTPClient(simpleServer.Listener.Addr().String(), false)
384 hostname = strings.SplitN(simpleServer.Listener.Addr().String(), ":", 2)[0]
385 port, _ = strconv.Atoi(strings.SplitN(simpleServer.Listener.Addr().String(), ":", 2)[1])
386 req = Request{Route: &Route{Path: []byte(path)}, Target: &Target{Hostname: hostname, Port: port}}
387 resp = Response{}
388 config = Config{
389 Timeout: 1 * time.Second,
390 ReadHeaders: true,
391 ReadBody: true,
392 MaxRedirects: 2,
393 }
394 )
395 req.Target.ParseHostHeader()
396 resp, err := DoClient(client, req, &config)
397 assert.Nil(t, err)
398
399 // First Request
400 resp = resp
401 // we expect the request uri to be empty
402 assert.Equal(t, "", string(resp.URI))
403 // it should be a redirect
404 assert.Equal(t, 302, resp.StatusCode)
405 // we also expect our headers and body to come back as expected
406 expected := []*Header{
407 {"X-Custom-Header", "key"},
408 {"X-Request-Url", "/before"},
409 }
410 for _, v := range expected {
411 assert.Contains(t, resp.Headers, v)
412 }
413 assert.NotEqual(t, "", string(resp.Body))
414
415 // redirected request
416 nresp := resp.Next
417 url := fmt.Sprintf("%s/after", simpleServer.URL)
418 // we expect the URI to contain the location header
419 assert.Equal(t, url, string(nresp.URI))

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