-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_test.go
57 lines (40 loc) · 1.25 KB
/
server_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
54
55
56
57
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestMikrowaysDemoServer(t *testing.T) {
t.Run("Any endpoint", func(t *testing.T) {
request := newGetRequest("/")
response := httptest.NewRecorder()
MikrowaysDemoServer(response, request)
assertResponseBody(t, response.Body.String(), "Hello World!!")
request = newGetRequest("/any-other-word")
response = httptest.NewRecorder()
MikrowaysDemoServer(response, request)
assertResponseBody(t, response.Body.String(), "Hello World!!")
})
t.Run("Health endpoint", func(t *testing.T) {
request := newGetRequest("/healthz")
response := httptest.NewRecorder()
MikrowaysDemoServer(response, request)
assertResponseBody(t, response.Body.String(), "Health OK!")
})
t.Run("Liveness endpoint", func(t *testing.T) {
request := newGetRequest("/liveness")
response := httptest.NewRecorder()
MikrowaysDemoServer(response, request)
assertResponseBody(t, response.Body.String(), "I am alive!!!")
})
}
func newGetRequest(name string) * http.Request {
req, _ := http.NewRequest(http.MethodGet, name, nil)
return req
}
func assertResponseBody(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("response body is wrong, got %q want %q", got, want)
}
}