Add logging tests

This commit is contained in:
Ryan McGuire 2024-12-11 15:41:09 -05:00
parent ee53efbcd6
commit 549124950a
3 changed files with 133 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package eia
import ( import (
"context" "context"
"errors"
"net/http" "net/http"
"time" "time"
@ -12,6 +13,14 @@ import (
func newLoggingMiddleware(logger *zerolog.Logger, level zerolog.Level) eiaapi.RequestEditorFn { func newLoggingMiddleware(logger *zerolog.Logger, level zerolog.Level) eiaapi.RequestEditorFn {
return func(_ context.Context, req *http.Request) error { return func(_ context.Context, req *http.Request) error {
if req == nil {
return errors.New("invalid nil http request")
} else if req.URL == nil {
return errors.New("missing request url")
} else if req.Method == "" {
return errors.New("missing request method")
}
// Don't log api_key // Don't log api_key
params := req.URL.Query() params := req.URL.Query()
delete(params, "api_key") delete(params, "api_key")

View File

@ -0,0 +1,48 @@
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())
}
})
}
}

View File

@ -89,6 +89,82 @@ func Test_prepMethodArgs(t *testing.T) {
} }
} }
func Test_getFRR(t *testing.T) {
sampleFinalRouteResponse := eiaapi.FinalRouteResponse{
Request: &eiaapi.RouteRequest{},
Response: &eiaapi.FinalRoute{
Description: ptr.To("Annual Energy Outlook"),
Id: ptr.To("aeo"),
Facets: &[]eiaapi.FacetMetaData{
{
Description: ptr.To("Future Scenario"),
Id: ptr.To("scenario"),
},
},
},
}
sampleRouteResponse := eiaapi.RouteResponse{
Request: &eiaapi.RouteRequest{},
Response: &eiaapi.Routes{
Description: ptr.To("Annual Energy Outlook"),
Id: ptr.To("aeo"),
Name: ptr.To("aeo"),
Routes: ptr.To([]eiaapi.Routes{
{
Id: ptr.To("2023"),
Name: ptr.To("2023"),
},
}),
},
}
type args struct {
json200 reflect.Value
}
tests := []struct {
name string
args args
want eiaapi.FinalRouteResponse
wantErr bool
wantNil bool
}{
{
name: "Extract final route response",
args: args{
json200: reflect.ValueOf(&sampleFinalRouteResponse),
},
want: sampleFinalRouteResponse,
wantNil: false,
wantErr: false,
},
{
name: "Unexpected routes response",
args: args{
json200: reflect.ValueOf(&sampleRouteResponse),
},
want: eiaapi.FinalRouteResponse{},
wantNil: true,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getFRR(tt.args.json200)
if (err != nil) != tt.wantErr {
t.Errorf("getFRR() error = %v, wantErr %v", err, tt.wantErr)
return
} else if (got == nil) != tt.wantNil {
t.Error("unexpected nil response")
return
} else if (got == nil) == tt.wantNil {
return
}
if !reflect.DeepEqual(*got, tt.want) {
t.Errorf("getFRR() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getRR(t *testing.T) { func Test_getRR(t *testing.T) {
sampleFinalRouteResponse := eiaapi.FinalRouteResponse{ sampleFinalRouteResponse := eiaapi.FinalRouteResponse{
Request: &eiaapi.RouteRequest{}, Request: &eiaapi.RouteRequest{},