Skip to content

Commit 48f4f78

Browse files
initial design
1 parent 27373e7 commit 48f4f78

File tree

53 files changed

+3027
-60
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3027
-60
lines changed

packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Payload/Multipart/MultipartTests.cs

+220
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System.Collections.Generic;
45
using System.IO;
56
using System.Threading.Tasks;
67
using NUnit.Framework;
78
using Payload.MultiPart;
9+
using Payload.MultiPart.Models;
810
using File = System.IO.File;
911

1012
namespace TestProjects.CadlRanch.Tests.Http.Payload.Multipart
@@ -28,6 +30,23 @@ public Task Basic() => Test(async (host) =>
2830
Assert.AreEqual(204, response.GetRawResponse().Status);
2931
});
3032

33+
[CadlRanchTest]
34+
public Task BasicConv() => Test(async (host) =>
35+
{
36+
var id = "123";
37+
await using var imageStream = File.OpenRead(SampleJpgPath);
38+
39+
var profileImage = new ProfileImageFileDetails(imageStream)
40+
{
41+
Filename = "profileImage",
42+
ContentType = "application/octet-stream"
43+
};
44+
var request = new MultiPartRequest(id, profileImage);
45+
var response = await new MultiPartClient(host, null).GetFormDataClient().BasicAsync(request);
46+
47+
Assert.AreEqual(204, response.GetRawResponse().Status);
48+
});
49+
3150
[CadlRanchTest]
3251
public Task JsonPart() => Test(async (host) =>
3352
{
@@ -42,6 +61,25 @@ public Task JsonPart() => Test(async (host) =>
4261
Assert.AreEqual(204, response.GetRawResponse().Status);
4362
});
4463

64+
[CadlRanchTest]
65+
public Task JsonPartConv() => Test(async (host) =>
66+
{
67+
Address address = new Address("X");
68+
69+
await using var imageStream = File.OpenRead(SampleJpgPath);
70+
var profileImage = new ProfileImageFileDetails(imageStream)
71+
{
72+
Filename = "profileImage",
73+
ContentType = "application/octet-stream"
74+
};
75+
76+
var request = new JsonPartRequest(address, profileImage);
77+
78+
var response = await new MultiPartClient(host, null).GetFormDataClient().JsonPartAsync(request);
79+
80+
Assert.AreEqual(204, response.GetRawResponse().Status);
81+
});
82+
4583
[CadlRanchTest]
4684
public Task CheckFileNameAndContentType() => Test(async (host) =>
4785
{
@@ -56,6 +94,24 @@ public Task CheckFileNameAndContentType() => Test(async (host) =>
5694
Assert.AreEqual(204, response.GetRawResponse().Status);
5795
});
5896

97+
[CadlRanchTest]
98+
public Task CheckFileNameAndContentTypeConv() => Test(async (host) =>
99+
{
100+
var id = "123";
101+
102+
await using var imageStream = File.OpenRead(SampleJpgPath);
103+
var profileImage = new ProfileImageFileDetails(imageStream)
104+
{
105+
Filename = "hello.jpg",
106+
ContentType = "image/jpg"
107+
};
108+
var request = new MultiPartRequest(id, profileImage);
109+
110+
var response = await new MultiPartClient(host, null).GetFormDataClient().CheckFileNameAndContentTypeAsync(request);
111+
112+
Assert.AreEqual(204, response.GetRawResponse().Status);
113+
});
114+
59115
[CadlRanchTest]
60116
public Task FileArrayAndBasic() => Test(async (host) =>
61117
{
@@ -78,6 +134,34 @@ public Task FileArrayAndBasic() => Test(async (host) =>
78134
Assert.AreEqual(204, response.GetRawResponse().Status);
79135
});
80136

137+
[CadlRanchTest]
138+
public Task FileArrayAndBasicConv() => Test(async (host) =>
139+
{
140+
var id = "123";
141+
Address address = new Address("X");
142+
143+
await using var imageStream1 = File.OpenRead(SampleJpgPath);
144+
var profileImage = new ProfileImageFileDetails(imageStream1)
145+
{
146+
Filename = "profileImage",
147+
ContentType = "application/octet-stream"
148+
};
149+
150+
await using var imageStream2 = File.OpenRead(SamplePngPath);
151+
await using var imageStream3 = File.OpenRead(SamplePngPath);
152+
var pictures = new List<PicturesFileDetails>()
153+
{
154+
new(imageStream2) { Filename = "pictures", ContentType = "application/octet-stream" },
155+
new(imageStream3) { Filename = "pictures", ContentType = "application/octet-stream" }
156+
};
157+
158+
var request = new ComplexPartsRequest(id, address, profileImage, pictures);
159+
160+
var response = await new MultiPartClient(host, null).GetFormDataClient().FileArrayAndBasicAsync(request);
161+
162+
Assert.AreEqual(204, response.GetRawResponse().Status);
163+
});
164+
81165
[CadlRanchTest]
82166
public Task HttpPartsImageJpegContentType() => Test(async (host) =>
83167
{
@@ -93,6 +177,21 @@ public Task HttpPartsImageJpegContentType() => Test(async (host) =>
93177
Assert.AreEqual(204, response.GetRawResponse().Status);
94178
});
95179

180+
[CadlRanchTest]
181+
public Task HttpPartsImageJpegContentTypeConv() => Test(async (host) =>
182+
{
183+
await using var imageStream = File.OpenRead(SampleJpgPath);
184+
var profileImage = new FileSpecificContentType(imageStream, "hello.jpg");
185+
var request = new FileWithHttpPartSpecificContentTypeRequest(profileImage);
186+
187+
var response = await new MultiPartClient(host, null).GetFormDataClient()
188+
.GetFormDataHttpPartsClient()
189+
.GetFormDataHttpPartsContentTypeClient()
190+
.ImageJpegContentTypeAsync(request);
191+
192+
Assert.AreEqual(204, response.GetRawResponse().Status);
193+
});
194+
96195
[CadlRanchTest]
97196
public Task HttpPartsOptionalContentType() => Test(async (host) =>
98197
{
@@ -119,6 +218,36 @@ public Task HttpPartsOptionalContentType() => Test(async (host) =>
119218
Assert.AreEqual(204, response.GetRawResponse().Status);
120219
});
121220

221+
[CadlRanchTest]
222+
public Task HttpPartsOptionalContentTypeConv() => Test(async (host) =>
223+
{
224+
await using var imageStream1 = File.OpenRead(SampleJpgPath);
225+
var profileImage = new FileOptionalContentType(imageStream1, "hello.jpg");
226+
var request = new FileWithHttpPartOptionalContentTypeRequest(profileImage);
227+
228+
var response = await new MultiPartClient(host, null).GetFormDataClient()
229+
.GetFormDataHttpPartsClient()
230+
.GetFormDataHttpPartsContentTypeClient()
231+
.OptionalContentTypeAsync(request);
232+
233+
Assert.AreEqual(204, response.GetRawResponse().Status);
234+
235+
using MultiPartFormDataBinaryContent contentWithContentType = new MultiPartFormDataBinaryContent();
236+
await using var imageStream2 = File.OpenRead(SampleJpgPath);
237+
var profileImage2 = new FileOptionalContentType(imageStream2, "hello.jpg")
238+
{
239+
ContentType = "application/octet-stream"
240+
};
241+
request = new FileWithHttpPartOptionalContentTypeRequest(profileImage2);
242+
243+
response = await new MultiPartClient(host, null).GetFormDataClient()
244+
.GetFormDataHttpPartsClient()
245+
.GetFormDataHttpPartsContentTypeClient()
246+
.OptionalContentTypeAsync(request);
247+
248+
Assert.AreEqual(204, response.GetRawResponse().Status);
249+
});
250+
122251
[CadlRanchTest]
123252
public Task HttpPartsRequiredContentType() => Test(async (host) =>
124253
{
@@ -134,6 +263,21 @@ public Task HttpPartsRequiredContentType() => Test(async (host) =>
134263
Assert.AreEqual(204, response.GetRawResponse().Status);
135264
});
136265

266+
[CadlRanchTest]
267+
public Task HttpPartsRequiredContentTypeConv() => Test(async (host) =>
268+
{
269+
await using var imageStream = File.OpenRead(SampleJpgPath);
270+
var profileImage = new FileRequiredMetaData(imageStream, "hello.jpg", "application/octet-stream");
271+
var request = new FileWithHttpPartRequiredContentTypeRequest(profileImage);
272+
273+
var response = await new MultiPartClient(host, null).GetFormDataClient()
274+
.GetFormDataHttpPartsClient()
275+
.GetFormDataHttpPartsContentTypeClient()
276+
.RequiredContentTypeAsync(request);
277+
278+
Assert.AreEqual(204, response.GetRawResponse().Status);
279+
});
280+
137281
[CadlRanchTest]
138282
public Task HttpPartsJsonArrayAndFileArray() => Test(async (host) =>
139283
{
@@ -170,6 +314,18 @@ public Task HttpPartsNonStringFloat() => Test(async (host) =>
170314
Assert.AreEqual(204, response.GetRawResponse().Status);
171315
});
172316

317+
[CadlRanchTest]
318+
public Task HttpPartsNonStringFloatAsync() => Test(async (host) =>
319+
{
320+
var temperature = new FloatRequestTemperature(0.5f);
321+
var request = new FloatRequest(temperature);
322+
var response = await new MultiPartClient(host, null).GetFormDataClient()
323+
.GetFormDataHttpPartsClient()
324+
.GetFormDataHttpPartsNonStringClient()
325+
.FloatAsync(request);
326+
Assert.AreEqual(204, response.GetRawResponse().Status);
327+
});
328+
173329
[CadlRanchTest]
174330
public Task BinaryArrayParts() => Test(async (host) =>
175331
{
@@ -186,6 +342,24 @@ public Task BinaryArrayParts() => Test(async (host) =>
186342
Assert.AreEqual(204, response.GetRawResponse().Status);
187343
});
188344

345+
[CadlRanchTest]
346+
public Task BinaryArrayPartsConv() => Test(async (host) =>
347+
{
348+
var id = "123";
349+
await using var imageStream1 = File.OpenRead(SamplePngPath);
350+
await using var imageStream2 = File.OpenRead(SamplePngPath);
351+
var pictures = new List<PicturesFileDetails>()
352+
{
353+
new(imageStream1) { Filename = "pictures", ContentType = "application/octet-stream" },
354+
new(imageStream2) { Filename = "pictures", ContentType = "application/octet-stream" }
355+
};
356+
var request = new BinaryArrayPartsRequest(id, pictures);
357+
358+
var response = await new MultiPartClient(host, null).GetFormDataClient().BinaryArrayPartsAsync(request);
359+
360+
Assert.AreEqual(204, response.GetRawResponse().Status);
361+
});
362+
189363
[CadlRanchTest]
190364
public Task AnonymousModel() => Test(async (host) =>
191365
{
@@ -198,14 +372,36 @@ public Task AnonymousModel() => Test(async (host) =>
198372
Assert.AreEqual(204, response.GetRawResponse().Status);
199373
});
200374

375+
[CadlRanchTest]
376+
public Task AnonymousModelConv() => Test(async (host) =>
377+
{
378+
await using var imageStream = File.OpenRead(SampleJpgPath);
379+
var profileImageFileDetails = new ProfileImageFileDetails(imageStream)
380+
{
381+
Filename = "profileImage",
382+
ContentType = "application/octet-stream"
383+
};
384+
385+
var response = await new MultiPartClient(host, null).GetFormDataClient().AnonymousModelAsync(profileImageFileDetails);
386+
387+
Assert.AreEqual(204, response.GetRawResponse().Status);
388+
});
389+
201390
[CadlRanchTest]
202391
public Task MultiBinaryPartsWithPicture()
203392
=> MultiBinaryParts(true);
393+
[CadlRanchTest]
394+
public Task MultiBinaryPartsWithPictureConv()
395+
=> MultiBinaryPartsConv(true);
204396

205397
[CadlRanchTest]
206398
public Task MultiBinaryPartsWithoutPicture()
207399
=> MultiBinaryParts(false);
208400

401+
[CadlRanchTest]
402+
public Task MultiBinaryPartsWithoutPictureConv()
403+
=> MultiBinaryPartsConv(false);
404+
209405
private Task MultiBinaryParts(bool hasPicture) => Test(async (host) =>
210406
{
211407
using MultiPartFormDataBinaryContent content = new MultiPartFormDataBinaryContent();
@@ -219,5 +415,29 @@ private Task MultiBinaryParts(bool hasPicture) => Test(async (host) =>
219415
var response = await new MultiPartClient(host, null).GetFormDataClient().MultiBinaryPartsAsync(content, content.ContentType, null);
220416
Assert.AreEqual(204, response.GetRawResponse().Status);
221417
});
418+
419+
private Task MultiBinaryPartsConv(bool hasPicture) => Test(async (host) =>
420+
{
421+
await using var imageStream1 = File.OpenRead(SampleJpgPath);
422+
var profileImage = new ProfileImageFileDetails(imageStream1)
423+
{
424+
Filename = "profileImage",
425+
ContentType = "application/octet-stream"
426+
};
427+
428+
await using var imageStream2 = File.OpenRead(SamplePngPath);
429+
var picture = new PictureFileDetails(imageStream2)
430+
{
431+
Filename = "picture",
432+
ContentType = "application/octet-stream"
433+
};
434+
var request = new MultiBinaryPartsRequest(profileImage);
435+
if (hasPicture)
436+
{
437+
request.Picture = picture;
438+
}
439+
var response = await new MultiPartClient(host, null).GetFormDataClient().MultiBinaryPartsAsync(request);
440+
Assert.AreEqual(204, response.GetRawResponse().Status);
441+
});
222442
}
223443
}

0 commit comments

Comments
 (0)