forked from andreia-oca/ci_cd_lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
35 lines (28 loc) · 881 Bytes
/
main_test.go
File metadata and controls
35 lines (28 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestRootHandler(t *testing.T) {
// Create a new request
req := httptest.NewRequest(http.MethodGet, "/", nil)
// Create a ResponseRecorder to record the response
rr := httptest.NewRecorder()
// Call the handler directly
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Replicating the handler from main.go
io.WriteString(w, "Hello from Go HTTP server!\n")
})
handler.ServeHTTP(rr, req)
// Check the status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
// Check the response body
expected := "Hello from Go HTTP server!\n"
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
}