|
1 | 1 | package httpsuite |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func restoreProblemConfig(t *testing.T) { |
| 9 | + t.Helper() |
| 10 | + SetProblemConfig(NewProblemConfig()) |
| 11 | + t.Cleanup(func() { SetProblemConfig(NewProblemConfig()) }) |
| 12 | +} |
| 13 | + |
| 14 | +func TestSetProblemConfig(t *testing.T) { |
| 15 | + restoreProblemConfig(t) |
| 16 | + |
| 17 | + cfg := NewProblemConfig() |
| 18 | + cfg.BaseURL = "https://api.example.com/" |
| 19 | + cfg.ErrorTypePaths["unauthorized"] = "errors/unauthorized" |
| 20 | + SetProblemConfig(cfg) |
| 21 | + |
| 22 | + if got, want := GetProblemTypeURL("unauthorized"), "https://api.example.com/errors/unauthorized"; got != want { |
| 23 | + t.Fatalf("expected %q, got %q", want, got) |
| 24 | + } |
| 25 | + if got := DefaultProblemConfig().TypeURL("not_found_error"); got != "https://api.example.com/errors/not-found" { |
| 26 | + t.Fatalf("unexpected default type URL %q", got) |
| 27 | + } |
| 28 | + if got := NewBadRequestProblem("x").Type; got != "https://api.example.com/errors/bad-request" { |
| 29 | + t.Fatalf("helper did not use configured default, got %q", got) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestSetProblemConfigCopiesInput(t *testing.T) { |
| 34 | + restoreProblemConfig(t) |
| 35 | + |
| 36 | + cfg := NewProblemConfig() |
| 37 | + SetProblemConfig(cfg) |
| 38 | + cfg.ErrorTypePaths["validation_error"] = "/mutated" |
| 39 | + |
| 40 | + if got := GetProblemTypeURL("validation_error"); got != "/errors/validation-error" { |
| 41 | + t.Fatalf("expected default to be isolated from caller map, got %q", got) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestParseOptionsProblemsOverrideSetProblemConfig(t *testing.T) { |
| 46 | + restoreProblemConfig(t) |
| 47 | + |
| 48 | + cfg := NewProblemConfig() |
| 49 | + cfg.BaseURL = "https://global.example.com" |
| 50 | + SetProblemConfig(cfg) |
| 51 | + |
| 52 | + merged := mergeProblemConfig(&ProblemConfig{BaseURL: "https://local.example.com"}) |
| 53 | + if got, want := merged.TypeURL("validation_error"), "https://local.example.com/errors/validation-error"; got != want { |
| 54 | + t.Fatalf("expected %q, got %q", want, got) |
| 55 | + } |
| 56 | + merged = mergeProblemConfig(nil) |
| 57 | + if got, want := merged.TypeURL("validation_error"), "https://global.example.com/errors/validation-error"; got != want { |
| 58 | + t.Fatalf("expected %q, got %q", want, got) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestSetProblemConfigRestore(t *testing.T) { |
| 63 | + restoreProblemConfig(t) |
| 64 | + |
| 65 | + SetProblemConfig(ProblemConfig{BaseURL: "https://api.example.com"}) |
| 66 | + if got := GetProblemTypeURL("validation_error"); got != BlankURL { |
| 67 | + t.Fatalf("expected %q, got %q", BlankURL, got) |
| 68 | + } |
| 69 | + |
| 70 | + SetProblemConfig(NewProblemConfig()) |
| 71 | + if got := GetProblemTypeURL("validation_error"); got != "/errors/validation-error" { |
| 72 | + t.Fatalf("expected built-in default, got %q", got) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestSetProblemConfigConcurrent(t *testing.T) { |
| 77 | + restoreProblemConfig(t) |
| 78 | + |
| 79 | + var wg sync.WaitGroup |
| 80 | + for i := 0; i < 8; i++ { |
| 81 | + wg.Add(2) |
| 82 | + go func() { |
| 83 | + defer wg.Done() |
| 84 | + SetProblemConfig(NewProblemConfig()) |
| 85 | + }() |
| 86 | + go func() { |
| 87 | + defer wg.Done() |
| 88 | + _ = GetProblemTypeURL("validation_error") |
| 89 | + _ = DefaultProblemConfig() |
| 90 | + }() |
| 91 | + } |
| 92 | + wg.Wait() |
| 93 | +} |
4 | 94 |
|
5 | 95 | func TestProblemConfigTypeURL(t *testing.T) { |
6 | 96 | t.Parallel() |
|
0 commit comments