Repository navigation
Expand file tree
/
Copy pathproblem_details_test.go
More file actions
145 lines (123 loc) · 3.59 KB
/
Copy pathproblem_details_test.go
File metadata and controls
145 lines (123 loc) · 3.59 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
package httpsuite
import (
"encoding/json"
"reflect"
"testing"
)
func TestNewProblemDetailsDefaults(t *testing.T) {
t.Parallel()
details := NewProblemDetails(700, "", "", "broken")
if details.Status != 500 {
t.Fatalf("expected status 500, got %d", details.Status)
}
if details.Type != BlankURL {
t.Fatalf("expected type %q, got %q", BlankURL, details.Type)
}
if details.Title != "Internal Server Error" {
t.Fatalf("expected fallback title, got %q", details.Title)
}
}
func TestProblemDetailsJSONRoundTrip(t *testing.T) {
t.Parallel()
original := ProblemDetails{
Type: "https://example.com/errors/bad",
Title: "Bad Request",
Status: 400,
Detail: "broken",
Instance: "/things/1",
Extensions: map[string]interface{}{
"messageKey": "errors.bad",
"correlationId": "abc-123",
},
}
body, err := json.Marshal(original)
if err != nil {
t.Fatalf("marshal: %v", err)
}
var decoded ProblemDetails
if err := json.Unmarshal(body, &decoded); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if !reflect.DeepEqual(original, decoded) {
t.Fatalf("round trip mismatch:\nwant %#v\ngot %#v", original, decoded)
}
}
func TestProblemDetailsUnmarshalJSONNoExtensions(t *testing.T) {
t.Parallel()
decoded := ProblemDetails{Extensions: map[string]interface{}{"stale": true}}
if err := json.Unmarshal([]byte(`{"type":"about:blank","title":"T","status":404}`), &decoded); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if decoded.Extensions != nil {
t.Fatalf("expected nil extensions, got %#v", decoded.Extensions)
}
if decoded.Status != 404 || decoded.Title != "T" {
t.Fatalf("unexpected decode: %#v", decoded)
}
}
func TestProblemDetailsUnmarshalJSONNull(t *testing.T) {
t.Parallel()
decoded := ProblemDetails{Title: "keep", Status: 400}
if err := json.Unmarshal([]byte(`null`), &decoded); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if decoded.Title != "keep" || decoded.Status != 400 {
t.Fatalf("expected null to be a no-op, got %#v", decoded)
}
}
func TestProblemDetailsUnmarshalJSONInvalid(t *testing.T) {
t.Parallel()
var decoded ProblemDetails
if err := json.Unmarshal([]byte(`{"status":"nope"}`), &decoded); err == nil {
t.Fatal("expected error for wrong status type")
}
}
func TestProblemDetailsMarshalJSONFlattensExtensions(t *testing.T) {
t.Parallel()
problem := &ProblemDetails{
Type: BlankURL,
Title: "Bad Request",
Status: 400,
Detail: "broken",
Extensions: map[string]interface{}{
"trace_id": "trace-123",
"extensions": "ignored",
},
}
body, err := json.Marshal(problem)
if err != nil {
t.Fatalf("marshal problem details: %v", err)
}
var payload map[string]any
if err := json.Unmarshal(body, &payload); err != nil {
t.Fatalf("unmarshal problem details: %v", err)
}
if _, exists := payload["extensions"]; exists {
t.Fatalf("expected flattened extensions, got nested payload %q", string(body))
}
if payload["trace_id"] != "trace-123" {
t.Fatalf("expected trace_id extension, got %#v", payload["trace_id"])
}
}
func TestProblemDetailsMarshalJSONHasNoNestedExtensions(t *testing.T) {
t.Parallel()
body, err := json.Marshal(ProblemDetails{
Type: BlankURL,
Title: "Bad Request",
Status: 400,
Extensions: map[string]interface{}{"code": "E1"},
})
if err != nil {
t.Fatalf("marshal: %v", err)
}
var payload map[string]any
if err := json.Unmarshal(body, &payload); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if _, ok := payload["extensions"]; ok {
t.Fatalf("unexpected nested extensions key: %s", body)
}
if payload["code"] != "E1" {
t.Fatalf("expected flattened extension, got %s", body)
}
}