-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from MinaFoundation/health-ep
PM-1939 - Health endpoint
- Loading branch information
Showing
13 changed files
with
139 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ on: | |
push: | ||
branches: | ||
- main | ||
tags: | ||
- '*' | ||
# tags: | ||
# - '*' | ||
pull_request: | ||
branches: | ||
- main | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package delegation_backend | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
// HealthStatus represents the JSON response structure for the /health endpoint | ||
type HealthStatus struct { | ||
Status string `json:"status"` | ||
} | ||
|
||
// HealthHandler handles the /health endpoint, checking if the application is ready. | ||
func HealthHandler(isReady func() bool) http.HandlerFunc { | ||
return func(rw http.ResponseWriter, r *http.Request) { | ||
if isReady() { | ||
rw.WriteHeader(http.StatusOK) | ||
json.NewEncoder(rw).Encode(HealthStatus{Status: "ok"}) | ||
} else { | ||
rw.WriteHeader(http.StatusServiceUnavailable) | ||
json.NewEncoder(rw).Encode(HealthStatus{Status: "unavailable"}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package delegation_backend | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
// Mock App structure with IsReady flag | ||
type MockApp struct { | ||
IsReady bool | ||
} | ||
|
||
// TestHealthEndpointBeforeReady tests the /health endpoint before the application is ready. | ||
func TestHealthEndpointBeforeReady(t *testing.T) { | ||
app := &MockApp{IsReady: false} | ||
|
||
req, err := http.NewRequest("GET", "/health", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
handler := HealthHandler(func() bool { return app.IsReady }) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusServiceUnavailable { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusServiceUnavailable) | ||
} | ||
|
||
} | ||
|
||
// TestHealthEndpointAfterReady tests the /health endpoint after the application is ready. | ||
func TestHealthEndpointAfterReady(t *testing.T) { | ||
app := &MockApp{IsReady: true} | ||
|
||
req, err := http.NewRequest("GET", "/health", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
handler := HealthHandler(func() bool { return app.IsReady }) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusOK { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) | ||
} | ||
|
||
} | ||
|
||
// TestHealthEndpointTransition tests the /health endpoint during the transition from not ready to ready. | ||
func TestHealthEndpointTransition(t *testing.T) { | ||
app := &MockApp{IsReady: false} | ||
|
||
req, err := http.NewRequest("GET", "/health", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
handler := HealthHandler(func() bool { return app.IsReady }) | ||
|
||
// Initially not ready | ||
handler.ServeHTTP(rr, req) | ||
if status := rr.Code; status != http.StatusServiceUnavailable { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusServiceUnavailable) | ||
} | ||
|
||
// Simulate the application becoming ready | ||
app.IsReady = true | ||
rr = httptest.NewRecorder() // Reset the recorder | ||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusOK { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters