| 1 | package health_test |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/TwiN/health" |
| 10 | ) |
| 11 | |
| 12 | func TestHealthHandler_ServeHTTP(t *testing.T) { |
| 13 | defer health.SetHealthy() |
| 14 | type Scenario struct { |
| 15 | Name string |
| 16 | useJSON bool |
| 17 | status health.Status |
| 18 | reason string |
| 19 | expectedResponseBody string |
| 20 | expectedResponseCode int |
| 21 | } |
| 22 | scenarios := []Scenario{ |
| 23 | { |
| 24 | Name: "text-up", |
| 25 | useJSON: false, |
| 26 | status: health.Up, |
| 27 | expectedResponseBody: "UP", |
| 28 | expectedResponseCode: 200, |
| 29 | }, |
| 30 | { |
| 31 | Name: "text-up-reason", |
| 32 | useJSON: false, |
| 33 | status: health.Up, |
| 34 | reason: "reason", |
| 35 | expectedResponseBody: "UP: reason", |
| 36 | expectedResponseCode: 200, |
| 37 | }, |
| 38 | { |
| 39 | Name: "text-down", |
| 40 | useJSON: false, |
| 41 | status: health.Down, |
| 42 | expectedResponseBody: "DOWN", |
| 43 | expectedResponseCode: 500, |
| 44 | }, |
| 45 | { |
| 46 | Name: "text-down-reason", |
| 47 | useJSON: false, |
| 48 | status: health.Down, |
| 49 | reason: "reason", |
| 50 | expectedResponseBody: "DOWN: reason", |
| 51 | expectedResponseCode: 500, |
| 52 | }, |
| 53 | { |
| 54 | Name: "json-up", |
| 55 | useJSON: true, |
| 56 | status: health.Up, |
| 57 | expectedResponseBody: `{"status":"UP"}`, |
| 58 | expectedResponseCode: 200, |
| 59 | }, |
| 60 | { |
| 61 | Name: "json-up-reason", |
| 62 | useJSON: true, |
| 63 | status: health.Up, |
| 64 | reason: "Error", |
| 65 | expectedResponseBody: `{"status":"UP","reason":"Error"}`, |
| 66 | expectedResponseCode: 200, |
| 67 | }, |
| 68 | { |
| 69 | Name: "json-down", |
| 70 | useJSON: true, |
| 71 | status: health.Down, |
| 72 | expectedResponseBody: `{"status":"DOWN"}`, |
| 73 | expectedResponseCode: 500, |
| 74 | }, |
| 75 | { |
| 76 | Name: "json-down-reason", |
| 77 | useJSON: true, |
| 78 | status: health.Down, |
| 79 | reason: "Error", |
| 80 | expectedResponseBody: `{"status":"DOWN","reason":"Error"}`, |
| 81 | expectedResponseCode: 500, |
| 82 | }, |
| 83 | { |
| 84 | Name: "json-down-reason-with-quotes", |
| 85 | useJSON: true, |
| 86 | status: health.Down, |
| 87 | reason: `error "with" quotes`, |
| 88 | expectedResponseBody: `{"status":"DOWN","reason":"error \"with\" quotes"}`, |
| 89 | expectedResponseCode: 500, |
| 90 | }, |
| 91 | } |
| 92 | for _, scenario := range scenarios { |
| 93 | t.Run(scenario.Name, func(t *testing.T) { |
| 94 | handler := health.Handler().WithJSON(scenario.useJSON) |
| 95 | health.SetStatus(scenario.status) |
| 96 | health.SetReason(scenario.reason) |
| 97 | |
| 98 | request, _ := http.NewRequest("GET", "/health", http.NoBody) |
| 99 | responseRecorder := httptest.NewRecorder() |
| 100 | |
| 101 | handler.ServeHTTP(responseRecorder, request) |
| 102 | if responseRecorder.Code != scenario.expectedResponseCode { |
| 103 | t.Errorf("expected GET /health to return status code %d, got %d", scenario.expectedResponseCode, responseRecorder.Code) |
| 104 | } |
| 105 | body, _ := io.ReadAll(responseRecorder.Body) |
| 106 | if string(body) != scenario.expectedResponseBody { |
| 107 | t.Errorf("expected GET /health to return %s, got %s", scenario.expectedResponseBody, string(body)) |
| 108 | } |
| 109 | }) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func TestHealthHandler_GetResponseStatusCodeAndBody(t *testing.T) { |
| 114 | defer health.SetHealthy() |
| 115 | handler := health.Handler().WithJSON(true) |
| 116 | health.SetStatus(health.Up) |
| 117 | |
| 118 | statusCode, body := handler.GetResponseStatusCodeAndBody() |
| 119 | if statusCode != 200 { |
| 120 | t.Error("expected status code to be 200, got", statusCode) |
| 121 | } |
| 122 | if string(body) != `{"status":"UP"}` { |
| 123 | t.Error("expected body to be {\"status\":\"UP\"}, got", string(body)) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | func TestSetStatus(t *testing.T) { |
| 128 | defer health.SetHealthy() |
| 129 | health.SetStatus(health.Up) |
| 130 | if health.GetStatus() != health.Up { |
| 131 | t.Error("expected status to be 'Up', got", health.GetStatus()) |
| 132 | } |
| 133 | health.SetStatus(health.Down) |
| 134 | if health.GetStatus() != health.Down { |
| 135 | t.Error("expected status to be 'Down', got", health.GetStatus()) |
| 136 | } |
| 137 | health.SetStatus(health.Up) |
| 138 | } |
| 139 | |
| 140 | func TestSetReason(t *testing.T) { |
| 141 | defer health.SetHealthy() |
| 142 | health.SetReason("hello") |
| 143 | if health.GetReason() != "hello" { |
| 144 | t.Error("expected reason to be 'hello', got", health.GetReason()) |
| 145 | } |
| 146 | health.SetReason("world") |
| 147 | if health.GetReason() != "world" { |
| 148 | t.Error("expected reason to be 'world', got", health.GetReason()) |
| 149 | } |
| 150 | health.SetReason("") |
| 151 | if health.GetReason() != "" { |
| 152 | t.Error("expected reason to be '', got", health.GetReason()) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func TestSetStatusAndReason(t *testing.T) { |
| 157 | defer health.SetHealthy() |
| 158 | health.SetStatusAndReason(health.Down, "for what") |
| 159 | if health.GetStatus() != health.Down { |
| 160 | t.Error("expected status to be 'Down', got", health.GetStatus()) |
| 161 | } |
| 162 | if health.GetReason() != "for what" { |
| 163 | t.Error("expected reason to be 'hello', got", health.GetReason()) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func TestSetStatusAndResetReason(t *testing.T) { |
| 168 | defer health.SetHealthy() |
| 169 | health.SetStatusAndReason(health.Down, "for what") |
| 170 | if health.GetStatus() != health.Down { |
| 171 | t.Error("expected status to be 'Down', got", health.GetStatus()) |
| 172 | } |
| 173 | if health.GetReason() != "for what" { |
| 174 | t.Error("expected reason to be 'for what', got", health.GetReason()) |
| 175 | } |
| 176 | health.SetStatusAndResetReason(health.Up) |
| 177 | if health.GetStatus() != health.Up { |
| 178 | t.Error("expected status to be 'Up', got", health.GetStatus()) |
| 179 | } |
| 180 | if health.GetReason() != "" { |
| 181 | t.Error("expected reason to be '', got", health.GetReason()) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func TestSetHealthy(t *testing.T) { |
| 186 | defer health.SetHealthy() |
| 187 | health.SetStatusAndReason(health.Down, "for what") |
| 188 | if health.GetStatus() != health.Down { |
| 189 | t.Error("expected status to be 'Down', got", health.GetStatus()) |
| 190 | } |
| 191 | if health.GetReason() != "for what" { |
| 192 | t.Error("expected reason to be 'for what', got", health.GetReason()) |
| 193 | } |
| 194 | health.SetHealthy() |
| 195 | if health.GetStatus() != health.Up { |
| 196 | t.Error("expected status to be 'Up', got", health.GetStatus()) |
| 197 | } |
| 198 | if health.GetReason() != "" { |
| 199 | t.Error("expected reason to be '', got", health.GetReason()) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | func TestSetUnhealthy(t *testing.T) { |
| 204 | defer health.SetHealthy() |
| 205 | health.SetStatusAndReason(health.Up, "") |
| 206 | if health.GetStatus() != health.Up { |
| 207 | t.Error("expected status to be '', got", health.GetStatus()) |
| 208 | } |
| 209 | if health.GetReason() != "" { |
| 210 | t.Error("expected reason to be '', got", health.GetReason()) |
| 211 | } |
| 212 | health.SetUnhealthy("for what") |
| 213 | if health.GetStatus() != health.Down { |
| 214 | t.Error("expected status to be 'Down', got", health.GetStatus()) |
| 215 | } |
| 216 | if health.GetReason() != "for what" { |
| 217 | t.Error("expected reason to be 'for what', got", health.GetReason()) |
| 218 | } |
| 219 | } |