From 97cd49552959313e1764a7c990e631af64c4c494 Mon Sep 17 00:00:00 2001 From: Sik Date: Mon, 5 Aug 2024 22:41:33 +0200 Subject: [PATCH] feat: added get byte array request --- Web.Test/RequestTest.cs | 48 +++++++++++++++++++++++++++++++++++++++++ Web/API/Request.cs | 48 +++++++++++++++++++++++++++++++++++------ 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/Web.Test/RequestTest.cs b/Web.Test/RequestTest.cs index 68d65c6..ae6877b 100644 --- a/Web.Test/RequestTest.cs +++ b/Web.Test/RequestTest.cs @@ -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() { @@ -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 result = await request.RunGetBytes(); + + AssertImageResponse(result, imageType); + } + private void AssertResponse(Result result, string expectedUrl) where T : class { Assert.AreEqual(200, result.StatusCode, "Status code should be 200"); @@ -87,5 +127,13 @@ private void AssertResponse(Result 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 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); + } } } diff --git a/Web/API/Request.cs b/Web/API/Request.cs index 6d57eb3..f66fcb5 100644 --- a/Web/API/Request.cs +++ b/Web/API/Request.cs @@ -251,12 +251,12 @@ public async Task> Run() return result; } - /// - /// Executes the HTTP request with the binary document content included. - /// - /// The type of the response expected from the request. - /// A Result object containing the response. - public async Task> RunDocument() + /// + /// Executes the HTTP request and returns the response content as a byte array. + /// + /// The type of the response expected from the request. + /// A Result object containing the response. + public async Task> RunDocument() { HttpRequestMessage request = BuildBaseRequest(); @@ -288,6 +288,42 @@ public async Task> RunDocument() return result; } + /// + /// Executes the HTTP request that will return a byte array. + /// + /// + /// A object containing the response content as a byte array, + /// the status code of the response, and any error message if the request fails. + /// + public async Task> RunGetBytes() + { + HttpRequestMessage request = BuildBaseRequest(); + Result 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