Skip to content

Commit

Permalink
HTTP Logger: Add Path and Enabled overrides (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
toddtreece authored Apr 11, 2022
1 parent 3729d4d commit 625a7be
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
36 changes: 32 additions & 4 deletions experimental/http_logger/http_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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
}
33 changes: 33 additions & 0 deletions experimental/http_logger/http_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 625a7be

Please sign in to comment.