(t *testing.T)
| 51 | } |
| 52 | |
| 53 | func TestFromHeadersClampsAndParses(t *testing.T) { |
| 54 | cases := []struct { |
| 55 | name string |
| 56 | val string |
| 57 | set bool |
| 58 | want float64 |
| 59 | }{ |
| 60 | {"fresh", "1.0", true, 1.0}, |
| 61 | {"mid", "0.73", true, 0.73}, |
| 62 | {"depleted", "0", true, 0.0}, |
| 63 | {"clamped above 1", "1.05", true, 1.0}, |
| 64 | {"clamped below 0", "-0.1", true, 0.0}, |
| 65 | {"garbage", "abc", false, 0.0}, |
| 66 | {"empty", "", false, 0.0}, |
| 67 | } |
| 68 | for _, tc := range cases { |
| 69 | t.Run(tc.name, func(t *testing.T) { |
| 70 | h := http.Header{} |
| 71 | if tc.val != "" { |
| 72 | h.Set("X-Budget-Remaining", tc.val) |
| 73 | } |
| 74 | got := FromHeaders(h) |
| 75 | if got.Set != tc.set { |
| 76 | t.Fatalf("Set: got %v want %v", got.Set, tc.set) |
| 77 | } |
| 78 | if got.Remaining != tc.want { |
| 79 | t.Fatalf("Remaining: got %v want %v", got.Remaining, tc.want) |
| 80 | } |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // TestFromHeadersRejectsNaNAndInf: ParseFloat accepts NaN/±Inf without |
| 86 | // error and the [0,1] clamp is a no-op against NaN, so they'd render as |
nothing calls this directly
no test coverage detected