Skip to content

Commit

Permalink
initial design
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerangel-msft committed Jan 15, 2025
1 parent 27373e7 commit 48f4f78
Show file tree
Hide file tree
Showing 53 changed files with 3,027 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using NUnit.Framework;
using Payload.MultiPart;
using Payload.MultiPart.Models;
using File = System.IO.File;

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

[CadlRanchTest]
public Task BasicConv() => Test(async (host) =>
{
var id = "123";
await using var imageStream = File.OpenRead(SampleJpgPath);

var profileImage = new ProfileImageFileDetails(imageStream)
{
Filename = "profileImage",
ContentType = "application/octet-stream"
};
var request = new MultiPartRequest(id, profileImage);
var response = await new MultiPartClient(host, null).GetFormDataClient().BasicAsync(request);

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task JsonPart() => Test(async (host) =>
{
Expand All @@ -42,6 +61,25 @@ public Task JsonPart() => Test(async (host) =>
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task JsonPartConv() => Test(async (host) =>
{
Address address = new Address("X");

await using var imageStream = File.OpenRead(SampleJpgPath);
var profileImage = new ProfileImageFileDetails(imageStream)
{
Filename = "profileImage",
ContentType = "application/octet-stream"
};

var request = new JsonPartRequest(address, profileImage);

var response = await new MultiPartClient(host, null).GetFormDataClient().JsonPartAsync(request);

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task CheckFileNameAndContentType() => Test(async (host) =>
{
Expand All @@ -56,6 +94,24 @@ public Task CheckFileNameAndContentType() => Test(async (host) =>
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task CheckFileNameAndContentTypeConv() => Test(async (host) =>
{
var id = "123";

await using var imageStream = File.OpenRead(SampleJpgPath);
var profileImage = new ProfileImageFileDetails(imageStream)
{
Filename = "hello.jpg",
ContentType = "image/jpg"
};
var request = new MultiPartRequest(id, profileImage);

var response = await new MultiPartClient(host, null).GetFormDataClient().CheckFileNameAndContentTypeAsync(request);

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task FileArrayAndBasic() => Test(async (host) =>
{
Expand All @@ -78,6 +134,34 @@ public Task FileArrayAndBasic() => Test(async (host) =>
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task FileArrayAndBasicConv() => Test(async (host) =>
{
var id = "123";
Address address = new Address("X");

await using var imageStream1 = File.OpenRead(SampleJpgPath);
var profileImage = new ProfileImageFileDetails(imageStream1)
{
Filename = "profileImage",
ContentType = "application/octet-stream"
};

await using var imageStream2 = File.OpenRead(SamplePngPath);
await using var imageStream3 = File.OpenRead(SamplePngPath);
var pictures = new List<PicturesFileDetails>()
{
new(imageStream2) { Filename = "pictures", ContentType = "application/octet-stream" },
new(imageStream3) { Filename = "pictures", ContentType = "application/octet-stream" }
};

var request = new ComplexPartsRequest(id, address, profileImage, pictures);

var response = await new MultiPartClient(host, null).GetFormDataClient().FileArrayAndBasicAsync(request);

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task HttpPartsImageJpegContentType() => Test(async (host) =>
{
Expand All @@ -93,6 +177,21 @@ public Task HttpPartsImageJpegContentType() => Test(async (host) =>
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task HttpPartsImageJpegContentTypeConv() => Test(async (host) =>
{
await using var imageStream = File.OpenRead(SampleJpgPath);
var profileImage = new FileSpecificContentType(imageStream, "hello.jpg");
var request = new FileWithHttpPartSpecificContentTypeRequest(profileImage);

var response = await new MultiPartClient(host, null).GetFormDataClient()
.GetFormDataHttpPartsClient()
.GetFormDataHttpPartsContentTypeClient()
.ImageJpegContentTypeAsync(request);

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task HttpPartsOptionalContentType() => Test(async (host) =>
{
Expand All @@ -119,6 +218,36 @@ public Task HttpPartsOptionalContentType() => Test(async (host) =>
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task HttpPartsOptionalContentTypeConv() => Test(async (host) =>
{
await using var imageStream1 = File.OpenRead(SampleJpgPath);
var profileImage = new FileOptionalContentType(imageStream1, "hello.jpg");
var request = new FileWithHttpPartOptionalContentTypeRequest(profileImage);

var response = await new MultiPartClient(host, null).GetFormDataClient()
.GetFormDataHttpPartsClient()
.GetFormDataHttpPartsContentTypeClient()
.OptionalContentTypeAsync(request);

Assert.AreEqual(204, response.GetRawResponse().Status);

using MultiPartFormDataBinaryContent contentWithContentType = new MultiPartFormDataBinaryContent();
await using var imageStream2 = File.OpenRead(SampleJpgPath);
var profileImage2 = new FileOptionalContentType(imageStream2, "hello.jpg")
{
ContentType = "application/octet-stream"
};
request = new FileWithHttpPartOptionalContentTypeRequest(profileImage2);

response = await new MultiPartClient(host, null).GetFormDataClient()
.GetFormDataHttpPartsClient()
.GetFormDataHttpPartsContentTypeClient()
.OptionalContentTypeAsync(request);

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task HttpPartsRequiredContentType() => Test(async (host) =>
{
Expand All @@ -134,6 +263,21 @@ public Task HttpPartsRequiredContentType() => Test(async (host) =>
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task HttpPartsRequiredContentTypeConv() => Test(async (host) =>
{
await using var imageStream = File.OpenRead(SampleJpgPath);
var profileImage = new FileRequiredMetaData(imageStream, "hello.jpg", "application/octet-stream");
var request = new FileWithHttpPartRequiredContentTypeRequest(profileImage);

var response = await new MultiPartClient(host, null).GetFormDataClient()
.GetFormDataHttpPartsClient()
.GetFormDataHttpPartsContentTypeClient()
.RequiredContentTypeAsync(request);

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task HttpPartsJsonArrayAndFileArray() => Test(async (host) =>
{
Expand Down Expand Up @@ -170,6 +314,18 @@ public Task HttpPartsNonStringFloat() => Test(async (host) =>
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task HttpPartsNonStringFloatAsync() => Test(async (host) =>
{
var temperature = new FloatRequestTemperature(0.5f);
var request = new FloatRequest(temperature);
var response = await new MultiPartClient(host, null).GetFormDataClient()
.GetFormDataHttpPartsClient()
.GetFormDataHttpPartsNonStringClient()
.FloatAsync(request);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task BinaryArrayParts() => Test(async (host) =>
{
Expand All @@ -186,6 +342,24 @@ public Task BinaryArrayParts() => Test(async (host) =>
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task BinaryArrayPartsConv() => Test(async (host) =>
{
var id = "123";
await using var imageStream1 = File.OpenRead(SamplePngPath);
await using var imageStream2 = File.OpenRead(SamplePngPath);
var pictures = new List<PicturesFileDetails>()
{
new(imageStream1) { Filename = "pictures", ContentType = "application/octet-stream" },
new(imageStream2) { Filename = "pictures", ContentType = "application/octet-stream" }
};
var request = new BinaryArrayPartsRequest(id, pictures);

var response = await new MultiPartClient(host, null).GetFormDataClient().BinaryArrayPartsAsync(request);

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task AnonymousModel() => Test(async (host) =>
{
Expand All @@ -198,14 +372,36 @@ public Task AnonymousModel() => Test(async (host) =>
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task AnonymousModelConv() => Test(async (host) =>
{
await using var imageStream = File.OpenRead(SampleJpgPath);
var profileImageFileDetails = new ProfileImageFileDetails(imageStream)
{
Filename = "profileImage",
ContentType = "application/octet-stream"
};

var response = await new MultiPartClient(host, null).GetFormDataClient().AnonymousModelAsync(profileImageFileDetails);

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task MultiBinaryPartsWithPicture()
=> MultiBinaryParts(true);
[CadlRanchTest]
public Task MultiBinaryPartsWithPictureConv()
=> MultiBinaryPartsConv(true);

[CadlRanchTest]
public Task MultiBinaryPartsWithoutPicture()
=> MultiBinaryParts(false);

[CadlRanchTest]
public Task MultiBinaryPartsWithoutPictureConv()
=> MultiBinaryPartsConv(false);

private Task MultiBinaryParts(bool hasPicture) => Test(async (host) =>
{
using MultiPartFormDataBinaryContent content = new MultiPartFormDataBinaryContent();
Expand All @@ -219,5 +415,29 @@ private Task MultiBinaryParts(bool hasPicture) => Test(async (host) =>
var response = await new MultiPartClient(host, null).GetFormDataClient().MultiBinaryPartsAsync(content, content.ContentType, null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

private Task MultiBinaryPartsConv(bool hasPicture) => Test(async (host) =>
{
await using var imageStream1 = File.OpenRead(SampleJpgPath);
var profileImage = new ProfileImageFileDetails(imageStream1)
{
Filename = "profileImage",
ContentType = "application/octet-stream"
};

await using var imageStream2 = File.OpenRead(SamplePngPath);
var picture = new PictureFileDetails(imageStream2)
{
Filename = "picture",
ContentType = "application/octet-stream"
};
var request = new MultiBinaryPartsRequest(profileImage);
if (hasPicture)
{
request.Picture = picture;
}
var response = await new MultiPartClient(host, null).GetFormDataClient().MultiBinaryPartsAsync(request);
Assert.AreEqual(204, response.GetRawResponse().Status);
});
}
}
Loading

0 comments on commit 48f4f78

Please sign in to comment.