Skip to content

Commit

Permalink
adds tests, fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
M4tteoP committed Mar 25, 2024
1 parent 6be984d commit dfb3b2d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
59 changes: 59 additions & 0 deletions internal/auditlog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,62 @@ func TestGetFormatters(t *testing.T) {
}
})
}

type noopWriter struct{}

func (noopWriter) Init(plugintypes.AuditLogConfig) error { return nil }
func (noopWriter) Write(plugintypes.AuditLog) error { return nil }
func (noopWriter) Close() error { return nil }

func TestRegisterAndGetWriter(t *testing.T) {

testCases := []struct {
name string
}{
{"customwriter"},
{"CustomWriter"},
{"CUSTOMWRITER"},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
RegisterWriter(tc.name, func() plugintypes.AuditLogWriter {
return noopWriter{}
})

writer, err := GetWriter(tc.name)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}

if writer == nil {
t.Fatalf("expected a writer, got nil")
}
})
}
}

func TestRegisterAndGetFormatter(t *testing.T) {

testCases := []struct {
name string
}{
{"customFormatter"},
{"customformatter"},
{"CUSTOMFORMATTER"},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
RegisterFormatter(tc.name, &noopFormatter{})
retrievedFormatter, err := GetFormatter(tc.name)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}

if retrievedFormatter == nil {
t.Fatalf("expected a formatter, got nil")
}
})
}
}
2 changes: 1 addition & 1 deletion internal/auditlog/noop_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package auditlog

import "github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"

// noopWriter is used to store logs in a single file
// noopWriter is used as a no operations audit log writer.
type noopWriter struct{}

func (noopWriter) Init(plugintypes.AuditLogConfig) error { return nil }
Expand Down

0 comments on commit dfb3b2d

Please sign in to comment.