-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
debug_header_test.go
111 lines (92 loc) · 2.54 KB
/
debug_header_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package hit_test
import (
"bytes"
"encoding/json"
"testing"
. "github.com/Eun/go-hit"
"io/ioutil"
"strings"
"github.com/lunixbochs/vtclean"
"github.com/stretchr/testify/require"
)
func TestDebugRequestHeader(t *testing.T) {
s := EchoServer()
defer s.Close()
t.Run("without header", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
Test(t,
Post(s.URL),
Send().Headers("X-Header1").Add("Foo"),
Send().Headers("X-Header2").Add("Bar"),
Fdebug(buf).Request().Headers(),
)
var m map[string]interface{}
require.NoError(t, json.NewDecoder(vtclean.NewReader(buf, false)).Decode(&m))
require.Len(t, m, 2)
require.Equal(t, "Foo", m["X-Header1"])
require.Equal(t, "Bar", m["X-Header2"])
})
t.Run("with header", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
Test(t,
Post(s.URL),
Send().Headers("X-Header1").Add("Foo"),
Send().Headers("X-Header2").Add("Bar"),
Fdebug(buf).Request().Headers("X-Header1"),
)
b, err := ioutil.ReadAll(vtclean.NewReader(buf, false))
require.NoError(t, err)
require.Equal(t, "Foo", strings.TrimSpace(string(b)))
})
t.Run("clear", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
Test(t,
Post(s.URL),
Send().Headers("X-Header1").Add("Foo"),
Send().Headers("X-Header2").Add("Bar"),
Expect().Body().String().Equal("Hello World"),
Fdebug(buf).Request().Headers(),
Clear().Expect(),
)
})
}
func TestDebugResponseHeader(t *testing.T) {
s := EchoServer()
defer s.Close()
t.Run("without header", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
Test(t,
Post(s.URL),
Send().Headers("X-Header1").Add("Foo"),
Send().Headers("X-Header2").Add("Bar"),
Fdebug(buf).Response().Headers(),
)
var m map[string]interface{}
require.NoError(t, json.NewDecoder(vtclean.NewReader(buf, false)).Decode(&m))
require.Equal(t, "Foo", m["X-Header1"])
require.Equal(t, "Bar", m["X-Header2"])
})
t.Run("with header", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
Test(t,
Post(s.URL),
Send().Headers("X-Header1").Add("Foo"),
Send().Headers("X-Header2").Add("Bar"),
Fdebug(buf).Response().Headers("X-Header1"),
)
b, err := ioutil.ReadAll(vtclean.NewReader(buf, false))
require.NoError(t, err)
require.Equal(t, "Foo", strings.TrimSpace(string(b)))
})
t.Run("clear", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
Test(t,
Post(s.URL),
Send().Headers("X-Header1").Add("Foo"),
Send().Headers("X-Header2").Add("Bar"),
Expect().Body().String().Equal("Hello World"),
Fdebug(buf).Response().Headers(),
Clear().Expect(),
)
})
}