forked from leeprovoost/go-rest-api-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_test.go
40 lines (36 loc) · 1.27 KB
/
handlers_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
package main
import (
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHealthcheckHandler(t *testing.T) {
ctx := CreateContextForTestSetup()
r, _ := http.NewRequest("GET", "/healthcheck", nil)
w := httptest.NewRecorder()
makeHandler(ctx, HealthcheckHandler).ServeHTTP(w, r)
assert.Equal(t, http.StatusOK, w.Code, "they should be equal")
assert.Equal(t, "application/json; charset=UTF-8", w.HeaderMap["Content-Type"][0], "they should be equal")
// parse json body
var f interface{}
json.Unmarshal(w.Body.Bytes(), &f)
obj := f.(map[string]interface{})
assert.Equal(t, "go-rest-api-template", obj["appName"], "they should be equal")
assert.Equal(t, ctx.Version, obj["version"], "they should be equal")
}
func TestListUsersHandler(t *testing.T) {
ctx := CreateContextForTestSetup()
req, _ := http.NewRequest("GET", "/users", nil)
w := httptest.NewRecorder()
makeHandler(ctx, ListUsersHandler).ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "they should be equal")
assert.Equal(t, "application/json; charset=UTF-8", w.HeaderMap["Content-Type"][0], "they should be equal")
//parse json body
var f interface{}
json.Unmarshal(w.Body.Bytes(), &f)
m := f.(map[string]interface{})
log.Println(m["users"])
}