Skip to content

Commit

Permalink
feat: added get byte array request
Browse files Browse the repository at this point in the history
  • Loading branch information
sikelio committed Aug 5, 2024
1 parent 9bbc479 commit 97cd495
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 6 deletions.
48 changes: 48 additions & 0 deletions Web.Test/RequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class RequestTest
private const string TestPostUrl = "https://httpbin.org/post";
private const string TestPutUrl = "https://httpbin.org/put";

private const string TestGetJpegImageUrl = "https://httpbin.org/image/jpeg";
private const string TestGetPngImageUrl = "https://httpbin.org/image/png";
private const string TestGetSvgImageUrl = "https://httpbin.org/image/svg";
private const string TestGetWebpImageUrl = "https://httpbin.org/image/webp";

[TestMethod]
public async Task SendGetRequest()
{
Expand Down Expand Up @@ -75,6 +80,41 @@ public async Task SendPostRequestWithFile()
StringAssert.Contains(result.Value.Data, "Hello world", "File content should contain 'Hello World'");
}

[TestMethod]
public async Task SendJpegImageRequest()
{
await SendImageRequest("image/jpeg", "jpeg");
}

[TestMethod]
public async Task SendPngImageRequest()
{
await SendImageRequest("image/png", "png");
}

[TestMethod]
public async Task SendSvgImageRequest()
{
await SendImageRequest("image/svg+xml", "svg");
}

[TestMethod]
public async Task SendWebpImageRequest()
{
await SendImageRequest("image/webp", "webp");
}

private async Task SendImageRequest(string acceptType, string imageType)
{
Request request = new(TestGetWebpImageUrl, HttpMethod.Get);
request
.AddHeader("Accept", acceptType);

Result<byte[]> result = await request.RunGetBytes();

AssertImageResponse(result, imageType);
}

private void AssertResponse<T>(Result<T> result, string expectedUrl) where T : class
{
Assert.AreEqual(200, result.StatusCode, "Status code should be 200");
Expand All @@ -87,5 +127,13 @@ private void AssertResponse<T>(Result<T> result, string expectedUrl) where T : c
Assert.IsNotNull(headers, "Headers should not be null");
Assert.AreEqual("httpbin.org", headers.Host, "Host header should be 'httpbin.org'");
}

private void AssertImageResponse(Result<byte[]> result, string imgType)
{
Assert.AreEqual(200, result.StatusCode, "Status code should be 200");
Assert.IsNotNull(result.Value, "Response value should not be null");
Assert.IsTrue(result.Value.Length > 0, "Image data should not be empty");
File.WriteAllBytes($"test_image.{imgType}", result.Value);
}
}
}
48 changes: 42 additions & 6 deletions Web/API/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,12 @@ public async Task<Result<T>> Run<T>()
return result;
}

/// <summary>
/// Executes the HTTP request with the binary document content included.
/// </summary>
/// <typeparam name="T">The type of the response expected from the request.</typeparam>
/// <returns>A Result object containing the response.</returns>
public async Task<Result<T>> RunDocument<T>()
/// <summary>
/// Executes the HTTP request and returns the response content as a byte array.
/// </summary>
/// <typeparam name="T">The type of the response expected from the request.</typeparam>
/// <returns>A Result object containing the response.</returns>
public async Task<Result<T>> RunDocument<T>()
{
HttpRequestMessage request = BuildBaseRequest();

Expand Down Expand Up @@ -288,6 +288,42 @@ public async Task<Result<T>> RunDocument<T>()
return result;
}

/// <summary>
/// Executes the HTTP request that will return a byte array.
/// </summary>
/// <returns>
/// A <see cref="Result{T}"/> object containing the response content as a byte array,
/// the status code of the response, and any error message if the request fails.
/// </returns>
public async Task<Result<byte[]>> RunGetBytes()
{
HttpRequestMessage request = BuildBaseRequest();
Result<byte[]> result = new();

try
{
HttpResponseMessage response = await _httpClient.SendAsync(request);

result.StatusCode = (int)response.StatusCode;

if (response.IsSuccessStatusCode)
{
result.Value = await response.Content.ReadAsByteArrayAsync();
}
else
{
result.Error = await response.Content.ReadAsStringAsync();
}
}
catch (Exception ex)
{
result.StatusCode = 500;
result.Error = ex.Message;
}

return result;
}

#endregion

#region Private function
Expand Down

0 comments on commit 97cd495

Please sign in to comment.