| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package proto |
| 15 |
|
| 16 |
import ( |
| 17 |
"encoding/json" |
| 18 |
"errors" |
| 19 |
"reflect" |
| 20 |
"testing" |
| 21 |
) |
| 22 |
|
| 23 |
func TestRequestError_Error(t *testing.T) { |
| 24 |
err := RequestError{Code: ErrorCodeAccessDenied, Err: errors.New("an error")} |
| 25 |
want := "an error" |
| 26 |
if got := err.Error(); got != want { |
| 27 |
t.Errorf("RequestError.Error() = %v, want %v", got, want) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TestRequestError_Unwrap(t *testing.T) { |
| 32 |
want := errors.New("an error") |
| 33 |
got := RequestError{Code: ErrorCodeAccessDenied, Err: want}.Unwrap() |
| 34 |
if got != want { |
| 35 |
t.Errorf("RequestError.Unwrap() = %v, want %v", got, want) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func TestRequestError_MarshalJSON(t *testing.T) { |
| 40 |
tests := []struct { |
| 41 |
name string |
| 42 |
e RequestError |
| 43 |
want []byte |
| 44 |
}{ |
| 45 |
{"empty", RequestError{}, []byte("{\"errorCode\":\"\"}")}, |
| 46 |
{"with code", RequestError{Code: ErrorCodeAccessDenied}, []byte("{\"errorCode\":\"ACCESS_DENIED\"}")}, |
| 47 |
{"with message", RequestError{Code: ErrorCodeAccessDenied, Err: errors.New("failed")}, []byte("{\"errorCode\":\"ACCESS_DENIED\",\"errorMessage\":\"failed\"}")}, |
| 48 |
{ |
| 49 |
"with metadata", |
| 50 |
RequestError{Code: ErrorCodeAccessDenied, Err: errors.New("failed"), Metadata: map[string]string{"a": "b"}}, |
| 51 |
[]byte("{\"errorCode\":\"ACCESS_DENIED\",\"errorMessage\":\"failed\",\"errorMetadata\":{\"a\":\"b\"}}"), |
| 52 |
}, |
| 53 |
} |
| 54 |
for _, tt := range tests { |
| 55 |
t.Run(tt.name, func(t *testing.T) { |
| 56 |
got, err := tt.e.MarshalJSON() |
| 57 |
if err != nil { |
| 58 |
t.Fatalf("RequestError.MarshalJSON() error = %v, wantErr false", err) |
| 59 |
} |
| 60 |
if !reflect.DeepEqual(got, tt.want) { |
| 61 |
t.Fatalf("RequestError.MarshalJSON() = %s, want %s", got, tt.want) |
| 62 |
} |
| 63 |
if tt.e.Code == "" { |
| 64 |
return |
| 65 |
} |
| 66 |
var got1 RequestError |
| 67 |
err = json.Unmarshal(got, &got1) |
| 68 |
if err != nil { |
| 69 |
t.Fatalf("RequestError.UnmarshalJSON() error = %v, wantErr false", err) |
| 70 |
} |
| 71 |
if got1.Code != tt.e.Code || !reflect.DeepEqual(got1.Metadata, tt.e.Metadata) { |
| 72 |
t.Fatalf("RequestError.UnmarshalJSON() = %s, want %s", got1, tt.e) |
| 73 |
} |
| 74 |
}) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestRequestError_UnmarshalJSON(t *testing.T) { |
| 79 |
type args struct { |
| 80 |
data []byte |
| 81 |
} |
| 82 |
tests := []struct { |
| 83 |
name string |
| 84 |
args args |
| 85 |
want RequestError |
| 86 |
wantErr bool |
| 87 |
}{ |
| 88 |
{"invalid", args{[]byte("")}, RequestError{}, true}, |
| 89 |
{"empty", args{[]byte("{}")}, RequestError{}, true}, |
| 90 |
{"with code", args{[]byte("{\"errorCode\":\"ACCESS_DENIED\"}")}, RequestError{Code: ErrorCodeAccessDenied}, false}, |
| 91 |
} |
| 92 |
for _, tt := range tests { |
| 93 |
t.Run(tt.name, func(t *testing.T) { |
| 94 |
var e RequestError |
| 95 |
if err := e.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr { |
| 96 |
t.Errorf("RequestError.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) |
| 97 |
} |
| 98 |
if !tt.wantErr && (e.Code != tt.want.Code || !reflect.DeepEqual(e.Metadata, tt.want.Metadata)) { |
| 99 |
t.Fatalf("RequestError.UnmarshalJSON() = %s, want %s", e, tt.want) |
| 100 |
} |
| 101 |
}) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestRequestError_Is(t *testing.T) { |
| 106 |
type args struct { |
| 107 |
target error |
| 108 |
} |
| 109 |
tests := []struct { |
| 110 |
name string |
| 111 |
e RequestError |
| 112 |
args args |
| 113 |
want bool |
| 114 |
}{ |
| 115 |
{"nil", RequestError{}, args{nil}, false}, |
| 116 |
{"not same type", RequestError{Err: errors.New("foo")}, args{errors.New("foo")}, false}, |
| 117 |
{"only same code", RequestError{Code: ErrorCodeGeneric, Err: errors.New("foo")}, args{RequestError{Code: ErrorCodeGeneric, Err: errors.New("bar")}}, false}, |
| 118 |
{"only same message", RequestError{Code: ErrorCodeTimeout, Err: errors.New("foo")}, args{RequestError{Code: ErrorCodeGeneric, Err: errors.New("foo")}}, false}, |
| 119 |
{"same with nil message", RequestError{Code: ErrorCodeGeneric}, args{RequestError{Code: ErrorCodeGeneric}}, true}, |
| 120 |
{"same", RequestError{Code: ErrorCodeGeneric, Err: errors.New("foo")}, args{RequestError{Code: ErrorCodeGeneric, Err: errors.New("foo")}}, true}, |
| 121 |
} |
| 122 |
for _, tt := range tests { |
| 123 |
t.Run(tt.name, func(t *testing.T) { |
| 124 |
if got := tt.e.Is(tt.args.target); got != tt.want { |
| 125 |
t.Errorf("RequestError.Is() = %v, want %v", got, tt.want) |
| 126 |
} |
| 127 |
}) |
| 128 |
} |
| 129 |
} |