-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathoutput_headers_test.go
More file actions
50 lines (38 loc) · 1.23 KB
/
output_headers_test.go
File metadata and controls
50 lines (38 loc) · 1.23 KB
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
//go:build go1.18
package main
import (
"io/ioutil"
"net/http"
"testing"
"github.com/bool64/httptestbench"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/swaggest/assertjson"
"github.com/swaggest/fchi"
"github.com/valyala/fasthttp"
)
// Benchmark_outputHeaders-4 41424 27054 ns/op 154 B:rcvd/op 77.0 B:sent/op 36963 rps 3641 B/op 35 allocs/op.
func Benchmark_outputHeaders(b *testing.B) {
r := NewRouter()
srv := fchi.NewTestServer(r)
defer srv.Close()
httptestbench.RoundTrip(b, 50, func(i int, req *fasthttp.Request) {
req.Header.SetMethod(http.MethodGet)
req.SetRequestURI(srv.URL + "/output-headers")
}, func(i int, resp *fasthttp.Response) bool {
return resp.StatusCode() == http.StatusOK
})
}
func Test_outputHeaders(t *testing.T) {
r := NewRouter()
srv := fchi.NewTestServer(r)
defer srv.Close()
resp, err := http.Get(srv.URL + "/output-headers")
require.NoError(t, err)
assert.Equal(t, resp.StatusCode, http.StatusOK)
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
assert.NoError(t, resp.Body.Close())
assert.Equal(t, "abc", resp.Header.Get("X-Header"))
assertjson.Equal(t, []byte(`{"inBody":"def"}`), body)
}