Skip to content

Commit 5f06dd6

Browse files
test(server): add unit test for HandlePing (#33)
Added a unit test for the HandlePing method in internal/server/server.go using httptest. This ensures the handler returns the correct status code, body, and headers. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent a2777bd commit 5f06dd6

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

internal/server/server_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package server
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
)
8+
9+
func TestHandlePing(t *testing.T) {
10+
// Create a minimal server instance since HandlePing doesn't use any fields
11+
srv := &Server{}
12+
13+
// Create a request
14+
req := httptest.NewRequest("GET", "/ping", nil)
15+
w := httptest.NewRecorder()
16+
17+
// Call the handler
18+
srv.HandlePing(w, req)
19+
20+
// Check the status code
21+
if status := w.Code; status != http.StatusOK {
22+
t.Errorf("handler returned wrong status code: got %v want %v",
23+
status, http.StatusOK)
24+
}
25+
26+
// Check the response body
27+
expected := "pong"
28+
if w.Body.String() != expected {
29+
t.Errorf("handler returned unexpected body: got %v want %v",
30+
w.Body.String(), expected)
31+
}
32+
33+
// Check the headers
34+
if origin := w.Header().Get("Access-Control-Allow-Origin"); origin != "*" {
35+
t.Errorf("handler returned wrong Access-Control-Allow-Origin: got %v want %v",
36+
origin, "*")
37+
}
38+
39+
if contentType := w.Header().Get("Content-Type"); contentType != "text/plain" {
40+
t.Errorf("handler returned wrong Content-Type: got %v want %v",
41+
contentType, "text/plain")
42+
}
43+
}

0 commit comments

Comments
 (0)