2022-03-22 12:31:18 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2022-11-17 11:29:54 +00:00
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
pkgerrors "github.com/pkg/errors"
|
2022-03-22 12:31:18 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/certificates/logging"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestError(t *testing.T) {
|
2022-11-17 11:29:54 +00:00
|
|
|
|
|
|
|
t.Setenv("STEPDEBUG", "1") // force to end of `Error` function instead of early return
|
2022-03-22 12:31:18 +00:00
|
|
|
theError := errors.New("the error")
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
rw http.ResponseWriter
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
withFields bool
|
2022-11-17 11:29:54 +00:00
|
|
|
want error
|
2022-03-22 12:31:18 +00:00
|
|
|
}{
|
2022-11-17 11:29:54 +00:00
|
|
|
{"normalLogger", args{httptest.NewRecorder(), theError}, false, theError},
|
|
|
|
{"responseLogger", args{logging.NewResponseLogger(httptest.NewRecorder()), theError}, true, theError},
|
2022-03-22 12:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2022-11-17 11:29:54 +00:00
|
|
|
if !reflect.DeepEqual(fields["error"], tt.want) {
|
|
|
|
t.Errorf("ResponseLogger[\"error\"] = %s, wants %s", fields["error"], tt.want)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error("ResponseWriter does not implement logging.ResponseLogger")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockStackTracedError struct{}
|
|
|
|
|
|
|
|
func (t *mockStackTracedError) Error() string {
|
|
|
|
return "a stacktraced error"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *mockStackTracedError) StackTrace() pkgerrors.StackTrace {
|
|
|
|
f := struct{}{}
|
|
|
|
return pkgerrors.StackTrace{ // fake stacktrace
|
|
|
|
pkgerrors.Frame(unsafe.Pointer(&f)),
|
|
|
|
pkgerrors.Frame(unsafe.Pointer(&f)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestError_StackTracedError(t *testing.T) {
|
|
|
|
|
|
|
|
t.Setenv("STEPDEBUG", "1")
|
|
|
|
aStackTracedError := mockStackTracedError{}
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
rw http.ResponseWriter
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
withFields bool
|
|
|
|
want error
|
|
|
|
}{
|
|
|
|
{"responseLoggerWithStackTracedError", args{logging.NewResponseLogger(httptest.NewRecorder()), &aStackTracedError}, true, &aStackTracedError},
|
|
|
|
}
|
|
|
|
|
|
|
|
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"], tt.want) {
|
|
|
|
t.Errorf("ResponseLogger[\"error\"] = %s, wants %s", fields["error"], tt.want)
|
|
|
|
}
|
|
|
|
// `stack-trace` expected to be set; not interested in actual output
|
|
|
|
if _, ok := fields["stack-trace"]; !ok {
|
|
|
|
t.Errorf("ResponseLogger[\"stack-trace\"] not set")
|
2022-03-22 12:31:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error("ResponseWriter does not implement logging.ResponseLogger")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|