-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgobernate_test.go
68 lines (56 loc) · 1.57 KB
/
gobernate_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
package gobernate_test
import (
"github.com/SebastiaanPasterkamp/gobernate"
"net/http"
"testing"
"time"
)
var testGobernateCases = []struct {
name string
path string
method string
expectedStatus int
expectedMime string
}{
{"GET /version", "/version", "GET", http.StatusOK, "application/json"},
{"POST /version", "/version", "POST", http.StatusMethodNotAllowed, ""},
{"GET /health", "/health", "GET", http.StatusOK, ""},
{"GET /readiness", "/readiness", "GET", http.StatusOK, ""},
{"GET /nonexistent", "/nonexistent", "GET", http.StatusNotFound, ""},
}
func TestGobernate(t *testing.T) {
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 100
g := gobernate.New("0", "gobernate-version", "1.0.0", "f00b4r", "now")
shutdown := g.Launch()
g.Ready()
time.Sleep(500 * time.Millisecond)
for _, tt := range testGobernateCases {
t.Run(tt.name, func(t *testing.T) {
var res *http.Response
var err error
switch tt.method {
case "GET":
res, err = http.Get(g.URL() + tt.path)
case "POST":
res, err = http.Post(g.URL()+tt.path, "text/plain", nil)
default:
t.Fatalf("Unknown method: %s", tt.method)
}
if err != nil {
t.Fatal(err)
}
if res.StatusCode != tt.expectedStatus {
t.Errorf("Status code for %s %s is wrong. Have: %d, want: %d.",
tt.method, tt.path, res.StatusCode, tt.expectedStatus)
}
})
}
g.Shutdown()
select {
case <-shutdown:
// expected
break
case <-time.After(1 * time.Second):
t.Errorf("Shutdown took too long. Expected termination within 1 second.")
}
}