From 625a7be240a2ef5925558491c1b3d3274382bb02 Mon Sep 17 00:00:00 2001 From: Todd Treece <360020+toddtreece@users.noreply.github.com> Date: Mon, 11 Apr 2022 08:23:26 -0400 Subject: [PATCH] HTTP Logger: Add Path and Enabled overrides (#485) --- experimental/http_logger/http_logger.go | 36 +++++++++++++++++--- experimental/http_logger/http_logger_test.go | 33 ++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/experimental/http_logger/http_logger.go b/experimental/http_logger/http_logger.go index 3f8183e9d..044a665d0 100644 --- a/experimental/http_logger/http_logger.go +++ b/experimental/http_logger/http_logger.go @@ -29,17 +29,26 @@ type HTTPLogger struct { fixture *fixture.Fixture } +type Options struct { + Path string + EnabledFn func() bool +} + // NewHTTPLogger creates a new HTTPLogger. -func NewHTTPLogger(pluginID string, proxied http.RoundTripper) *HTTPLogger { - path := defaultPath(pluginID) - s := storage.NewHARStorage(path) +func NewHTTPLogger(pluginID string, proxied http.RoundTripper, opts ...Options) *HTTPLogger { + if len(opts) > 1 { + panic("too many Options arguments provided") + } + + loggerOpts := getOptions(pluginID, opts...) + s := storage.NewHARStorage(loggerOpts.Path) f := fixture.NewFixture(s) return &HTTPLogger{ pluginID: pluginID, proxied: proxied, fixture: f, - enabled: defaultEnabledCheck, + enabled: loggerOpts.EnabledFn, } } @@ -96,3 +105,22 @@ func getTempFilePath(pluginID string) string { filename := fmt.Sprintf("%s_%d.har", pluginID, time.Now().UnixMilli()) return path.Join(os.TempDir(), filename) } + +func getOptions(pluginID string, opts ...Options) Options { + o := Options{EnabledFn: defaultEnabledCheck, Path: defaultPath(pluginID)} + + // if there's not one set of options provided, return the defaults + if len(opts) != 1 { + return o + } + + if opts[0].Path != "" { + o.Path = opts[0].Path + } + + if opts[0].EnabledFn != nil { + o.EnabledFn = opts[0].EnabledFn + } + + return o +} diff --git a/experimental/http_logger/http_logger_test.go b/experimental/http_logger/http_logger_test.go index 382bba2b8..3fa9c8094 100644 --- a/experimental/http_logger/http_logger_test.go +++ b/experimental/http_logger/http_logger_test.go @@ -48,6 +48,39 @@ func TestHTTPLogger(t *testing.T) { _, err = os.Stat(f.Name()) require.Equal(t, true, errors.Is(err, os.ErrNotExist)) }) + + t.Run("should set path and enabled overrides", func(t *testing.T) { + // ensure env variables are not set + os.Setenv(httplogger.PluginHARLogEnabledEnv, "false") + os.Setenv(httplogger.PluginHARLogPathEnv, "") + + f, err := os.CreateTemp("", "test_*.har") + defer os.Remove(f.Name()) + require.NoError(t, err) + h := httplogger.NewHTTPLogger("example-plugin-id", &fakeRoundTripper{}, httplogger.Options{ + Path: f.Name(), + EnabledFn: func() bool { return true }, + }) + c := &http.Client{ + Transport: h, + Timeout: time.Second * 30, + } + res, err := c.Get("http://example.com") + require.NoError(t, err) + defer res.Body.Close() + require.Equal(t, http.StatusOK, res.StatusCode) + b, err := ioutil.ReadAll(res.Body) + require.NoError(t, err) + require.Equal(t, "OK", string(b)) + expected := storage.NewHARStorage("testdata/example.har") + actual := storage.NewHARStorage(f.Name()) + require.Equal(t, 1, len(actual.Entries())) + require.Equal(t, expected.Entries()[0].Request, actual.Entries()[0].Request) + require.Equal(t, expected.Entries()[0].Response, actual.Entries()[0].Response) + har, err := ioutil.ReadFile(f.Name()) + require.NoError(t, err) + require.Greater(t, len(har), 0) + }) } func setup(enabled bool) (*http.Client, *os.File) {