generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add health check endpoint and tests (#30)
* Add health check endpoint and tests * remove comment * add timeStamp in response
- Loading branch information
1 parent
d0e93ef
commit 30329d1
Showing
3 changed files
with
67 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package controllers | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
) | ||
|
||
type HealthCheckResponse struct { | ||
Status string `json:"status"` | ||
Timestamp string `json:"timestamp"` | ||
} | ||
|
||
func HealthCheckHandler(response http.ResponseWriter, request *http.Request, params httprouter.Params) { | ||
response.Header().Set("Content-Type", "application/json") | ||
|
||
data := HealthCheckResponse{ | ||
Status: "ok", | ||
Timestamp: time.Now().Format(time.RFC3339), | ||
} | ||
|
||
if err := json.NewEncoder(response).Encode(data); err != nil { | ||
http.Error(response, `{"status":"error","message":"Internal Server Error"}`, http.StatusInternalServerError) | ||
return | ||
} | ||
} |
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,38 @@ | ||
package controllers_test | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Real-Dev-Squad/discord-service/controllers" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHealthCheckHandler(t *testing.T) { | ||
req, err := http.NewRequest("GET", "/health", nil) | ||
assert.NoError(t, err) | ||
|
||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
controllers.HealthCheckHandler(w, r, nil) | ||
}) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
assert.Equal(t, http.StatusOK, rr.Code) | ||
assert.Equal(t, "application/json", rr.Header().Get("Content-Type")) | ||
|
||
var response map[string]string | ||
err = json.Unmarshal(rr.Body.Bytes(), &response) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "ok", response["status"]) | ||
|
||
// Validate the timestamp format | ||
timestamp, exists := response["timestamp"] | ||
assert.True(t, exists, "timestamp field should exist") | ||
_, err = time.Parse(time.RFC3339, timestamp) | ||
assert.NoError(t, err, "timestamp should be in RFC3339 format") | ||
} |
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