This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server_test.go
69 lines (59 loc) · 1.55 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
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func init() {
cfg = &config{}
}
func TestServerRoutes(t *testing.T) {
cases := []struct {
method, endpoint string
expectBody bool
body []byte
resStatus int
}{
{"GET", "/", false, nil, 404},
{"GET", "/healthcheck", false, nil, 200},
}
client := &http.Client{}
server := httptest.NewServer(NewServerRoutes())
for i, c := range cases {
req, err := http.NewRequest(c.method, server.URL+c.endpoint, bytes.NewReader(c.body))
if err != nil {
t.Errorf("case %d error creating request: %s", i, err.Error())
continue
}
res, err := client.Do(req)
if err != nil {
t.Errorf("case %d error performing request: %s", i, err.Error())
continue
}
if res.StatusCode != c.resStatus {
t.Errorf("case %d: %s - %s status code mismatch. expected: %d, got: %d", i, c.method, c.endpoint, c.resStatus, res.StatusCode)
continue
}
if c.expectBody {
env := &struct {
Meta map[string]interface{}
Data interface{}
Pagination map[string]interface{}
}{}
if err := json.NewDecoder(res.Body).Decode(env); err != nil {
t.Errorf("case %d: %s - %s error unmarshaling json envelope: %s", i, c.method, c.endpoint, err.Error())
continue
}
if env.Meta == nil {
t.Errorf("case %d: %s - %s doesn't have a meta field", i, c.method, c.endpoint)
continue
}
if env.Data == nil {
t.Errorf("case %d: %s - %s doesn't have a data field", i, c.method, c.endpoint)
continue
}
}
}
}