| @@ -32,6 +32,8 @@ import ( · 31 unmodified lines |
| 32 | 32 | coreusage "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/usage" |
| 33 | 33 | sdkconfig "github.com/router-for-me/CLIProxyAPI/v7/sdk/config" |
| 34 | 34 | "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi" |
| 35 | + log "github.com/sirupsen/logrus" |
| 36 | + logtest "github.com/sirupsen/logrus/hooks/test" |
| 35 | 37 | "gopkg.in/yaml.v3" |
| 36 | 38 | ) |
| 37 | 39 | |
| @@ -666,6 +668,48 @@ func TestHealthz(t *testing.T) { · 628 unmodified lines |
| 666 | 668 | }) |
| 667 | 669 | } |
| 668 | 670 | |
| 671 | +func TestHealthzSkipsAccessLogging(t *testing.T) { |
| 672 | + server := newTestServer(t) |
| 673 | + logger := log.StandardLogger() |
| 674 | + previousHooks := logger.ReplaceHooks(make(log.LevelHooks)) |
| 675 | + previousLevel := logger.GetLevel() |
| 676 | + hook := logtest.NewLocal(logger) |
| 677 | + logger.SetLevel(log.InfoLevel) |
| 678 | + t.Cleanup(func() { |
| 679 | + logger.ReplaceHooks(previousHooks) |
| 680 | + logger.SetLevel(previousLevel) |
| 681 | + }) |
| 682 | + |
| 683 | + for _, method := range []string{http.MethodGet, http.MethodHead} { |
| 684 | + t.Run(method, func(t *testing.T) { |
| 685 | + hook.Reset() |
| 686 | + recorder := httptest.NewRecorder() |
| 687 | + server.engine.ServeHTTP(recorder, httptest.NewRequest(method, "/healthz", nil)) |
| 688 | + if recorder.Code != http.StatusOK { |
| 689 | + t.Fatalf("health probe status = %d, want 200", recorder.Code) |
| 690 | + } |
| 691 | + for _, entry := range hook.AllEntries() { |
| 692 | + if _, isAccessLog := entry.Data["request_id"]; isAccessLog && strings.Contains(entry.Message, `"/healthz"`) { |
| 693 | + t.Errorf("health probe emitted an access log: %s", entry.Message) |
| 694 | + } |
| 695 | + } |
| 696 | + |
| 697 | + hook.Reset() |
| 698 | + ordinary := httptest.NewRecorder() |
| 699 | + server.engine.ServeHTTP(ordinary, httptest.NewRequest(http.MethodGet, "/healthz-access-log-control", nil)) |
| 700 | + if ordinary.Code != http.StatusNotFound { |
| 701 | + t.Fatalf("control status = %d, want 404", ordinary.Code) |
| 702 | + } |
| 703 | + for _, entry := range hook.AllEntries() { |
| 704 | + if _, isAccessLog := entry.Data["request_id"]; isAccessLog && strings.Contains(entry.Message, `"/healthz-access-log-control"`) { |
| 705 | + return |
| 706 | + } |
| 707 | + } |
| 708 | + t.Error("ordinary request did not emit an access log after health probe") |
| 709 | + }) |
| 710 | + } |
| 711 | +} |
| 712 | + |
| 669 | 713 | func TestCodexLiveRoutesRequireAuthAndAreRegistered(t *testing.T) { |
| 670 | 714 | server := newTestServer(t) |
| 671 | 715 | |