-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem_details_test.go
More file actions
53 lines (45 loc) · 1.24 KB
/
problem_details_test.go
File metadata and controls
53 lines (45 loc) · 1.24 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
package httpsuite
import (
"encoding/json"
"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 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"])
}
}