forked from Eun/go-hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_body_test.go
124 lines (105 loc) · 2.34 KB
/
send_body_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
112
113
114
115
116
117
118
119
120
121
122
123
124
package hit_test
import (
"bytes"
"fmt"
"testing"
. "github.com/Eun/go-hit"
"github.com/stretchr/testify/require"
)
func TestSendBody_JSON(t *testing.T) {
s := EchoServer()
defer s.Close()
Test(t,
Post(s.URL),
Send().Body().JSON([]string{"A", "B"}),
Expect().Body().Equal(`["A","B"]`),
)
}
func TestSendBody(t *testing.T) {
s := EchoServer()
defer s.Close()
t.Run("bytes", func(t *testing.T) {
Test(t,
Post(s.URL),
Send().Body([]byte("Hello World")),
Expect().Body().Equal(`Hello World`),
)
})
t.Run("string", func(t *testing.T) {
Test(t,
Post(s.URL),
Send().Body("Hello World"),
Expect().Body().Equal(`Hello World`),
)
})
t.Run("reader", func(t *testing.T) {
Test(t,
Post(s.URL),
Send().Body(bytes.NewBufferString("Hello World")),
Expect().Body().Equal(`Hello World`),
)
})
t.Run("slice", func(t *testing.T) {
Test(t,
Post(s.URL),
Send().Body([]string{"A", "B"}),
Expect().Body().Equal(`["A","B"]`),
)
})
t.Run("int", func(t *testing.T) {
Test(t,
Post(s.URL),
Send().Body(8),
Expect().Body().Equal(`8`),
)
})
}
func TestSendBody_ModifyPreviousBody(t *testing.T) {
s := EchoServer()
defer s.Close()
Test(t,
Post(s.URL),
Send().Body("Hello"),
Send().Body(func(hit Hit) {
hit.Request().Body().SetString(fmt.Sprintf("%s World", hit.Request().Body().String()))
}),
Expect().Body().Equal(`Hello World`),
)
}
func TestSendBody_EmptyBody(t *testing.T) {
s := EchoServer()
defer s.Close()
Test(t,
Get(s.URL),
Send().Body(func(hit Hit) {
require.Empty(t, hit.Request().Body().String())
}),
)
}
func TestSendBody_Final(t *testing.T) {
s := EchoServer()
defer s.Close()
t.Run("Send().Body(value).JSON()", func(t *testing.T) {
ExpectError(t,
Do(Send().Body("Data").JSON(nil)),
PtrStr("only usable with Send().Body() not with Send().Body(value)"),
)
})
t.Run("Send().Body(value).Interface()", func(t *testing.T) {
ExpectError(t,
Do(Send().Body("Data").Interface(nil)),
PtrStr("only usable with Send().Body() not with Send().Body(value)"),
)
})
}
func TestSendBody_WithoutArgument(t *testing.T) {
s := EchoServer()
defer s.Close()
ExpectError(t,
Do(
Post(s.URL),
Send().Body(),
),
PtrStr("unable to run Send().Body() without an argument or without a chain. Please use Send().Body(something) or Send().Body().Something"),
)
}