-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
53 lines (42 loc) · 1.16 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
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestGetRoot(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(getRoot)
handler.ServeHTTP(recorder, req)
if status := recorder.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
expected := "This is my website!\n"
if recorder.Body.String() != expected {
t.Errorf("Handler returned unexpected body: got %v want %v",
recorder.Body.String(), expected)
}
}
func TestGetHello(t *testing.T) {
req, err := http.NewRequest("GET", "/hello", nil)
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(getHello)
handler.ServeHTTP(recorder, req)
if status := recorder.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
expected := "Hello, There!\n"
if recorder.Body.String() != expected {
t.Errorf("Handler returned unexpected body: got %v want %v",
recorder.Body.String(), expected)
}
}