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

Function TestDoClientRedirectRequestMultiHost

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

Source from the content-addressed store, hash-verified

435}
436
437func TestDoClientRedirectRequestMultiHost(t *testing.T) {
438 afterServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
439 w.Header().Set("x-custom-header", "key2")
440 w.Header().Set("x-request-url", r.URL.String())
441 w.Header().Set("x-host", r.Host)
442 w.WriteHeader(201)
443
444 body, err := httputil.DumpRequest(r, true)
445 assert.Nil(t, err)
446 w.Write(body)
447 }))
448
449 simpleServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
450 w.Header().Set("x-custom-header", "key")
451 w.Header().Set("x-request-url", r.URL.String())
452 w.Header().Set("x-host", r.Host)
453 w.Header().Set("Location", afterServer.URL)
454 w.WriteHeader(302)
455
456 body, err := httputil.DumpRequest(r, true)
457 assert.Nil(t, err)
458 w.Write(body)
459 }))
460
461 var (
462 path = "/ignored"
463 client = NewHTTPClient(simpleServer.Listener.Addr().String(), false)
464 hostname = strings.SplitN(simpleServer.Listener.Addr().String(), ":", 2)[0]
465 port, _ = strconv.Atoi(strings.SplitN(simpleServer.Listener.Addr().String(), ":", 2)[1])
466 req = Request{Route: &Route{Path: []byte(path)}, Target: &Target{Hostname: hostname, Port: port}}
467 resp = Response{}
468 config = Config{
469 Timeout: 1 * time.Second,
470 ReadHeaders: true,
471 ReadBody: true,
472 MaxRedirects: 2,
473 }
474 )
475 req.Target.ParseHostHeader()
476 resp, err := DoClient(client, req, &config)
477 assert.Nil(t, err)
478
479 // First Request
480 resp = resp
481 // we expect the request uri to be empty
482 assert.Equal(t, "", string(resp.URI))
483 // it should be a redirect
484 assert.Equal(t, 302, resp.StatusCode)
485 // we also expect our headers and body to come back as expected
486 expected := []*Header{
487 {"X-Custom-Header", "key"},
488 {"X-Request-Url", "/ignored"},
489 }
490 for _, v := range expected {
491 assert.Contains(t, resp.Headers, v)
492 }
493 assert.NotEqual(t, "", string(resp.Body))
494

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