eia-api-go/pkg/eia/eia_logging_test.go
2024-12-11 15:41:09 -05:00

49 lines
892 B
Go

package eia
import (
"context"
"net/http"
"net/url"
"testing"
"github.com/rs/zerolog"
)
func Test_newLoggingMiddleware(t *testing.T) {
logger := zerolog.New(zerolog.NewTestWriter(t))
tests := []struct {
name string
request *http.Request
wantErr bool
}{
{
name: "Test empty request",
request: &http.Request{},
wantErr: true,
},
{
name: "Test valid request",
request: &http.Request{
Method: "GET",
URL: &url.URL{
Scheme: "https",
Host: "test.test",
Path: "/test",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
middlewareFunc := newLoggingMiddleware(&logger, zerolog.TraceLevel)
err := middlewareFunc(context.Background(), tt.request)
if (err != nil) != tt.wantErr {
t.Errorf("Test %s had unexpected error: %s", tt.name, err.Error())
}
})
}
}