-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
32 lines (26 loc) · 799 Bytes
/
main_test.go
File metadata and controls
32 lines (26 loc) · 799 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
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHelloHandler(t *testing.T) {
// Create a request to the /hello endpoint (GET method)
req, err := http.NewRequest("GET", "/hello", nil)
if err != nil {
t.Fatal(err)
}
// Create a ResponseRecorder to record the response
rr := httptest.NewRecorder()
// Call the helloHandler with the request and response recorder
helloHandler(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 DevOps Sri Lanka\n"
if body := rr.Body.String(); body != expected {
t.Errorf("Handler returned unexpected body: got %v want %v", body, expected)
}
}