-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
164 lines (147 loc) · 4.51 KB
/
Copy patherrors_test.go
File metadata and controls
164 lines (147 loc) · 4.51 KB
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
package mysa
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestUnauthenticatedError(t *testing.T) {
t.Run("Error message without cause", func(t *testing.T) {
err := &UnauthenticatedError{Message: "session expired"}
if err.Error() != "session expired" {
t.Errorf("Error() = %v, want %v", err.Error(), "session expired")
}
})
t.Run("Error message with cause", func(t *testing.T) {
cause := errors.New("token invalid")
err := &UnauthenticatedError{Message: "session expired", Cause: cause}
if !strings.Contains(err.Error(), "session expired") {
t.Errorf("Error() should contain message")
}
if !strings.Contains(err.Error(), "token invalid") {
t.Errorf("Error() should contain cause")
}
})
t.Run("Unwrap returns cause", func(t *testing.T) {
cause := errors.New("token invalid")
err := &UnauthenticatedError{Message: "session expired", Cause: cause}
if err.Unwrap() != cause {
t.Errorf("Unwrap() = %v, want %v", err.Unwrap(), cause)
}
})
t.Run("Is ErrNotAuthenticated", func(t *testing.T) {
err := &UnauthenticatedError{Message: "test"}
if !errors.Is(err, ErrNotAuthenticated) {
t.Errorf("errors.Is(err, ErrNotAuthenticated) = false, want true")
}
})
}
func TestMysaApiError(t *testing.T) {
t.Run("Error message without body", func(t *testing.T) {
err := &MysaApiError{
StatusCode: 404,
Status: "404 Not Found",
URL: "https://api.example.com/test",
Body: "",
}
expected := "Mysa API error: https://api.example.com/test returned 404 (404 Not Found)"
if err.Error() != expected {
t.Errorf("Error() = %v, want %v", err.Error(), expected)
}
})
t.Run("Error message with body", func(t *testing.T) {
err := &MysaApiError{
StatusCode: 400,
Status: "400 Bad Request",
URL: "https://api.example.com/test",
Body: "invalid request",
}
if !strings.Contains(err.Error(), "invalid request") {
t.Errorf("Error() should contain body")
}
})
t.Run("IsNotFound", func(t *testing.T) {
err := &MysaApiError{StatusCode: 404}
if !err.IsNotFound() {
t.Errorf("IsNotFound() = false, want true")
}
err.StatusCode = 200
if err.IsNotFound() {
t.Errorf("IsNotFound() = true, want false")
}
})
t.Run("IsUnauthorized", func(t *testing.T) {
err := &MysaApiError{StatusCode: 401}
if !err.IsUnauthorized() {
t.Errorf("IsUnauthorized() = false, want true")
}
err.StatusCode = 200
if err.IsUnauthorized() {
t.Errorf("IsUnauthorized() = true, want false")
}
})
}
func TestNewMysaApiError(t *testing.T) {
// Create a test server that returns an error
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error": "bad request"}`))
}))
defer server.Close()
// Make a request
resp, err := http.Get(server.URL)
if err != nil {
t.Fatalf("failed to make request: %v", err)
}
defer resp.Body.Close()
apiErr := NewMysaApiError(resp)
if apiErr.StatusCode != 400 {
t.Errorf("StatusCode = %v, want 400", apiErr.StatusCode)
}
if !strings.Contains(apiErr.Body, "bad request") {
t.Errorf("Body = %v, want to contain 'bad request'", apiErr.Body)
}
}
func TestMqttPublishError(t *testing.T) {
cause := errors.New("connection lost")
err := &MqttPublishError{
Topic: "/v1/dev/abc/in",
Attempts: 3,
Cause: cause,
}
if !strings.Contains(err.Error(), "/v1/dev/abc/in") {
t.Errorf("Error() should contain topic")
}
if !strings.Contains(err.Error(), "3") {
t.Errorf("Error() should contain attempts")
}
if err.Unwrap() != cause {
t.Errorf("Unwrap() = %v, want %v", err.Unwrap(), cause)
}
}
func TestDeviceError(t *testing.T) {
t.Run("Error message without cause", func(t *testing.T) {
err := &DeviceError{DeviceId: "device-123", Message: "not found"}
expected := "device device-123: not found"
if err.Error() != expected {
t.Errorf("Error() = %v, want %v", err.Error(), expected)
}
})
t.Run("Error message with cause", func(t *testing.T) {
cause := errors.New("timeout")
err := &DeviceError{DeviceId: "device-123", Message: "failed", Cause: cause}
if !strings.Contains(err.Error(), "device-123") {
t.Errorf("Error() should contain device ID")
}
if !strings.Contains(err.Error(), "timeout") {
t.Errorf("Error() should contain cause")
}
})
t.Run("Is ErrDeviceNotFound", func(t *testing.T) {
err := &DeviceError{DeviceId: "test", Message: "not found"}
if !errors.Is(err, ErrDeviceNotFound) {
t.Errorf("errors.Is(err, ErrDeviceNotFound) = false, want true")
}
})
}