-
Notifications
You must be signed in to change notification settings - Fork 11
/
encoder_test.go
87 lines (58 loc) · 1.9 KB
/
encoder_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
package tg
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestURLEncodedEncoder_WriteString(t *testing.T) {
buf := bytes.Buffer{}
encoder := newURLEncodedEncoder(&buf)
if assert.NoError(t, encoder.WriteString("a", "1")) {
assert.Equal(t, "a=1", buf.String())
}
if assert.NoError(t, encoder.WriteString("b", "2")) {
assert.Equal(t, "a=1&b=2", buf.String())
}
if assert.NoError(t, encoder.WriteString("c", "3")) {
assert.Equal(t, "a=1&b=2&c=3", buf.String())
}
}
func TestURLEncodedEncoder_WriteFile(t *testing.T) {
encoder := newURLEncodedEncoder(nil)
assert.Error(t, encoder.WriteFile("a", InputFile{}))
}
func TestURLEncodedEncoder_ContentType(t *testing.T) {
encoder := newURLEncodedEncoder(nil)
assert.Equal(t, "application/x-www-form-urlencoded", encoder.ContentType())
}
func TestURLEncodedEncoder_Close(t *testing.T) {
encoder := newURLEncodedEncoder(nil)
assert.NoError(t, encoder.Close())
}
func TestMultipartEncoder(t *testing.T) {
buf := bytes.Buffer{}
encoder := newMultipartEncoder(&buf)
assert.NoError(t, encoder.WriteString("a", "1"))
assert.NoError(t, encoder.WriteString("b", "2"))
assert.NoError(t, encoder.WriteString("c", "3"))
assert.NoError(t, encoder.Close())
assert.NotZero(t, buf.String())
}
func TestMultipartEncoder_WriteFile(t *testing.T) {
buf := bytes.Buffer{}
encoder := newMultipartEncoder(&buf)
file := NewInputFileBytes("test.txt", []byte("bla-bla-bla"))
assert.NoError(t, encoder.WriteFile("document", file))
assert.NoError(t, encoder.Close())
assert.NotZero(t, buf.String())
}
func TestMultipartEncoder_ContentType(t *testing.T) {
encoder := newMultipartEncoder(nil)
assert.True(t, strings.HasPrefix(encoder.ContentType(), "multipart/form-data; boundary"))
}
func TestMultipartEncoder_Close(t *testing.T) {
buf := bytes.Buffer{}
encoder := newMultipartEncoder(&buf)
assert.NoError(t, encoder.Close())
}