-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
53 lines (42 loc) · 1.31 KB
/
main_test.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// main_test.go
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// TestHomeHandler tests the homeHandler function
func TestHomeHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(homeHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("homeHandler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected := "<title>Home</title>"
if !strings.Contains(rr.Body.String(), expected) {
t.Errorf("homeHandler returned unexpected body: got %v want to contain %v", rr.Body.String(), expected)
}
}
// TestAboutHandler tests the aboutHandler function
func TestAboutHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/about", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(aboutHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("aboutHandler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected := "<title>About</title>"
if !strings.Contains(rr.Body.String(), expected) {
t.Errorf("aboutHandler returned unexpected body: got %v want to contain %v", rr.Body.String(), expected)
}
}