-
Notifications
You must be signed in to change notification settings - Fork 2
/
interfaces_test.go
91 lines (77 loc) · 1.7 KB
/
interfaces_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package compress
import (
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func testInterfaces(hf http.HandlerFunc, useServer bool) {
log.SetOutput(ioutil.Discard)
log.SetFlags(0)
defer log.SetOutput(os.Stderr)
h := HandleFunc(hf)
if useServer {
ts := httptest.NewServer(h)
defer ts.Close()
req, _ := http.NewRequest(http.MethodGet, ts.URL, nil)
req.Header.Set("Accept-Encoding", "gzip")
http.DefaultClient.Do(req)
} else {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept-Encoding", "gzip")
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
}
}
var handlerFuncCloseNotify = func(w http.ResponseWriter, r *http.Request) {
n, ok := w.(http.CloseNotifier)
if ok {
cn := n.CloseNotify()
go func() {
<-cn
}()
}
}
func TestCloseNotify(t *testing.T) {
testInterfaces(handlerFuncCloseNotify, true)
}
func TestNoCloseNotify(t *testing.T) {
testInterfaces(handlerFuncCloseNotify, false)
}
func TestFlush(t *testing.T) {
testInterfaces(func(w http.ResponseWriter, r *http.Request) {
f, ok := w.(http.Flusher)
if ok {
f.Flush()
}
}, true)
}
var handlerFuncHijack = func(w http.ResponseWriter, r *http.Request) {
h, ok := w.(http.Hijacker)
if ok {
conn, _, err := h.Hijack()
if err == nil {
conn.Close()
}
}
}
func TestHijack(t *testing.T) {
testInterfaces(handlerFuncHijack, true)
}
func TestNoHijack(t *testing.T) {
testInterfaces(handlerFuncHijack, false)
}
var handlerFuncPush = func(w http.ResponseWriter, r *http.Request) {
p, ok := w.(http.Pusher)
if ok {
p.Push("/", nil)
}
}
func TestPush(t *testing.T) {
// TODO: Set HTTP/2 test server.
}
func TestNoPush(t *testing.T) {
testInterfaces(handlerFuncPush, false)
}