forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_test.go
More file actions
100 lines (78 loc) · 2.09 KB
/
error_test.go
File metadata and controls
100 lines (78 loc) · 2.09 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
package bitbucket
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDecodeError_ValidError(t *testing.T) {
t.Parallel()
errorMap := map[string]interface{}{
"error": map[string]interface{}{
"message": "Repository not found",
"fields": map[string][]string{},
},
}
err := DecodeError(errorMap)
assert.Error(t, err)
assert.Equal(t, "Repository not found", err.Error())
}
func TestDecodeError_EmptyMessage(t *testing.T) {
t.Parallel()
errorMap := map[string]interface{}{
"error": map[string]interface{}{
"message": "",
},
}
err := DecodeError(errorMap)
require.Error(t, err)
assert.Equal(t, "", err.Error())
}
func TestDecodeError_MissingErrorKey(t *testing.T) {
t.Parallel()
errorMap := map[string]interface{}{}
err := DecodeError(errorMap)
require.Error(t, err)
assert.Equal(t, "", err.Error())
}
func TestDecodeError_InvalidStructure(t *testing.T) {
t.Parallel()
errorMap := map[string]interface{}{
"error": "not a map",
}
err := DecodeError(errorMap)
require.Error(t, err)
assert.Contains(t, err.Error(), "expected a map")
}
func TestDecodeError_WithFields(t *testing.T) {
t.Parallel()
errorMap := map[string]interface{}{
"error": map[string]interface{}{
"message": "Bad request",
"fields": map[string][]string{
"username": {"Username is required"},
},
},
}
err := DecodeError(errorMap)
require.Error(t, err)
assert.Equal(t, "Bad request", err.Error())
}
func TestUnexpectedResponseStatusError_Error(t *testing.T) {
t.Parallel()
err := &UnexpectedResponseStatusError{
Status: "404 Not Found",
Body: []byte(`{"error": "not found"}`),
}
assert.Equal(t, "404 Not Found", err.Error())
}
func TestUnexpectedResponseStatusError_ErrorWithBody(t *testing.T) {
t.Parallel()
err := &UnexpectedResponseStatusError{
Status: "500 Internal Server Error",
Body: []byte(`{"error": "something broke"}`),
}
result := err.ErrorWithBody()
assert.Error(t, result)
assert.Contains(t, result.Error(), "500 Internal Server Error")
assert.Contains(t, result.Error(), `{"error": "something broke"}`)
}