1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT License.
3
3
4
+ using System . Collections . Generic ;
4
5
using System . IO ;
5
6
using System . Threading . Tasks ;
6
7
using NUnit . Framework ;
7
8
using Payload . MultiPart ;
9
+ using Payload . MultiPart . Models ;
8
10
using File = System . IO . File ;
9
11
10
12
namespace TestProjects . CadlRanch . Tests . Http . Payload . Multipart
@@ -28,6 +30,23 @@ public Task Basic() => Test(async (host) =>
28
30
Assert . AreEqual ( 204 , response . GetRawResponse ( ) . Status ) ;
29
31
} ) ;
30
32
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
+
31
50
[ CadlRanchTest ]
32
51
public Task JsonPart ( ) => Test ( async ( host ) =>
33
52
{
@@ -42,6 +61,25 @@ public Task JsonPart() => Test(async (host) =>
42
61
Assert . AreEqual ( 204 , response . GetRawResponse ( ) . Status ) ;
43
62
} ) ;
44
63
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
+
45
83
[ CadlRanchTest ]
46
84
public Task CheckFileNameAndContentType ( ) => Test ( async ( host ) =>
47
85
{
@@ -56,6 +94,24 @@ public Task CheckFileNameAndContentType() => Test(async (host) =>
56
94
Assert . AreEqual ( 204 , response . GetRawResponse ( ) . Status ) ;
57
95
} ) ;
58
96
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
+
59
115
[ CadlRanchTest ]
60
116
public Task FileArrayAndBasic ( ) => Test ( async ( host ) =>
61
117
{
@@ -78,6 +134,34 @@ public Task FileArrayAndBasic() => Test(async (host) =>
78
134
Assert . AreEqual ( 204 , response . GetRawResponse ( ) . Status ) ;
79
135
} ) ;
80
136
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
+
81
165
[ CadlRanchTest ]
82
166
public Task HttpPartsImageJpegContentType ( ) => Test ( async ( host ) =>
83
167
{
@@ -93,6 +177,21 @@ public Task HttpPartsImageJpegContentType() => Test(async (host) =>
93
177
Assert . AreEqual ( 204 , response . GetRawResponse ( ) . Status ) ;
94
178
} ) ;
95
179
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
+
96
195
[ CadlRanchTest ]
97
196
public Task HttpPartsOptionalContentType ( ) => Test ( async ( host ) =>
98
197
{
@@ -119,6 +218,36 @@ public Task HttpPartsOptionalContentType() => Test(async (host) =>
119
218
Assert . AreEqual ( 204 , response . GetRawResponse ( ) . Status ) ;
120
219
} ) ;
121
220
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
+
122
251
[ CadlRanchTest ]
123
252
public Task HttpPartsRequiredContentType ( ) => Test ( async ( host ) =>
124
253
{
@@ -134,6 +263,21 @@ public Task HttpPartsRequiredContentType() => Test(async (host) =>
134
263
Assert . AreEqual ( 204 , response . GetRawResponse ( ) . Status ) ;
135
264
} ) ;
136
265
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
+
137
281
[ CadlRanchTest ]
138
282
public Task HttpPartsJsonArrayAndFileArray ( ) => Test ( async ( host ) =>
139
283
{
@@ -170,6 +314,18 @@ public Task HttpPartsNonStringFloat() => Test(async (host) =>
170
314
Assert . AreEqual ( 204 , response . GetRawResponse ( ) . Status ) ;
171
315
} ) ;
172
316
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
+
173
329
[ CadlRanchTest ]
174
330
public Task BinaryArrayParts ( ) => Test ( async ( host ) =>
175
331
{
@@ -186,6 +342,24 @@ public Task BinaryArrayParts() => Test(async (host) =>
186
342
Assert . AreEqual ( 204 , response . GetRawResponse ( ) . Status ) ;
187
343
} ) ;
188
344
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
+
189
363
[ CadlRanchTest ]
190
364
public Task AnonymousModel ( ) => Test ( async ( host ) =>
191
365
{
@@ -198,14 +372,36 @@ public Task AnonymousModel() => Test(async (host) =>
198
372
Assert . AreEqual ( 204 , response . GetRawResponse ( ) . Status ) ;
199
373
} ) ;
200
374
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
+
201
390
[ CadlRanchTest ]
202
391
public Task MultiBinaryPartsWithPicture ( )
203
392
=> MultiBinaryParts ( true ) ;
393
+ [ CadlRanchTest ]
394
+ public Task MultiBinaryPartsWithPictureConv ( )
395
+ => MultiBinaryPartsConv ( true ) ;
204
396
205
397
[ CadlRanchTest ]
206
398
public Task MultiBinaryPartsWithoutPicture ( )
207
399
=> MultiBinaryParts ( false ) ;
208
400
401
+ [ CadlRanchTest ]
402
+ public Task MultiBinaryPartsWithoutPictureConv ( )
403
+ => MultiBinaryPartsConv ( false ) ;
404
+
209
405
private Task MultiBinaryParts ( bool hasPicture ) => Test ( async ( host ) =>
210
406
{
211
407
using MultiPartFormDataBinaryContent content = new MultiPartFormDataBinaryContent ( ) ;
@@ -219,5 +415,29 @@ private Task MultiBinaryParts(bool hasPicture) => Test(async (host) =>
219
415
var response = await new MultiPartClient ( host , null ) . GetFormDataClient ( ) . MultiBinaryPartsAsync ( content , content . ContentType , null ) ;
220
416
Assert . AreEqual ( 204 , response . GetRawResponse ( ) . Status ) ;
221
417
} ) ;
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
+ } ) ;
222
442
}
223
443
}
0 commit comments