(t *testing.T)
| 469 | } |
| 470 | |
| 471 | func TestBasePrefix(t *testing.T) { |
| 472 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 473 | fmt.Fprintln(w, "OK") |
| 474 | }) |
| 475 | |
| 476 | testCases := []struct { |
| 477 | name string |
| 478 | basePrefix string |
| 479 | path string |
| 480 | expected string |
| 481 | }{ |
| 482 | { |
| 483 | name: "no prefix", |
| 484 | basePrefix: "", |
| 485 | path: "/.within.website/x/cmd/anubis/api/make-challenge", |
| 486 | expected: "/.within.website/x/cmd/anubis/api/make-challenge", |
| 487 | }, |
| 488 | { |
| 489 | name: "with prefix", |
| 490 | basePrefix: "/myapp", |
| 491 | path: "/myapp/.within.website/x/cmd/anubis/api/make-challenge", |
| 492 | expected: "/myapp/.within.website/x/cmd/anubis/api/make-challenge", |
| 493 | }, |
| 494 | { |
| 495 | name: "with prefix and trailing slash", |
| 496 | basePrefix: "/myapp/", |
| 497 | path: "/myapp/.within.website/x/cmd/anubis/api/make-challenge", |
| 498 | expected: "/myapp/.within.website/x/cmd/anubis/api/make-challenge", |
| 499 | }, |
| 500 | } |
| 501 | |
| 502 | for _, tc := range testCases { |
| 503 | t.Run(tc.name, func(t *testing.T) { |
| 504 | // Reset the global BasePrefix before each test |
| 505 | anubis.BasePrefix = "" |
| 506 | |
| 507 | pol := loadPolicies(t, "", 4) |
| 508 | |
| 509 | srv := spawnAnubis(t, Options{ |
| 510 | Next: h, |
| 511 | Policy: pol, |
| 512 | BasePrefix: tc.basePrefix, |
| 513 | }) |
| 514 | |
| 515 | ts := httptest.NewServer(internal.RemoteXRealIP(true, "tcp", srv)) |
| 516 | defer ts.Close() |
| 517 | |
| 518 | cli := httpClient(t) |
| 519 | |
| 520 | req, err := http.NewRequest(http.MethodPost, ts.URL+tc.path, nil) |
| 521 | if err != nil { |
| 522 | t.Fatal(err) |
| 523 | } |
| 524 | |
| 525 | q := req.URL.Query() |
| 526 | redir := tc.basePrefix |
| 527 | if tc.basePrefix == "" { |
| 528 | redir = "/" |
nothing calls this directly
no test coverage detected