-
Notifications
You must be signed in to change notification settings - Fork 0
/
problemdetail_test.go
214 lines (175 loc) · 8.13 KB
/
problemdetail_test.go
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package problemdetail_test
import (
"errors"
"fmt"
"net/http/httptest"
"strings"
"testing"
"github.com/josestg/problemdetail"
)
// BalanceProblemDetail is a sample problem detail with extension by embedding ProblemDetail.
type BalanceProblemDetail struct {
*problemdetail.ProblemDetail
Balance int32 `json:"balance" xml:"balance"`
Accounts []string `json:"accounts" xml:"accounts"`
}
func TestWriteJSON_WithExtension(t *testing.T) {
data := BalanceProblemDetail{
ProblemDetail: problemdetail.New(
"https://example.com/probs/out-of-credit",
problemdetail.WithDetail("Your current balance is 30, but that costs 50."),
problemdetail.WithInstance("/account/12345/abc"),
problemdetail.WithTitle("You do not have enough credit."),
),
Balance: 30,
Accounts: []string{"/account/12345", "/account/67890"},
}
rec := httptest.NewRecorder()
err := problemdetail.WriteJSON(rec, &data, 403)
expectTrue(t, err == nil)
expRaw := `{"type":"https://example.com/probs/out-of-credit","title":"You do not have enough credit.","status":403,"detail":"Your current balance is 30, but that costs 50.","instance":"/account/12345/abc","balance":30,"accounts":["/account/12345","/account/67890"]}`
gotRaw := strings.TrimSpace(rec.Body.String())
expectTrue(t, gotRaw == expRaw)
expectTrue(t, rec.Code == 403)
expectTrue(t, rec.Header().Get("Content-Type") == "application/problem+json; charset=utf-8")
}
func TestWriteJSON_WithUntyped(t *testing.T) {
data := problemdetail.New(problemdetail.Untyped, problemdetail.WithValidateLevel(problemdetail.LStandard))
rec := httptest.NewRecorder()
err := problemdetail.WriteJSON(rec, data, 403)
expectTrue(t, err == nil)
expRaw := `{"type":"about:blank","title":"Forbidden","status":403}`
gotRaw := strings.TrimSpace(rec.Body.String())
expectTrue(t, gotRaw == expRaw)
expectTrue(t, rec.Code == 403)
expectTrue(t, rec.Header().Get("Content-Type") == "application/problem+json; charset=utf-8")
}
func TestWriteJSON_WithTyped(t *testing.T) {
data := problemdetail.New(
"https://example.com/probs/out-of-credit",
problemdetail.WithDetail("Your current balance is 30, but that costs 50."),
problemdetail.WithInstance("/account/12345/abc"),
problemdetail.WithTitle("You do not have enough credit."),
)
rec := httptest.NewRecorder()
err := problemdetail.WriteJSON(rec, data, 403)
expectTrue(t, err == nil)
expRaw := `{"type":"https://example.com/probs/out-of-credit","title":"You do not have enough credit.","status":403,"detail":"Your current balance is 30, but that costs 50.","instance":"/account/12345/abc"}`
gotRaw := strings.TrimSpace(rec.Body.String())
expectTrue(t, gotRaw == expRaw)
expectTrue(t, rec.Code == 403)
expectTrue(t, rec.Header().Get("Content-Type") == "application/problem+json; charset=utf-8")
}
func TestWriteJSON_WithStrictButAllEmpty(t *testing.T) {
data := problemdetail.New("")
rec := httptest.NewRecorder()
err := problemdetail.WriteJSON(rec, data, 0)
expectTrue(t, err != nil)
expectTrue(t, errors.Is(err, problemdetail.ErrTypeRequired))
expectTrue(t, errors.Is(err, problemdetail.ErrTitleRequired))
expectTrue(t, errors.Is(err, problemdetail.ErrStatusRequired))
expectTrue(t, errors.Is(err, problemdetail.ErrDetailRequired))
expectTrue(t, errors.Is(err, problemdetail.ErrInstanceRequired))
expectTrue(t, !errors.Is(err, problemdetail.ErrTypeFormat))
expectTrue(t, !errors.Is(err, problemdetail.ErrInstanceFormat))
}
func TestWriteJSON_WithTypedStrictButTypeAndInstanceInvalidFormat(t *testing.T) {
data := problemdetail.New("--not-\n/a/valid/uri--",
problemdetail.WithInstance("\n-not/a/valid/path\n"),
problemdetail.WithInstance("\n-not/a/valid/path\n"),
)
rec := httptest.NewRecorder()
err := problemdetail.WriteJSON(rec, data, 0)
expectTrue(t, err != nil)
expectTrue(t, errors.Is(err, problemdetail.ErrTypeFormat))
expectTrue(t, errors.Is(err, problemdetail.ErrInstanceFormat))
}
func TestWriteXML_WithExtension(t *testing.T) {
data := BalanceProblemDetail{
ProblemDetail: problemdetail.New(
"https://example.com/probs/out-of-credit",
problemdetail.WithDetail("Your current balance is 30, but that costs 50."),
problemdetail.WithInstance("/account/12345/abc"),
problemdetail.WithTitle("You do not have enough credit."),
),
Balance: 30,
Accounts: []string{"/account/12345", "/account/67890"},
}
rec := httptest.NewRecorder()
err := problemdetail.WriteXML(rec, &data, 403)
expectTrue(t, err == nil)
rawExp := `<problem xmlns="urn:ietf:rfc:7807"><type>https://example.com/probs/out-of-credit</type><title>You do not have enough credit.</title><status>403</status><detail>Your current balance is 30, but that costs 50.</detail><instance>/account/12345/abc</instance><balance>30</balance><accounts>/account/12345</accounts><accounts>/account/67890</accounts></problem>`
rawGot := strings.TrimSpace(rec.Body.String())
expectTrue(t, rawGot == rawExp)
expectTrue(t, rec.Code == 403)
expectTrue(t, rec.Header().Get("Content-Type") == "application/problem+xml; charset=utf-8")
}
func TestWriteXML_WithUntyped(t *testing.T) {
data := problemdetail.New(problemdetail.Untyped, problemdetail.WithValidateLevel(problemdetail.LStandard))
rec := httptest.NewRecorder()
err := problemdetail.WriteXML(rec, data, 403)
expectTrue(t, err == nil)
rawExp := `<problem xmlns="urn:ietf:rfc:7807"><type>about:blank</type><title>Forbidden</title><status>403</status></problem>`
rawGot := strings.TrimSpace(rec.Body.String())
expectTrue(t, rawGot == rawExp)
expectTrue(t, rec.Code == 403)
expectTrue(t, rec.Header().Get("Content-Type") == "application/problem+xml; charset=utf-8")
}
func TestWriteXML_WithTyped(t *testing.T) {
data := problemdetail.New(
"https://example.com/probs/out-of-credit",
problemdetail.WithDetail("Your current balance is 30, but that costs 50."),
problemdetail.WithInstance("/account/12345/abc"),
problemdetail.WithTitle("You do not have enough credit."),
)
rec := httptest.NewRecorder()
err := problemdetail.WriteXML(rec, data, 403)
expectTrue(t, err == nil)
rawExp := `<problem xmlns="urn:ietf:rfc:7807"><type>https://example.com/probs/out-of-credit</type><title>You do not have enough credit.</title><status>403</status><detail>Your current balance is 30, but that costs 50.</detail><instance>/account/12345/abc</instance></problem>`
rawGot := strings.TrimSpace(rec.Body.String())
expectTrue(t, rawGot == rawExp)
expectTrue(t, rec.Code == 403)
expectTrue(t, rec.Header().Get("Content-Type") == "application/problem+xml; charset=utf-8")
}
func TestWriteXML_WithStrictButAllEmpty(t *testing.T) {
data := problemdetail.New("")
rec := httptest.NewRecorder()
err := problemdetail.WriteXML(rec, data, 0)
expectTrue(t, err != nil)
expectTrue(t, errors.Is(err, problemdetail.ErrTypeRequired))
expectTrue(t, errors.Is(err, problemdetail.ErrTitleRequired))
expectTrue(t, errors.Is(err, problemdetail.ErrStatusRequired))
expectTrue(t, errors.Is(err, problemdetail.ErrDetailRequired))
expectTrue(t, errors.Is(err, problemdetail.ErrInstanceRequired))
expectTrue(t, !errors.Is(err, problemdetail.ErrTypeFormat))
expectTrue(t, !errors.Is(err, problemdetail.ErrInstanceFormat))
}
func TestWriteXML_WithTypedStrictButTypeAndInstanceInvalidFormat(t *testing.T) {
data := problemdetail.New("--not-\n/a/valid/uri--",
problemdetail.WithInstance("\n-not/a/valid/path\n"),
problemdetail.WithInstance("\n-not/a/valid/path\n"),
)
rec := httptest.NewRecorder()
err := problemdetail.WriteXML(rec, data, 0)
expectTrue(t, err != nil)
expectTrue(t, errors.Is(err, problemdetail.ErrTypeFormat))
expectTrue(t, errors.Is(err, problemdetail.ErrInstanceFormat))
}
func TestProblemDetail_Error(t *testing.T) {
pd := problemdetail.New("https://example.com/probs/out-of-credit",
problemdetail.WithDetail("Your current balance is 30, but that costs 50."),
problemdetail.WithInstance("/account/12345/abc"),
problemdetail.WithTitle("You do not have enough credit."),
)
err := fmt.Errorf("error: %w", pd)
var pdErr *problemdetail.ProblemDetail
expectTrue(t, errors.As(err, &pdErr))
expectTrue(t, pdErr == pd)
expectTrue(t, pdErr.Error() == "problem detail: https://example.com/probs/out-of-credit")
}
func expectTrue(t *testing.T, b bool) {
t.Helper()
if !b {
t.Fatal("expected true, got false")
}
}