Something went wrong. Try again.
RFC-7807 Problem JSON in Go
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414package problem_test
import ( "encoding/json/jsontext" "encoding/json/v2" "flag" "io" "net/http" "net/http/httptest" "os" "path/filepath" "testing"
"go.followtheprocess.codes/problem" "go.followtheprocess.codes/test")
var update = flag.Bool("update", false, "Update golden files")
func TestProblemJSON(t *testing.T) { test.ColorEnabled(os.Getenv("CI") == "") // Force colour for diffs locally
tests := []struct { name string golden string problem problem.Problem }{ { name: "empty", 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{ Type: "https://example.com/probs/out-of-credit", Title: "Not enough credit", Detail: "Your current balance is 30, but that costs 50", Instance: "/account/12345/msgs/abc", Status: http.StatusBadRequest, }, 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{ Type: "https://example.com/probs/out-of-credit", Title: "Not enough credit", Detail: "Your current balance is 30, but that costs 50", Instance: "/account/12345/msgs/abc", Status: http.StatusBadRequest, Extra: map[string]any{ "balance": 30, "accounts": []string{"/accounts/12345", "/accounts/67890"}, }, }, 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 { t.Run(tt.name, func(t *testing.T) { got, err := json.Marshal(tt.problem, jsontext.WithIndent(" "), json.Deterministic(true)) test.Ok(t, err)
golden := filepath.Join("testdata", "TestProblemJSON", tt.golden)
if *update { test.Ok(t, os.WriteFile(golden, got, 0o644)) }
want, err := os.ReadFile(golden) test.Ok(t, err)
test.DiffBytes(t, got, want) }) }}
func TestJSONMethod(t *testing.T) { test.ColorEnabled(os.Getenv("CI") == "") // Force colour for diffs locally
tests := []struct { name string golden string problem problem.Problem }{ { name: "empty", 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{ Type: "https://example.com/probs/out-of-credit", Title: "Not enough credit", Detail: "Your current balance is 30, but that costs 50", Instance: "/account/12345/msgs/abc", Status: http.StatusBadRequest, }, 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{ Type: "https://example.com/probs/out-of-credit", Title: "Not enough credit", Detail: "Your current balance is 30, but that costs 50", Instance: "/account/12345/msgs/abc", Status: http.StatusBadRequest, Extra: map[string]any{ "balance": 30, "accounts": []string{"/accounts/12345", "/accounts/67890"}, }, }, 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 { t.Run(tt.name, func(t *testing.T) { got := tt.problem.JSON()
golden := filepath.Join("testdata", "TestJSONMethod", tt.golden)
if *update { test.Ok(t, os.WriteFile(golden, got, 0o644)) }
want, err := os.ReadFile(golden) test.Ok(t, err)
test.DiffBytes(t, got, want) }) }}
func TestRespond(t *testing.T) { tests := []struct { name string // Name of the test case body string // Expected JSON body options []problem.Option // Options to pass to Respond status int // Expected HTTP status on the response }{ { name: "empty", options: []problem.Option{}, status: http.StatusOK, // If w.WriteHeader isn't called explicitly, it defaults to 200 body: `{"type":"about:blank"}`, }, { name: "teapot", options: []problem.Option{ problem.Status(http.StatusTeapot), }, status: http.StatusTeapot, body: `{"type":"about:blank","status":418}`, }, { name: "type and detail", options: []problem.Option{ problem.Type("https://example.com/problems/invalid"), problem.Detail("That thing you provided was not valid"), problem.Status(http.StatusBadRequest), }, status: http.StatusBadRequest, body: `{"type":"https://example.com/problems/invalid","detail":"That thing you provided was not valid","status":400}`, }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { handler := func(w http.ResponseWriter, _ *http.Request) { problem.Respond(w, tt.options...) }
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil) w := httptest.NewRecorder() handler(w, req)
res := w.Result() defer res.Body.Close()
test.Equal(t, res.StatusCode, tt.status)
body, err := io.ReadAll(res.Body) test.Ok(t, err)
test.DiffBytes(t, body, []byte(tt.body)) }) }}
func TestEqual(t *testing.T) { tests := []struct { name string // Name of the test a, b problem.Problem // Problems to compare want bool // Whether they should be equal }{ { name: "both empty", a: problem.Problem{}, b: problem.Problem{}, want: true, }, { name: "a empty b not", a: problem.Problem{}, b: problem.Problem{ Type: "about:blank", }, want: false, }, { name: "all fields equal", a: problem.Problem{ Type: "https://example.com/probs/out-of-credit", Title: "Not enough credit", Detail: "Your current balance is 30, but that costs 50", Instance: "/account/12345/msgs/abc", Status: http.StatusBadRequest, }, b: problem.Problem{ Type: "https://example.com/probs/out-of-credit", Title: "Not enough credit", Detail: "Your current balance is 30, but that costs 50", Instance: "/account/12345/msgs/abc", Status: http.StatusBadRequest, }, want: true, }, { name: "differ by type", a: problem.Problem{Type: "about:blank"}, b: problem.Problem{Type: "https://example.com/probs/out-of-credit"}, want: false, }, { name: "differ by title", a: problem.Problem{Title: "Not enough credit"}, b: problem.Problem{Title: "Too much credit"}, want: false, }, { name: "differ by detail", a: problem.Problem{Detail: "Your current balance is 30, but that costs 50"}, b: problem.Problem{Detail: "Your current balance is 30, but that costs 60"}, want: false, }, { name: "differ by instance", a: problem.Problem{Instance: "/account/12345/msgs/abc"}, b: problem.Problem{Instance: "/account/67890/msgs/abc"}, want: false, }, { name: "differ by status", a: problem.Problem{Status: http.StatusBadRequest}, b: problem.Problem{Status: http.StatusTeapot}, want: false, }, { name: "equal extra with slice values", a: problem.Problem{ Extra: map[string]any{ "balance": 30, "accounts": []string{"/accounts/12345", "/accounts/67890"}, }, }, b: problem.Problem{ Extra: map[string]any{ "balance": 30, "accounts": []string{"/accounts/12345", "/accounts/67890"}, }, }, want: true, }, { name: "equal extra with nested map values", a: problem.Problem{ Extra: map[string]any{ "limits": map[string]any{"credit": 50, "overdraft": 10}, }, }, b: problem.Problem{ Extra: map[string]any{ "limits": map[string]any{"credit": 50, "overdraft": 10}, }, }, want: true, }, { name: "differ by extra value", a: problem.Problem{Extra: map[string]any{"balance": 30}}, b: problem.Problem{Extra: map[string]any{"balance": 40}}, want: false, }, { name: "differ by extra slice element", a: problem.Problem{Extra: map[string]any{"accounts": []string{"/accounts/12345"}}}, b: problem.Problem{Extra: map[string]any{"accounts": []string{"/accounts/67890"}}}, want: false, }, { name: "one extra nil other populated", a: problem.Problem{}, b: problem.Problem{Extra: map[string]any{"balance": 30}}, want: false, }, { name: "extra nil vs empty map", a: problem.Problem{Extra: nil}, b: problem.Problem{Extra: map[string]any{}}, want: false, }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := problem.Equal(tt.a, tt.b) test.Equal(t, got, tt.want) }) }}