forked from microsoft/kiota-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultipartBodyTests.cs
More file actions
128 lines (99 loc) · 4.86 KB
/
Copy pathMultipartBodyTests.cs
File metadata and controls
128 lines (99 loc) · 4.86 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
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
125
126
127
128
using System.IO;
using Microsoft.Kiota.Abstractions.Serialization;
using Moq;
using Xunit;
namespace Microsoft.Kiota.Abstractions.Tests;
public class MultipartBodyTests
{
[Fact]
public void KeepsFilename()
{
var writerMock = new Mock<ISerializationWriter>();
var jsonWriterMock = new Mock<ISerializationWriter>();
var requestAdapterMock = new Mock<IRequestAdapter>();
var serializationFactoryMock = new Mock<ISerializationWriterFactory>();
var body = new MultipartBody();
jsonWriterMock.Setup(w => w.WriteStringValue("", "fileContent"));
using var ms = new MemoryStream();
using var sr = new StreamWriter(ms);
sr.Write("fileContent");
sr.Flush();
jsonWriterMock.Setup(w => w.GetSerializedContent()).Returns(ms);
serializationFactoryMock
.Setup(r => r.GetSerializationWriter("application/json"))
.Returns(jsonWriterMock.Object);
requestAdapterMock
.Setup(r => r.SerializationWriterFactory)
.Returns(serializationFactoryMock.Object);
body.RequestAdapter = requestAdapterMock.Object;
writerMock.Setup(w => w.WriteStringValue("", "--" + body.Boundary));
writerMock.Setup(w => w.WriteStringValue("Content-Type", "application/json"));
writerMock.Setup(w => w.WriteStringValue("Content-Disposition", "form-data; name=\"file\"; filename=\"file.json\""));
writerMock.Setup(w => w.WriteByteArrayValue("", ms.ToArray()));
writerMock.Setup(w => w.WriteStringValue("", ""));
writerMock.Setup(w => w.WriteStringValue("", "--" + body.Boundary + "--"));
body.AddOrReplacePart("file", "application/json", "fileContent", "file.json");
body.Serialize(writerMock.Object);
writerMock.VerifyAll();
requestAdapterMock.VerifyAll();
serializationFactoryMock.VerifyAll();
}
[Fact]
public void WorksWithoutFilename()
{
var writerMock = new Mock<ISerializationWriter>();
var jsonWriterMock = new Mock<ISerializationWriter>();
var requestAdapterMock = new Mock<IRequestAdapter>();
var serializationFactoryMock = new Mock<ISerializationWriterFactory>();
var body = new MultipartBody();
jsonWriterMock.Setup(w => w.WriteStringValue("", "fileContent"));
using var ms = new MemoryStream();
using var sr = new StreamWriter(ms);
sr.Write("fileContent");
sr.Flush();
jsonWriterMock.Setup(w => w.GetSerializedContent()).Returns(ms);
serializationFactoryMock
.Setup(r => r.GetSerializationWriter("application/json"))
.Returns(jsonWriterMock.Object);
requestAdapterMock
.Setup(r => r.SerializationWriterFactory)
.Returns(serializationFactoryMock.Object);
body.RequestAdapter = requestAdapterMock.Object;
writerMock.Setup(w => w.WriteStringValue("", "--" + body.Boundary));
writerMock.Setup(w => w.WriteStringValue("Content-Type", "application/json"));
writerMock.Setup(w => w.WriteStringValue("Content-Disposition", "form-data; name=\"file\""));
writerMock.Setup(w => w.WriteByteArrayValue("", ms.ToArray()));
writerMock.Setup(w => w.WriteStringValue("", ""));
writerMock.Setup(w => w.WriteStringValue("", "--" + body.Boundary + "--"));
body.AddOrReplacePart("file", "application/json", "fileContent");
body.Serialize(writerMock.Object);
writerMock.VerifyAll();
requestAdapterMock.VerifyAll();
serializationFactoryMock.VerifyAll();
}
[Fact]
public void AllowsDuplicateEntries()
{
var body = new MultipartBody();
body.AddOrReplacePart("file", "application/json", "fileContent", "file.json");
body.AddOrReplacePart("file", "application/json", "fileContent2", "file2.json");
//Assert both files are stored in the body
Assert.Equal("fileContent", body.GetPartValue<string>("file", "file.json"));
Assert.Equal("fileContent2", body.GetPartValue<string>("file", "file2.json"));
//Assert part can be removed if fileName is specified
Assert.True(body.RemovePart("file", "file.json"));
//Assert file.json is removed and file2.json is still accessible
Assert.Null(body.GetPartValue<string>("file", "file.json"));
Assert.Equal("fileContent2", body.GetPartValue<string>("file", "file2.json"));
}
[Fact]
public void MultiPartBodyExistingGetAndRemoveStillWork()
{
var body = new MultipartBody();
body.AddOrReplacePart("file", "application/json", "fileContent", "file.json");
// existing usecase, file should still be able to be retreived
Assert.Equal("fileContent", body.GetPartValue<string>("file"));
// existing usecase, file should be sucesfully removed
Assert.True(body.RemovePart("file"));
}
}