-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathsetupandteardown_test.go
61 lines (53 loc) · 1.21 KB
/
setupandteardown_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
package main
import (
"fmt"
"net/http"
"os"
"testing"
"time"
)
func handlerOK(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok")
}
func handlerError(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "error")
}
var server *http.Server
func mySetupFunction() {
mux := http.NewServeMux()
mux.HandleFunc("/ok", handlerOK)
mux.HandleFunc("/error", handlerError)
server = &http.Server{
Addr: "127.0.0.1:4000",
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go server.ListenAndServe()
}
func myTeardownFunction() {
server.Close()
}
func TestHealthcheck(t *testing.T) {
urlBase := fmt.Sprintf("http://%s", server.Addr)
_, err := healthcheck(urlBase + "/ok")
if err != nil {
t.Errorf("Healthcheck: errors found when not expected -> %s", err)
}
ok, _ := healthcheck(urlBase + "/error")
if ok {
t.Errorf("Healthcheck: errors not found when expected")
}
_, err = healthcheck("/err")
if err == nil {
t.Errorf("Healthcheck: errors not found when expected")
}
}
func TestMain(m *testing.M) {
mySetupFunction()
retCode := m.Run()
myTeardownFunction()
os.Exit(retCode)
}