Skip to content

Commit

Permalink
api: moved log-related tests to api/log
Browse files Browse the repository at this point in the history
  • Loading branch information
azazeal committed Mar 18, 2022
1 parent f1ff349 commit 0831f6e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
44 changes: 44 additions & 0 deletions api/log/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package log

import (
"errors"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/smallstep/certificates/logging"
)

func TestError(t *testing.T) {
theError := errors.New("the error")

type args struct {
rw http.ResponseWriter
err error
}
tests := []struct {
name string
args args
withFields bool
}{
{"normalLogger", args{httptest.NewRecorder(), theError}, false},
{"responseLogger", args{logging.NewResponseLogger(httptest.NewRecorder()), theError}, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Error(tt.args.rw, tt.args.err)
if tt.withFields {
if rl, ok := tt.args.rw.(logging.ResponseLogger); ok {
fields := rl.Fields()
if !reflect.DeepEqual(fields["error"], theError) {
t.Errorf("ResponseLogger[\"error\"] = %s, wants %s", fields["error"], theError)
}
} else {
t.Error("ResponseWriter does not implement logging.ResponseLogger")
}
}
})
}
}
36 changes: 0 additions & 36 deletions api/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,11 @@ package api
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/pkg/errors"

"github.com/smallstep/certificates/api/log"
"github.com/smallstep/certificates/logging"
)

func TestLogError(t *testing.T) {
theError := errors.New("the error")
type args struct {
rw http.ResponseWriter
err error
}
tests := []struct {
name string
args args
withFields bool
}{
{"normalLogger", args{httptest.NewRecorder(), theError}, false},
{"responseLogger", args{logging.NewResponseLogger(httptest.NewRecorder()), theError}, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
log.Error(tt.args.rw, tt.args.err)
if tt.withFields {
if rl, ok := tt.args.rw.(logging.ResponseLogger); ok {
fields := rl.Fields()
if !reflect.DeepEqual(fields["error"], theError) {
t.Errorf("ResponseLogger[\"error\"] = %s, wants %s", fields["error"], theError)
}
} else {
t.Error("ResponseWriter does not implement logging.ResponseLogger")
}
}
})
}
}

func TestJSON(t *testing.T) {
type args struct {
rw http.ResponseWriter
Expand Down

0 comments on commit 0831f6e

Please sign in to comment.