File: error_test.go

package info (click to toggle)
golang-github-azure-go-autorest 7.2.0%2BREALLY.7.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 536 kB
  • ctags: 925
  • sloc: makefile: 4
file content (188 lines) | stat: -rw-r--r-- 6,914 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package autorest

import (
	"fmt"
	"net/http"
	"reflect"
	"regexp"
	"testing"
)

func TestNewErrorWithError_AssignsPackageType(t *testing.T) {
	e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message")

	if e.PackageType != "packageType" {
		t.Errorf("autorest: Error failed to set package type -- expected %v, received %v", "packageType", e.PackageType)
	}
}

func TestNewErrorWithError_AssignsMethod(t *testing.T) {
	e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message")

	if e.Method != "method" {
		t.Errorf("autorest: Error failed to set method -- expected %v, received %v", "method", e.Method)
	}
}

func TestNewErrorWithError_AssignsMessage(t *testing.T) {
	e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message")

	if e.Message != "message" {
		t.Errorf("autorest: Error failed to set message -- expected %v, received %v", "message", e.Message)
	}
}

func TestNewErrorWithError_AssignsUndefinedStatusCodeIfRespNil(t *testing.T) {
	e := NewErrorWithError(nil, "packageType", "method", nil, "message")
	if e.StatusCode != UndefinedStatusCode {
		t.Errorf("autorest: Error failed to set status code -- expected %v, received %v", UndefinedStatusCode, e.StatusCode)
	}
}

func TestNewErrorWithError_AssignsStatusCode(t *testing.T) {
	e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", &http.Response{
		StatusCode: http.StatusBadRequest,
		Status:     http.StatusText(http.StatusBadRequest)}, "message")

	if e.StatusCode != http.StatusBadRequest {
		t.Errorf("autorest: Error failed to set status code -- expected %v, received %v", http.StatusBadRequest, e.StatusCode)
	}
}

func TestNewErrorWithError_AcceptsArgs(t *testing.T) {
	e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message %s", "arg")

	if matched, _ := regexp.MatchString(`.*arg.*`, e.Message); !matched {
		t.Errorf("autorest: Error failed to apply message arguments -- expected %v, received %v",
			`.*arg.*`, e.Message)
	}
}

func TestNewErrorWithError_AssignsError(t *testing.T) {
	err := fmt.Errorf("original")
	e := NewErrorWithError(err, "packageType", "method", nil, "message")

	if e.Original != err {
		t.Errorf("autorest: Error failed to set error -- expected %v, received %v", err, e.Original)
	}
}

func TestNewErrorWithResponse_ContainsStatusCode(t *testing.T) {
	e := NewErrorWithResponse("packageType", "method", &http.Response{
		StatusCode: http.StatusBadRequest,
		Status:     http.StatusText(http.StatusBadRequest)}, "message")

	if e.StatusCode != http.StatusBadRequest {
		t.Errorf("autorest: Error failed to set status code -- expected %v, received %v", http.StatusBadRequest, e.StatusCode)
	}
}

func TestNewErrorWithResponse_nilResponse_ReportsUndefinedStatusCode(t *testing.T) {
	e := NewErrorWithResponse("packageType", "method", nil, "message")

	if e.StatusCode != UndefinedStatusCode {
		t.Errorf("autorest: Error failed to set status code -- expected %v, received %v", UndefinedStatusCode, e.StatusCode)
	}
}

func TestNewErrorWithResponse_Forwards(t *testing.T) {
	e1 := NewError("packageType", "method", "message %s", "arg")
	e2 := NewErrorWithResponse("packageType", "method", nil, "message %s", "arg")

	if !reflect.DeepEqual(e1, e2) {
		t.Error("autorest: NewError did not return an error equivelent to NewErrorWithError")
	}
}

func TestNewErrorWithError_Forwards(t *testing.T) {
	e1 := NewError("packageType", "method", "message %s", "arg")
	e2 := NewErrorWithError(nil, "packageType", "method", nil, "message %s", "arg")

	if !reflect.DeepEqual(e1, e2) {
		t.Error("autorest: NewError did not return an error equivelent to NewErrorWithError")
	}
}

func TestNewErrorWithError_DoesNotWrapADetailedError(t *testing.T) {
	e1 := NewError("packageType1", "method1", "message1 %s", "arg1")
	e2 := NewErrorWithError(e1, "packageType2", "method2", nil, "message2 %s", "arg2")

	if !reflect.DeepEqual(e1, e2) {
		t.Errorf("autorest: NewErrorWithError incorrectly wrapped a DetailedError -- expected %v, received %v", e1, e2)
	}
}

func TestNewErrorWithError_WrapsAnError(t *testing.T) {
	e1 := fmt.Errorf("Inner Error")
	var e2 interface{} = NewErrorWithError(e1, "packageType", "method", nil, "message")

	if _, ok := e2.(DetailedError); !ok {
		t.Errorf("autorest: NewErrorWithError failed to wrap a standard error -- received %T", e2)
	}
}

func TestDetailedError(t *testing.T) {
	err := fmt.Errorf("original")
	e := NewErrorWithError(err, "packageType", "method", nil, "message")

	if matched, _ := regexp.MatchString(`.*original.*`, e.Error()); !matched {
		t.Errorf("autorest: Error#Error failed to return original error message -- expected %v, received %v",
			`.*original.*`, e.Error())
	}
}

func TestDetailedErrorConstainsPackageType(t *testing.T) {
	e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message")

	if matched, _ := regexp.MatchString(`.*packageType.*`, e.Error()); !matched {
		t.Errorf("autorest: Error#String failed to include PackageType -- expected %v, received %v",
			`.*packageType.*`, e.Error())
	}
}

func TestDetailedErrorConstainsMethod(t *testing.T) {
	e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message")

	if matched, _ := regexp.MatchString(`.*method.*`, e.Error()); !matched {
		t.Errorf("autorest: Error#String failed to include Method -- expected %v, received %v",
			`.*method.*`, e.Error())
	}
}

func TestDetailedErrorConstainsMessage(t *testing.T) {
	e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message")

	if matched, _ := regexp.MatchString(`.*message.*`, e.Error()); !matched {
		t.Errorf("autorest: Error#String failed to include Message -- expected %v, received %v",
			`.*message.*`, e.Error())
	}
}

func TestDetailedErrorConstainsStatusCode(t *testing.T) {
	e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", &http.Response{
		StatusCode: http.StatusBadRequest,
		Status:     http.StatusText(http.StatusBadRequest)}, "message")

	if matched, _ := regexp.MatchString(`.*400.*`, e.Error()); !matched {
		t.Errorf("autorest: Error#String failed to include Status Code -- expected %v, received %v",
			`.*400.*`, e.Error())
	}
}

func TestDetailedErrorConstainsOriginal(t *testing.T) {
	e := NewErrorWithError(fmt.Errorf("original"), "packageType", "method", nil, "message")

	if matched, _ := regexp.MatchString(`.*original.*`, e.Error()); !matched {
		t.Errorf("autorest: Error#String failed to include Original error -- expected %v, received %v",
			`.*original.*`, e.Error())
	}
}

func TestDetailedErrorSkipsOriginal(t *testing.T) {
	e := NewError("packageType", "method", "message")

	if matched, _ := regexp.MatchString(`.*Original.*`, e.Error()); matched {
		t.Errorf("autorest: Error#String included missing Original error -- unexpected %v, received %v",
			`.*Original.*`, e.Error())
	}
}