forked from rcrowley/go-tigertonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
56 lines (53 loc) · 1.17 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
package tigertonic
import (
"net/http"
"testing"
"time"
)
func TestServerCATLS(t *testing.T) {
s, err := NewTLSServer("", "test.crt", "test.key", NotFoundHandler{})
if nil != err {
t.Fatal(err)
}
s.CA("test.crt")
if nil == s.TLSConfig.Certificates || 1 != len(s.TLSConfig.Certificates) {
t.Fatal("no Certificates")
}
if nil == s.TLSConfig.RootCAs || 1 != len(s.TLSConfig.RootCAs.Subjects()) {
t.Fatal("no RootCAs")
}
}
func TestServerGracefulStop(t *testing.T) {
chT := make(chan time.Time, 1)
s := NewServer(
"127.0.0.1:8888",
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
time.Sleep(2 * time.Millisecond)
w.WriteHeader(http.StatusNoContent)
chT <- time.Now()
}),
)
go s.ListenAndServe()
chR := make(chan *http.Response, 1)
go func() {
rs, err := http.Get("http://127.0.0.1:8888/")
if nil != err {
t.Fatal(err)
}
chR <- rs
}()
time.Sleep(time.Millisecond)
s.Close()
now := time.Now()
then := <-chT
if now.Before(then) {
t.Fatal(now, "before", then)
}
if then.Add(10 * time.Millisecond).Before(now) {
t.Fatal("connection not closed quickly")
}
rs := <-chR
if http.StatusNoContent != rs.StatusCode {
t.Fatal(rs)
}
}