-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathext_test.go
More file actions
124 lines (107 loc) · 3.69 KB
/
Copy pathext_test.go
File metadata and controls
124 lines (107 loc) · 3.69 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
package problems
import (
"encoding/json"
"errors"
"net/http"
"testing"
)
type creditProblemExt struct {
Balance float64 `json:"balance"`
Accounts []string `json:"accounts"`
}
type creditProblem struct {
Problem
Balance float64 `json:"balance"`
Accounts []string `json:"accounts"`
}
var unAuthDetails = "you are unauthorized to access this resource"
func TestExtend(t *testing.T) {
tests := []struct {
name string
problem *ExtendedProblem[creditProblemExt]
expectJson string
}{
{
name: "should render properly with Extend",
problem: Extend[creditProblemExt](
New().
WithStatus(http.StatusUnauthorized).
WithDetail(unAuthDetails),
creditProblemExt{
Balance: 30,
Accounts: []string{"/account/12345", "/account/67890"},
},
),
expectJson: `{"type":"about:blank","title":"Unauthorized","status":401,"detail":"you are unauthorized to access this resource","extensions":{"balance":30,"accounts":["/account/12345","/account/67890"]}}`,
},
{
name: "should render properly with Extend",
problem: NewExt[creditProblemExt]().
WithStatus(http.StatusUnauthorized).
WithDetailf("account %d has insufficient funds", 12345).
WithExtension(creditProblemExt{
Balance: 30,
Accounts: []string{"/account/12345", "/account/67890"},
}),
expectJson: `{"type":"about:blank","title":"Unauthorized","status":401,"detail":"account 12345 has insufficient funds","extensions":{"balance":30,"accounts":["/account/12345","/account/67890"]}}`,
},
{
name: "should render properly with Error",
problem: ExtFromError[creditProblemExt](errors.New("an error occurred")).
WithStatus(http.StatusUnauthorized).
WithExtension(creditProblemExt{
Balance: 30,
Accounts: []string{"/account/12345", "/account/67890"},
}),
expectJson: `{"type":"about:blank","title":"Unauthorized","status":401,"detail":"an error occurred","extensions":{"balance":30,"accounts":["/account/12345","/account/67890"]}}`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if _, err := test.problem.Validate(); err != nil {
t.Errorf("problem is not valid: %s", err)
}
data, err := json.Marshal(test.problem)
if err != nil {
t.Errorf("failed to marshal extended problem as json: %s", err)
}
if string(data) != test.expectJson {
t.Errorf("extended problem does not match expectation:\ngot\n%s\nwant\n%s", string(data), test.expectJson)
}
if len(test.problem.Error()) == 0 {
t.Errorf("extended problem error message was empty")
}
})
}
}
func TestExtensionViaEmbedding(t *testing.T) {
problem := &creditProblem{
Problem: *New().
WithStatus(http.StatusUnauthorized).
WithDetail(unAuthDetails),
Balance: 30,
Accounts: []string{"/account/12345", "/account/67890"},
}
if _, err := problem.Validate(); err != nil {
t.Errorf("problem is not valid: %s", err)
}
data, err := json.Marshal(problem)
if err != nil {
t.Errorf("failed to marshal extended problem as json: %s", err)
}
expect := `{"type":"about:blank","title":"Unauthorized","status":401,"detail":"you are unauthorized to access this resource","balance":30,"accounts":["/account/12345","/account/67890"]}`
if string(data) != expect {
t.Errorf("extended problem does not match expectation:\ngot\n%s\nwant\n%s", string(data), expect)
}
}
func TestExtension(t *testing.T) {
problem := NewExt[creditProblemExt]().
WithType("https://example.com").
WithTitle("This is a custom title").
WithStatus(http.StatusBadRequest).
WithDetail("Here are some details").
WithInstance("https://example.com/errors/150")
if _, err := problem.Validate(); err != nil {
t.Errorf("extended problem is not valid but should be: %s", err)
}
}