-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
43 lines (36 loc) · 980 Bytes
/
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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func appInit() *http.ServeMux {
router := http.NewServeMux()
router.HandleFunc("/", HomeHandler)
return router
}
func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
func TestHomeHandlerStatus(t *testing.T) {
router := appInit()
w := performRequest(router, "GET", "/")
assert.Equal(t, http.StatusOK, w.Code)
}
func TestHomeHandlerBody(t *testing.T) {
router := appInit()
w := performRequest(router, "GET", "/")
assert.Equal(t, "Hello World!\n", w.Body.String())
}
func TestHomeHandlerMethods(t *testing.T) {
router := appInit()
methods := []string{"POST", "PUT", "DELETE", "OPTIONS", "TRACE", "PATCH"}
for _, m := range methods {
w := performRequest(router, m, "/")
assert.Equal(t, http.StatusOK, w.Code)
}
}