forked from microsoft/kiota-abstractions-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipart_body_test.go
84 lines (75 loc) · 2.74 KB
/
multipart_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
package abstractions
import (
"testing"
"github.com/microsoft/kiota-abstractions-go/internal"
serialization "github.com/microsoft/kiota-abstractions-go/serialization"
"github.com/stretchr/testify/assert"
)
func TestMultipartIsParsable(t *testing.T) {
multipart := NewMultipartBody()
if _, ok := multipart.(serialization.Parsable); !ok {
t.Errorf("MultipartBody does not implement Parsable")
}
}
func TestMultipartImplementsDefensiveProgramming(t *testing.T) {
multipart := NewMultipartBody()
if err := multipart.AddOrReplacePart("", "foo", "bar"); err == nil {
t.Errorf("AddOrReplacePart should return an error when name is empty")
}
if err := multipart.AddOrReplacePart("foo", "", "bar"); err == nil {
t.Errorf("AddOrReplacePart should return an error when contentType is empty")
}
if err := multipart.AddOrReplacePart("foo", "bar", nil); err == nil {
t.Errorf("AddOrReplacePart should return an error when content is nil")
}
if err := multipart.RemovePart(""); err == nil {
t.Errorf("RemovePart should return an error when name is empty")
}
if _, err := multipart.GetPartValue(""); err == nil {
t.Errorf("GetPartValue should return an error when name is empty")
}
if err := multipart.Serialize(nil); err == nil {
t.Errorf("Serialize should return an error when writer is nil")
}
}
func TestItRequiresARequestAdapter(t *testing.T) {
multipart := NewMultipartBody()
mockSerializer := &internal.MockSerializer{}
if err := multipart.Serialize(mockSerializer); err == nil {
t.Errorf("Serialize should return an error when request adapter is nil")
}
}
func TestItRequiresParts(t *testing.T) {
multipart := NewMultipartBody()
mockSerializer := &internal.MockSerializer{}
mockRequestAdapter := &MockRequestAdapter{}
multipart.SetRequestAdapter(mockRequestAdapter)
if err := multipart.Serialize(mockSerializer); err == nil {
t.Errorf("Serialize should return an error when request adapter is nil")
}
}
func TestItAddsAPart(t *testing.T) {
multipart := NewMultipartBody()
mockRequestAdapter := &MockRequestAdapter{}
multipart.SetRequestAdapter(mockRequestAdapter)
err := multipart.AddOrReplacePart("foo", "bar", "baz")
assert.Nil(t, err)
value, err := multipart.GetPartValue("foo")
assert.Nil(t, err)
valueString, ok := value.(string)
assert.True(t, ok)
assert.Equal(t, "baz", valueString)
}
func TestItRemovesPart(t *testing.T) {
multipart := NewMultipartBody()
mockRequestAdapter := &MockRequestAdapter{}
multipart.SetRequestAdapter(mockRequestAdapter)
err := multipart.AddOrReplacePart("foo", "bar", "baz")
assert.Nil(t, err)
err = multipart.RemovePart("FOO")
assert.Nil(t, err)
value, err := multipart.GetPartValue("foo")
assert.Nil(t, err)
assert.Nil(t, value)
}
//serialize method is being tested in the serialization library