(t *testing.T)
| 360 | } |
| 361 | |
| 362 | func 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)) |
nothing calls this directly
no test coverage detected