|
| 1 | +package problem_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/semmidev/problem" |
| 12 | +) |
| 13 | + |
| 14 | +func TestProblemSerialization(t *testing.T) { |
| 15 | + t.Run("marshaling", func(t *testing.T) { |
| 16 | + p := problem.New( |
| 17 | + problem.BadRequest, |
| 18 | + problem.WithDetail("Invalid input provided"), |
| 19 | + problem.WithInstance("/api/v1/users"), |
| 20 | + problem.WithExtension("trace_id", "req-1234"), |
| 21 | + problem.WithExtension("balance", 5000), |
| 22 | + ) |
| 23 | + |
| 24 | + data, err := json.Marshal(p) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("failed to marshal: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + var raw map[string]any |
| 30 | + if err := json.Unmarshal(data, &raw); err != nil { |
| 31 | + t.Fatalf("failed to unmarshal back to map: %v", err) |
| 32 | + } |
| 33 | + |
| 34 | + // Ensure top-level fields match expectations |
| 35 | + if raw["type"] != "about:blank" { |
| 36 | + t.Errorf("expected type 'about:blank', got %v", raw["type"]) |
| 37 | + } |
| 38 | + if raw["title"] != "Bad Request" { |
| 39 | + t.Errorf("expected title 'Bad Request', got %v", raw["title"]) |
| 40 | + } |
| 41 | + if raw["status"].(float64) != float64(http.StatusBadRequest) { |
| 42 | + t.Errorf("expected status %v, got %v", http.StatusBadRequest, raw["status"]) |
| 43 | + } |
| 44 | + if raw["detail"] != "Invalid input provided" { |
| 45 | + t.Errorf("expected detail, got %v", raw["detail"]) |
| 46 | + } |
| 47 | + if raw["instance"] != "/api/v1/users" { |
| 48 | + t.Errorf("expected instance, got %v", raw["instance"]) |
| 49 | + } |
| 50 | + if raw["trace_id"] != "req-1234" { |
| 51 | + t.Errorf("expected trace_id extension, got %v", raw["trace_id"]) |
| 52 | + } |
| 53 | + if raw["balance"].(float64) != 5000 { |
| 54 | + t.Errorf("expected balance extension, got %v", raw["balance"]) |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("unmarshaling", func(t *testing.T) { |
| 59 | + rawJSON := []byte(`{ |
| 60 | + "type": "https://example.com/probs/out-of-credit", |
| 61 | + "title": "You do not have enough credit.", |
| 62 | + "status": 403, |
| 63 | + "detail": "Your current balance is 30, but that costs 50.", |
| 64 | + "instance": "/account/12345/msgs/abc", |
| 65 | + "balance": 30, |
| 66 | + "accounts": ["/account/12345", "/account/67890"] |
| 67 | + }`) |
| 68 | + |
| 69 | + var p problem.Problem |
| 70 | + if err := json.Unmarshal(rawJSON, &p); err != nil { |
| 71 | + t.Fatalf("failed to unmarshal: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + if p.Type != "https://example.com/probs/out-of-credit" { |
| 75 | + t.Errorf("bad type: %s", p.Type) |
| 76 | + } |
| 77 | + if p.Title != "You do not have enough credit." { |
| 78 | + t.Errorf("bad title: %s", p.Title) |
| 79 | + } |
| 80 | + if p.Status != 403 { |
| 81 | + t.Errorf("bad status: %d", p.Status) |
| 82 | + } |
| 83 | + if p.Detail != "Your current balance is 30, but that costs 50." { |
| 84 | + t.Errorf("bad detail: %s", p.Detail) |
| 85 | + } |
| 86 | + if p.Instance != "/account/12345/msgs/abc" { |
| 87 | + t.Errorf("bad instance: %s", p.Instance) |
| 88 | + } |
| 89 | + |
| 90 | + // Extensions |
| 91 | + if p.Extensions == nil { |
| 92 | + t.Fatal("expected extensions to be initialized") |
| 93 | + } |
| 94 | + if p.Extensions["balance"].(float64) != 30 { |
| 95 | + t.Errorf("bad extension balance: %v", p.Extensions["balance"]) |
| 96 | + } |
| 97 | + |
| 98 | + // check accounts list length |
| 99 | + accounts, ok := p.Extensions["accounts"].([]any) |
| 100 | + if !ok || len(accounts) != 2 { |
| 101 | + t.Errorf("bad extension accounts array: %v", p.Extensions["accounts"]) |
| 102 | + } |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +func TestProblemErrorWrapping(t *testing.T) { |
| 107 | + origErr := errors.New("underlying generic network timeout") |
| 108 | + |
| 109 | + p := problem.Wrap(origErr, problem.GatewayTimeout, problem.WithDetail("Upstream service failed")) |
| 110 | + |
| 111 | + // Implement Standard Error() |
| 112 | + if p.Error() == "" { |
| 113 | + t.Error("error string should not be empty") |
| 114 | + } |
| 115 | + |
| 116 | + // Unwrap |
| 117 | + if unwrapped := p.Unwrap(); unwrapped != origErr { |
| 118 | + t.Errorf("expected to unwrap origErr, got: %v", unwrapped) |
| 119 | + } |
| 120 | + |
| 121 | + // errors.Is check |
| 122 | + if !errors.Is(p, origErr) { |
| 123 | + t.Error("errors.Is should return true for underlying error") |
| 124 | + } |
| 125 | + |
| 126 | + // errors.As check (getting Problem back out of an error) |
| 127 | + var asErr *problem.Problem |
| 128 | + if !errors.As(p, &asErr) { |
| 129 | + t.Error("errors.As should extract Problem from itself") |
| 130 | + } |
| 131 | + |
| 132 | + // Double-wrapping scenario (simulating fmt.Errorf) |
| 133 | + wrap2 := fmt.Errorf("adding more context: %w", p) |
| 134 | + if !errors.As(wrap2, &asErr) { |
| 135 | + t.Error("errors.As should extract Problem from a wrapped generic error") |
| 136 | + } |
| 137 | + |
| 138 | + // IsProblem convenience |
| 139 | + if extracted, ok := problem.IsProblem(wrap2); !ok || extracted == nil { |
| 140 | + t.Error("IsProblem should successfully extract the Problem from a wrapped error") |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func TestProblemHTTPWrite(t *testing.T) { |
| 145 | + p := problem.New(problem.InternalServerError, problem.WithDetail("DB connection failed")) |
| 146 | + |
| 147 | + rec := httptest.NewRecorder() |
| 148 | + if err := p.Write(rec); err != nil { |
| 149 | + t.Fatalf("Write should not return error: %v", err) |
| 150 | + } |
| 151 | + |
| 152 | + res := rec.Result() |
| 153 | + if res.StatusCode != 500 { |
| 154 | + t.Errorf("bad status code, expected 500 got %d", res.StatusCode) |
| 155 | + } |
| 156 | + |
| 157 | + contentType := res.Header.Get("Content-Type") |
| 158 | + if contentType != "application/problem+json; charset=utf-8" { |
| 159 | + t.Errorf("bad content type: %s", contentType) |
| 160 | + } |
| 161 | + |
| 162 | + if snif := res.Header.Get("X-Content-Type-Options"); snif != "nosniff" { |
| 163 | + t.Errorf("expected nosniff, got %v", snif) |
| 164 | + } |
| 165 | +} |
0 commit comments