diff --git a/problem_test.go b/problem_test.go index 857fe1e..97e91a2 100644 --- a/problem_test.go +++ b/problem_test.go @@ -28,6 +28,11 @@ func TestProblemJSON(t *testing.T) { problem: problem.Problem{}, golden: "empty.json", }, + { + name: "empty new", + problem: problem.New(), + golden: "empty-new.json", // Not the same as empty as `type` defaults to "about:blank" + }, { name: "no extra", problem: problem.Problem{ @@ -39,6 +44,17 @@ func TestProblemJSON(t *testing.T) { }, golden: "no-extra.json", }, + { + name: "no extra new", + problem: problem.New( + problem.Type("https://example.com/probs/out-of-credit"), + problem.Title("Not enough credit"), + problem.Detail("Your current balance is 30, but that costs 50"), + problem.Instance("/account/12345/msgs/abc"), + problem.Status(http.StatusBadRequest), + ), + golden: "no-extra.json", // Should serialize exactly the same + }, { name: "with extra", problem: problem.Problem{ @@ -54,6 +70,34 @@ func TestProblemJSON(t *testing.T) { }, golden: "with-extra.json", }, + { + name: "with extra new", + problem: problem.New( + problem.Type("https://example.com/probs/out-of-credit"), + problem.Title("Not enough credit"), + problem.Detail("Your current balance is 30, but that costs 50"), + problem.Instance("/account/12345/msgs/abc"), + problem.Status(http.StatusBadRequest), + problem.Extra("balance", 30), + problem.Extra("accounts", []string{"/accounts/12345", "/accounts/67890"}), + ), + golden: "with-extra.json", // Should serialize exactly the same + }, + { + name: "with extra map new", + problem: problem.New( + problem.Type("https://example.com/probs/out-of-credit"), + problem.Title("Not enough credit"), + problem.Detail("Your current balance is 30, but that costs 50"), + problem.Instance("/account/12345/msgs/abc"), + problem.Status(http.StatusBadRequest), + problem.ExtraMap(map[string]any{ + "balance": 30, + "accounts": []string{"/accounts/12345", "/accounts/67890"}, + }), + ), + golden: "with-extra.json", // Should serialize exactly the same + }, } for _, tt := range tests { diff --git a/testdata/empty-new.json b/testdata/empty-new.json new file mode 100644 index 0000000..638c77a --- /dev/null +++ b/testdata/empty-new.json @@ -0,0 +1,3 @@ +{ + "type": "about:blank" +} \ No newline at end of file