2019-11-27 20:25:40 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2019-12-18 22:44:08 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/certificates/errs"
|
2019-11-27 20:25:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestError_MarshalJSON(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
Status int
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
want []byte
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{"ok", fields{400, fmt.Errorf("bad request")}, []byte(`{"status":400,"message":"Bad Request"}`), false},
|
|
|
|
{"ok no error", fields{500, nil}, []byte(`{"status":500,"message":"Internal Server Error"}`), false},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-18 22:44:08 +00:00
|
|
|
e := &errs.Error{
|
2019-11-27 20:25:40 +00:00
|
|
|
Status: tt.fields.Status,
|
|
|
|
Err: tt.fields.Err,
|
|
|
|
}
|
|
|
|
got, err := e.MarshalJSON()
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Error.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("Error.MarshalJSON() = %s, want %s", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestError_UnmarshalJSON(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2019-12-18 22:44:08 +00:00
|
|
|
expected *errs.Error
|
2019-11-27 20:25:40 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-12-18 22:44:08 +00:00
|
|
|
{"ok", args{[]byte(`{"status":400,"message":"bad request"}`)}, &errs.Error{Status: 400, Err: fmt.Errorf("bad request")}, false},
|
|
|
|
{"fail", args{[]byte(`{"status":"400","message":"bad request"}`)}, &errs.Error{}, true},
|
2019-11-27 20:25:40 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-18 22:44:08 +00:00
|
|
|
e := new(errs.Error)
|
2019-11-27 20:25:40 +00:00
|
|
|
if err := e.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Error.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tt.expected, e) {
|
|
|
|
t.Errorf("Error.UnmarshalJSON() wants = %v, got %v", tt.expected, e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|