@@ -186,5 +186,116 @@ await petApi.UploadFileAsync(
186186 StringAssert . Contains ( _capturingHandler . CapturedBody , "testmetadata" ,
187187 "Body should contain the additionalMetadata value." ) ;
188188 }
189+
190+ /// <summary>
191+ /// Covers the case where an optional file and an optional text field are both supplied.
192+ /// Ensures that adding text alongside a file does not strip the boundary.
193+ /// </summary>
194+ [ TestMethod ]
195+ public async Task UploadFile_WithBothFileAndText_ContentTypeHasBoundary ( )
196+ {
197+ var petApi = _host . Services . GetRequiredService < IPetApi > ( ) ;
198+ using var stream = new MemoryStream ( new byte [ ] { 1 , 2 , 3 } ) ;
199+
200+ await petApi . UploadFileAsync (
201+ petId : 1 ,
202+ file : new Option < System . IO . Stream > ( stream ) ,
203+ additionalMetadata : new Option < string > ( "testmetadata" ) ) ;
204+
205+ Assert . IsNotNull ( _capturingHandler . CapturedContentType ,
206+ "Content-Type header should not be null." ) ;
207+ StringAssert . Contains ( _capturingHandler . CapturedContentType , "multipart/form-data" ,
208+ "Content-Type should be multipart/form-data." ) ;
209+ StringAssert . Contains ( _capturingHandler . CapturedContentType , "boundary=" ,
210+ "Content-Type must include a boundary parameter." ) ;
211+ }
212+
213+ [ TestMethod ]
214+ public async Task UploadFile_WithBothFileAndText_BodyContainsBothFields ( )
215+ {
216+ var petApi = _host . Services . GetRequiredService < IPetApi > ( ) ;
217+ using var stream = new MemoryStream ( new byte [ ] { 1 , 2 , 3 } ) ;
218+
219+ await petApi . UploadFileAsync (
220+ petId : 1 ,
221+ file : new Option < System . IO . Stream > ( stream ) ,
222+ additionalMetadata : new Option < string > ( "testmetadata" ) ) ;
223+
224+ Assert . IsNotNull ( _capturingHandler . CapturedBody , "Body should not be null." ) ;
225+ StringAssert . Contains ( _capturingHandler . CapturedBody , "name=file" ,
226+ "Body should contain a part named 'file'." ) ;
227+ StringAssert . Contains ( _capturingHandler . CapturedBody , "testmetadata" ,
228+ "Body should contain the additionalMetadata value." ) ;
229+ }
230+
231+ /// <summary>
232+ /// Covers UploadFileWithRequiredFile: a required (non-optional) binary file param.
233+ /// Tests the required-file code path in the template, which differs from optional files.
234+ /// </summary>
235+ [ TestMethod ]
236+ public async Task UploadFileWithRequiredFile_RequiredFileOnly_ContentTypeHasBoundary ( )
237+ {
238+ var petApi = _host . Services . GetRequiredService < IPetApi > ( ) ;
239+ using var stream = new MemoryStream ( new byte [ ] { 7 , 8 , 9 } ) ;
240+
241+ await petApi . UploadFileWithRequiredFileAsync ( petId : 1 , requiredFile : stream ) ;
242+
243+ Assert . IsNotNull ( _capturingHandler . CapturedContentType ,
244+ "Content-Type header should not be null." ) ;
245+ StringAssert . Contains ( _capturingHandler . CapturedContentType , "multipart/form-data" ,
246+ "Content-Type should be multipart/form-data." ) ;
247+ StringAssert . Contains ( _capturingHandler . CapturedContentType , "boundary=" ,
248+ "Content-Type must include a boundary parameter." ) ;
249+ }
250+
251+ [ TestMethod ]
252+ public async Task UploadFileWithRequiredFile_RequiredFileOnly_BodyContainsCorrectFieldName ( )
253+ {
254+ var petApi = _host . Services . GetRequiredService < IPetApi > ( ) ;
255+ using var stream = new MemoryStream ( new byte [ ] { 7 , 8 , 9 } ) ;
256+
257+ await petApi . UploadFileWithRequiredFileAsync ( petId : 1 , requiredFile : stream ) ;
258+
259+ Assert . IsNotNull ( _capturingHandler . CapturedBody , "Body should not be null." ) ;
260+ StringAssert . Contains ( _capturingHandler . CapturedBody , "name=requiredFile" ,
261+ "Multipart part should use the field name 'requiredFile' as defined in the spec." ) ;
262+ }
263+
264+ [ TestMethod ]
265+ public async Task UploadFileWithRequiredFile_WithTextAndFile_ContentTypeHasBoundary ( )
266+ {
267+ var petApi = _host . Services . GetRequiredService < IPetApi > ( ) ;
268+ using var stream = new MemoryStream ( new byte [ ] { 7 , 8 , 9 } ) ;
269+
270+ await petApi . UploadFileWithRequiredFileAsync (
271+ petId : 1 ,
272+ requiredFile : stream ,
273+ additionalMetadata : new Option < string > ( "testmetadata" ) ) ;
274+
275+ Assert . IsNotNull ( _capturingHandler . CapturedContentType ,
276+ "Content-Type header should not be null." ) ;
277+ StringAssert . Contains ( _capturingHandler . CapturedContentType , "multipart/form-data" ,
278+ "Content-Type should be multipart/form-data." ) ;
279+ StringAssert . Contains ( _capturingHandler . CapturedContentType , "boundary=" ,
280+ "Adding text fields alongside binary fields must not strip the boundary." ) ;
281+ }
282+
283+ [ TestMethod ]
284+ public async Task UploadFileWithRequiredFile_WithTextAndFile_BodyContainsBothFields ( )
285+ {
286+ var petApi = _host . Services . GetRequiredService < IPetApi > ( ) ;
287+ using var stream = new MemoryStream ( new byte [ ] { 7 , 8 , 9 } ) ;
288+
289+ await petApi . UploadFileWithRequiredFileAsync (
290+ petId : 1 ,
291+ requiredFile : stream ,
292+ additionalMetadata : new Option < string > ( "testmetadata" ) ) ;
293+
294+ Assert . IsNotNull ( _capturingHandler . CapturedBody , "Body should not be null." ) ;
295+ StringAssert . Contains ( _capturingHandler . CapturedBody , "name=requiredFile" ,
296+ "Body should contain a part named 'requiredFile'." ) ;
297+ StringAssert . Contains ( _capturingHandler . CapturedBody , "testmetadata" ,
298+ "Body should contain the additionalMetadata value." ) ;
299+ }
189300 }
190301}
0 commit comments