| 1 |
package autorest |
| 2 |
|
| 3 |
// Copyright 2017 Microsoft Corporation |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
// you may not use this file except in compliance with the License. |
| 7 |
// You may obtain a copy of the License at |
| 8 |
// |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
// |
| 11 |
// Unless required by applicable law or agreed to in writing, software |
| 12 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
// See the License for the specific language governing permissions and |
| 15 |
// limitations under the License. |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"net/http" |
| 20 |
"reflect" |
| 21 |
"regexp" |
| 22 |
"testing" |
| 23 |
) |
| 24 |
|
| 25 |
func TestNewErrorWithError_AssignsPackageType(t *testing.T) { |
| 26 |
e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message") |
| 27 |
|
| 28 |
if e.PackageType != "packageType" { |
| 29 |
t.Fatalf("autorest: Error failed to set package type -- expected %v, received %v", "packageType", e.PackageType) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func TestNewErrorWithError_AssignsMethod(t *testing.T) { |
| 34 |
e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message") |
| 35 |
|
| 36 |
if e.Method != "method" { |
| 37 |
t.Fatalf("autorest: Error failed to set method -- expected %v, received %v", "method", e.Method) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func TestNewErrorWithError_AssignsMessage(t *testing.T) { |
| 42 |
e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message") |
| 43 |
|
| 44 |
if e.Message != "message" { |
| 45 |
t.Fatalf("autorest: Error failed to set message -- expected %v, received %v", "message", e.Message) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func TestNewErrorWithError_AssignsUndefinedStatusCodeIfRespNil(t *testing.T) { |
| 50 |
e := NewErrorWithError(nil, "packageType", "method", nil, "message") |
| 51 |
if e.StatusCode != UndefinedStatusCode { |
| 52 |
t.Fatalf("autorest: Error failed to set status code -- expected %v, received %v", UndefinedStatusCode, e.StatusCode) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func TestNewErrorWithError_AssignsStatusCode(t *testing.T) { |
| 57 |
e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", &http.Response{ |
| 58 |
StatusCode: http.StatusBadRequest, |
| 59 |
Status: http.StatusText(http.StatusBadRequest)}, "message") |
| 60 |
|
| 61 |
if e.StatusCode != http.StatusBadRequest { |
| 62 |
t.Fatalf("autorest: Error failed to set status code -- expected %v, received %v", http.StatusBadRequest, e.StatusCode) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestNewErrorWithError_AcceptsArgs(t *testing.T) { |
| 67 |
e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message %s", "arg") |
| 68 |
|
| 69 |
if matched, _ := regexp.MatchString(`.*arg.*`, e.Message); !matched { |
| 70 |
t.Fatalf("autorest: Error failed to apply message arguments -- expected %v, received %v", |
| 71 |
`.*arg.*`, e.Message) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func TestNewErrorWithError_AssignsError(t *testing.T) { |
| 76 |
err := fmt.Errorf("original") |
| 77 |
e := NewErrorWithError(err, "packageType", "method", nil, "message") |
| 78 |
|
| 79 |
if e.Original != err { |
| 80 |
t.Fatalf("autorest: Error failed to set error -- expected %v, received %v", err, e.Original) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func TestNewErrorWithResponse_ContainsStatusCode(t *testing.T) { |
| 85 |
e := NewErrorWithResponse("packageType", "method", &http.Response{ |
| 86 |
StatusCode: http.StatusBadRequest, |
| 87 |
Status: http.StatusText(http.StatusBadRequest)}, "message") |
| 88 |
|
| 89 |
if e.StatusCode != http.StatusBadRequest { |
| 90 |
t.Fatalf("autorest: Error failed to set status code -- expected %v, received %v", http.StatusBadRequest, e.StatusCode) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestNewErrorWithResponse_nilResponse_ReportsUndefinedStatusCode(t *testing.T) { |
| 95 |
e := NewErrorWithResponse("packageType", "method", nil, "message") |
| 96 |
|
| 97 |
if e.StatusCode != UndefinedStatusCode { |
| 98 |
t.Fatalf("autorest: Error failed to set status code -- expected %v, received %v", UndefinedStatusCode, e.StatusCode) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func TestNewErrorWithResponse_Forwards(t *testing.T) { |
| 103 |
e1 := NewError("packageType", "method", "message %s", "arg") |
| 104 |
e2 := NewErrorWithResponse("packageType", "method", nil, "message %s", "arg") |
| 105 |
|
| 106 |
if !reflect.DeepEqual(e1, e2) { |
| 107 |
t.Fatal("autorest: NewError did not return an error equivalent to NewErrorWithError") |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func TestNewErrorWithError_Forwards(t *testing.T) { |
| 112 |
e1 := NewError("packageType", "method", "message %s", "arg") |
| 113 |
e2 := NewErrorWithError(nil, "packageType", "method", nil, "message %s", "arg") |
| 114 |
|
| 115 |
if !reflect.DeepEqual(e1, e2) { |
| 116 |
t.Fatal("autorest: NewError did not return an error equivalent to NewErrorWithError") |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
func TestNewErrorWithError_DoesNotWrapADetailedError(t *testing.T) { |
| 121 |
e1 := NewError("packageType1", "method1", "message1 %s", "arg1") |
| 122 |
e2 := NewErrorWithError(e1, "packageType2", "method2", nil, "message2 %s", "arg2") |
| 123 |
|
| 124 |
if !reflect.DeepEqual(e1, e2) { |
| 125 |
t.Fatalf("autorest: NewErrorWithError incorrectly wrapped a DetailedError -- expected %v, received %v", e1, e2) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestNewErrorWithError_WrapsAnError(t *testing.T) { |
| 130 |
e1 := fmt.Errorf("Inner Error") |
| 131 |
var e2 interface{} = NewErrorWithError(e1, "packageType", "method", nil, "message") |
| 132 |
|
| 133 |
if _, ok := e2.(DetailedError); !ok { |
| 134 |
t.Fatalf("autorest: NewErrorWithError failed to wrap a standard error -- received %T", e2) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func TestDetailedError(t *testing.T) { |
| 139 |
err := fmt.Errorf("original") |
| 140 |
e := NewErrorWithError(err, "packageType", "method", nil, "message") |
| 141 |
|
| 142 |
if matched, _ := regexp.MatchString(`.*original.*`, e.Error()); !matched { |
| 143 |
t.Fatalf("autorest: Error#Error failed to return original error message -- expected %v, received %v", |
| 144 |
`.*original.*`, e.Error()) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func TestDetailedErrorConstainsPackageType(t *testing.T) { |
| 149 |
e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message") |
| 150 |
|
| 151 |
if matched, _ := regexp.MatchString(`.*packageType.*`, e.Error()); !matched { |
| 152 |
t.Fatalf("autorest: Error#String failed to include PackageType -- expected %v, received %v", |
| 153 |
`.*packageType.*`, e.Error()) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func TestDetailedErrorConstainsMethod(t *testing.T) { |
| 158 |
e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message") |
| 159 |
|
| 160 |
if matched, _ := regexp.MatchString(`.*method.*`, e.Error()); !matched { |
| 161 |
t.Fatalf("autorest: Error#String failed to include Method -- expected %v, received %v", |
| 162 |
`.*method.*`, e.Error()) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
func TestDetailedErrorConstainsMessage(t *testing.T) { |
| 167 |
e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message") |
| 168 |
|
| 169 |
if matched, _ := regexp.MatchString(`.*message.*`, e.Error()); !matched { |
| 170 |
t.Fatalf("autorest: Error#String failed to include Message -- expected %v, received %v", |
| 171 |
`.*message.*`, e.Error()) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
func TestDetailedErrorConstainsStatusCode(t *testing.T) { |
| 176 |
e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", &http.Response{ |
| 177 |
StatusCode: http.StatusBadRequest, |
| 178 |
Status: http.StatusText(http.StatusBadRequest)}, "message") |
| 179 |
|
| 180 |
if matched, _ := regexp.MatchString(`.*400.*`, e.Error()); !matched { |
| 181 |
t.Fatalf("autorest: Error#String failed to include Status Code -- expected %v, received %v", |
| 182 |
`.*400.*`, e.Error()) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
func TestDetailedErrorConstainsOriginal(t *testing.T) { |
| 187 |
e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message") |
| 188 |
|
| 189 |
if matched, _ := regexp.MatchString(`.*original.*`, e.Error()); !matched { |
| 190 |
t.Fatalf("autorest: Error#String failed to include Original error -- expected %v, received %v", |
| 191 |
`.*original.*`, e.Error()) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
func TestDetailedErrorSkipsOriginal(t *testing.T) { |
| 196 |
e := NewError("packageType", "method", "message") |
| 197 |
|
| 198 |
if matched, _ := regexp.MatchString(`.*Original.*`, e.Error()); matched { |
| 199 |
t.Fatalf("autorest: Error#String included missing Original error -- unexpected %v, received %v", |
| 200 |
`.*Original.*`, e.Error()) |
| 201 |
} |
| 202 |
} |