| 383 | } |
| 384 | |
| 385 | func TestPathRedirect(t *testing.T) { |
| 386 | routes := "route add mock / http://a.com/$path opts \"redirect=301\"\n" |
| 387 | routes += "route add mock /foo http://a.com/abc opts \"redirect=301\"\n" |
| 388 | routes += "route add mock /bar http://b.com/$path opts \"redirect=302 strip=/bar\"\n" |
| 389 | tbl, _ := route.NewTable(bytes.NewBufferString(routes)) |
| 390 | |
| 391 | proxy := httptest.NewServer(&HTTPProxy{ |
| 392 | ProtectHeaders: testProtectHeaders, |
| 393 | Transport: http.DefaultTransport, |
| 394 | Lookup: func(r *http.Request) *route.Target { |
| 395 | return tbl.Lookup(r, route.Picker["rr"], route.Matcher["prefix"], globCache, globEnabled) |
| 396 | }, |
| 397 | }) |
| 398 | defer proxy.Close() |
| 399 | |
| 400 | tests := []struct { |
| 401 | req string |
| 402 | wantCode int |
| 403 | wantLoc string |
| 404 | }{ |
| 405 | {req: "/", wantCode: 301, wantLoc: "http://a.com/"}, |
| 406 | {req: "/aaa/bbb", wantCode: 301, wantLoc: "http://a.com/aaa/bbb"}, |
| 407 | {req: "/foo", wantCode: 301, wantLoc: "http://a.com/abc"}, |
| 408 | {req: "/bar", wantCode: 302, wantLoc: "http://b.com/"}, |
| 409 | {req: "/bar/aaa", wantCode: 302, wantLoc: "http://b.com/aaa"}, |
| 410 | } |
| 411 | |
| 412 | http.DefaultClient.CheckRedirect = func(req *http.Request, via []*http.Request) error { |
| 413 | // do not follow redirects |
| 414 | return http.ErrUseLastResponse |
| 415 | } |
| 416 | |
| 417 | for _, tt := range tests { |
| 418 | resp, _ := mustGet(proxy.URL + tt.req) |
| 419 | if resp.StatusCode != tt.wantCode { |
| 420 | t.Errorf("got status code %d, want %d", resp.StatusCode, tt.wantCode) |
| 421 | } |
| 422 | gotLoc, _ := resp.Location() |
| 423 | if gotLoc.String() != tt.wantLoc { |
| 424 | t.Errorf("got location %s, want %s", gotLoc, tt.wantLoc) |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | func TestProxyLogOutput(t *testing.T) { |
| 430 | t.Run("uncompressed response", func(t *testing.T) { |