-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpinfo_test.go
70 lines (57 loc) · 2.13 KB
/
httpinfo_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
package httpinfo
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestFromRequest(t *testing.T) {
var request = httptest.NewRequest(http.MethodGet, "/", nil)
assert.Nil(t, fromRequest(request))
Record()(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
assert.NotNil(t, fromRequest(r))
})).ServeHTTP(httptest.NewRecorder(), request)
}
func TestIsUsed(t *testing.T) {
var request = httptest.NewRequest(http.MethodGet, "/", nil)
assert.False(t, IsUsed(request))
Record()(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
assert.True(t, IsUsed(r))
})).ServeHTTP(httptest.NewRecorder(), request)
}
func TestStatus(t *testing.T) {
Record()(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
var status = http.StatusAlreadyReported
http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(status)
}).ServeHTTP(rw, r)
assert.Equal(t, status, Status(r))
})).ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
}
func TestExecutionTime(t *testing.T) {
if testing.Short() {
t.Skip()
return
}
Record()(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
time.Sleep(500 * time.Millisecond)
}).ServeHTTP(rw, r)
assert.True(t, ExecutionTime(r) > 500*time.Millisecond)
})).ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
}
func TestContentLength(t *testing.T) {
Record()(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
var hello = []byte("hello")
http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
rw.Write(hello) // nolint: errcheck, gosec
}).ServeHTTP(rw, r)
assert.Equal(t, len(hello), ContentLength(r))
})).ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
}
func TestRoute(t *testing.T) {
Record()(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method+" "+r.URL.Path, Route(r))
})).ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
}