| 445 | } |
| 446 | |
| 447 | func TestPathHandling(t *testing.T) { |
| 448 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 449 | fmt.Fprint(w, r.URL.Path) |
| 450 | })) |
| 451 | defer ts.Close() |
| 452 | |
| 453 | table := []struct { |
| 454 | name string |
| 455 | prefix string |
| 456 | reqPath string |
| 457 | expectPath string |
| 458 | appendPath bool |
| 459 | }{ |
| 460 | {"test1", "/api/", "/metrics", "404 page not found\n", false}, |
| 461 | {"test2", "/api/", "/api/metrics", "/api/metrics", false}, |
| 462 | {"test3", "/api/", "/api/v1/pods/", "/api/v1/pods/", false}, |
| 463 | {"test4", "/", "/metrics", "/metrics", false}, |
| 464 | {"test5", "/", "/api/v1/pods/", "/api/v1/pods/", false}, |
| 465 | {"test6", "/custom/", "/metrics", "404 page not found\n", false}, |
| 466 | {"test7", "/custom/", "/api/metrics", "404 page not found\n", false}, |
| 467 | {"test8", "/custom/", "/api/v1/pods/", "404 page not found\n", false}, |
| 468 | {"test9", "/custom/", "/custom/api/metrics", "/api/metrics", false}, |
| 469 | {"test10", "/custom/", "/custom/api/v1/pods/", "/api/v1/pods/", false}, |
| 470 | {"test11", "/custom/", "/custom/api/v1/services/", "/api/v1/services/", true}, |
| 471 | } |
| 472 | |
| 473 | cc := &rest.Config{ |
| 474 | Host: ts.URL, |
| 475 | } |
| 476 | |
| 477 | for _, tt := range table { |
| 478 | t.Run(tt.name, func(t *testing.T) { |
| 479 | p, err := NewServer("", tt.prefix, "/not/used/for/this/test", nil, cc, 0, tt.appendPath) |
| 480 | if err != nil { |
| 481 | t.Fatalf("%#v: %v", tt, err) |
| 482 | } |
| 483 | pts := httptest.NewServer(p.handler) |
| 484 | defer pts.Close() |
| 485 | |
| 486 | r, err := http.Get(pts.URL + tt.reqPath) |
| 487 | if err != nil { |
| 488 | t.Fatalf("%#v: %v", tt, err) |
| 489 | } |
| 490 | body, err := io.ReadAll(r.Body) |
| 491 | r.Body.Close() |
| 492 | if err != nil { |
| 493 | t.Fatalf("%#v: %v", tt, err) |
| 494 | } |
| 495 | if e, a := tt.expectPath, string(body); e != a { |
| 496 | t.Errorf("%#v: Wanted %q, got %q", tt, e, a) |
| 497 | } |
| 498 | }) |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | func TestExtractHost(t *testing.T) { |
| 503 | fixtures := map[string]string{ |