Skip to content

Commit 74b29f3

Browse files
authored
feat: remove RestSharp dependency (#31)
1 parent 453761f commit 74b29f3

3 files changed

Lines changed: 38 additions & 22 deletions

File tree

tests/Captcha.FunctionalTests/Captcha.FunctionalTests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.4.0" />
1919
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.23.0" />
2020
<PackageReference Include="Reqnroll.NUnit" Version="3.3.4" />
21-
<PackageReference Include="RestSharp" Version="114.0.0" />
2221
<PackageReference Include="nunit" Version="4.5.1" />
2322
<PackageReference Include="NUnit3TestAdapter" Version="6.2.0" />
2423
</ItemGroup>

tests/Captcha.FunctionalTests/StepDefinitions/CaptchaSteps.cs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
namespace Captcha.FunctionalTests.StepDefinitions;
22

33
using System.Globalization;
4+
using System.Text;
5+
using System.Text.Json;
46
using Core.Models;
57
using NUnit.Framework;
68
using Reqnroll;
7-
using RestSharp;
89
using SkiaSharp;
910
using Support;
1011

@@ -14,7 +15,7 @@ public class CaptchaSteps(ScenarioContext context) : TestBase(context)
1415
private GetCreateCaptchaRequest? _getRequest;
1516
private PostCreateCaptchaRequest? _postRequest;
1617

17-
private RestResponse? _response;
18+
private HttpResponseMessage? _response;
1819

1920
[Given(@"I have a captcha request with following parameters:")]
2021
public void GivenIHaveACaptchaRequestWithFollowingParameters(Table table)
@@ -48,20 +49,17 @@ public void GivenIHaveACaptchaRequestWithFollowingParameters(Table table)
4849
[When(@"I send the request to the Create endpoint of the CaptchaController")]
4950
public async Task WhenISendTheRequestToTheCreateEndpointOfTheCaptchaController()
5051
{
51-
var request = new RestRequest(TestConstants.CreateCaptchaEndpoint)
52-
{
53-
RequestFormat = DataFormat.Json,
54-
Method = Method.Post
55-
}.AddJsonBody(_postRequest);
56-
57-
_response = await Client.ExecuteAsync(request);
52+
var json = JsonSerializer.Serialize(_postRequest);
53+
var content = new StringContent(json, Encoding.UTF8, "application/json");
54+
_response = await Client.PostAsync(TestConstants.CreateCaptchaEndpoint, content);
5855
}
5956

6057
[Then(@"I expect a captcha image to be returned with the following attributes:")]
61-
public void ThenIExpectACaptchaImageToBeReturnedWithTheFollowingAttributes(Table table)
58+
public async Task ThenIExpectACaptchaImageToBeReturnedWithTheFollowingAttributes(Table table)
6259
{
6360
var row = table.Rows[0];
64-
using var ms = new MemoryStream(_response.RawBytes);
61+
var rawBytes = await _response!.Content.ReadAsByteArrayAsync();
62+
using var ms = new MemoryStream(rawBytes);
6563
var img = SKImage.FromEncodedData(ms);
6664

6765
var expectedWidth = int.Parse(row[TestConstants.Width], CultureInfo.InvariantCulture);
@@ -72,9 +70,10 @@ public void ThenIExpectACaptchaImageToBeReturnedWithTheFollowingAttributes(Table
7270
}
7371

7472
[Then(@"I expect a captcha image to be returned without any black borders")]
75-
public void ThenIExpectACaptchaImageToBeReturnedWithoutAnyBlackBorders()
73+
public async Task ThenIExpectACaptchaImageToBeReturnedWithoutAnyBlackBorders()
7674
{
77-
using var ms = new MemoryStream(_response!.RawBytes!);
75+
var rawBytes = await _response!.Content.ReadAsByteArrayAsync();
76+
using var ms = new MemoryStream(rawBytes);
7877
var img = SKImage.FromEncodedData(ms);
7978
var bmp = SKBitmap.FromImage(img);
8079

@@ -94,7 +93,7 @@ public void ThenIExpectACaptchaImageToBeReturnedWithoutAnyBlackBorders()
9493
}
9594

9695
[Then("I expect a captcha image to contain at least {string} pixels of color {string} and at least {string} pixels of color {string}")]
97-
public void ThenIExpectACaptchaImageToContainPixelsOfColorAndPixelsOfColor
96+
public async Task ThenIExpectACaptchaImageToContainPixelsOfColorAndPixelsOfColor
9897
(string firstColorAmountOfPixels, string firstColorHex, string secondColorAmountOfPixels, string secondColorHex)
9998
{
10099
var firstColor = SKColor.Parse(firstColorHex);
@@ -103,7 +102,8 @@ public void ThenIExpectACaptchaImageToContainPixelsOfColorAndPixelsOfColor
103102
var secondColor = SKColor.Parse(secondColorHex);
104103
var secondColorExpectedAmount = int.Parse(secondColorAmountOfPixels, CultureInfo.InvariantCulture);
105104

106-
using var ms = new MemoryStream(_response!.RawBytes!);
105+
var rawBytes = await _response!.Content.ReadAsByteArrayAsync();
106+
using var ms = new MemoryStream(rawBytes);
107107
var img = SKImage.FromEncodedData(ms);
108108
var bmp = SKBitmap.FromImage(img);
109109

@@ -163,9 +163,27 @@ public void GivenIHaveACaptchaRequestUsingGetWithFollowingParameters(Table table
163163
[When("I send the get request to the Create endpoint of the CaptchaController")]
164164
public async Task WhenISendTheGetRequestToTheCreateEndpointOfTheCaptchaController()
165165
{
166-
var request = new RestRequest(TestConstants.CreateCaptchaEndpoint)
167-
.AddObject(_getRequest);
166+
var queryParams = new Dictionary<string, string?>
167+
{
168+
["Text"] = _getRequest!.Text
169+
};
170+
if (_getRequest.Width.HasValue)
171+
{
172+
queryParams["Width"] = _getRequest.Width.Value.ToString(CultureInfo.InvariantCulture);
173+
}
174+
175+
if (_getRequest.Height.HasValue)
176+
{
177+
queryParams["Height"] = _getRequest.Height.Value.ToString(CultureInfo.InvariantCulture);
178+
}
179+
180+
if (_getRequest.Difficulty.HasValue)
181+
{
182+
queryParams["Difficulty"] = _getRequest.Difficulty.ToString();
183+
}
168184

169-
_response = await Client.ExecuteAsync(request);
185+
var queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value!)}"));
186+
var url = $"{TestConstants.CreateCaptchaEndpoint}?{queryString}";
187+
_response = await Client.GetAsync(url);
170188
}
171189
}

tests/Captcha.FunctionalTests/Support/TestBase.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ namespace Captcha.FunctionalTests.Support;
22

33
using Microsoft.AspNetCore.Mvc.Testing;
44
using Reqnroll;
5-
using RestSharp;
65
public class TestBase
76
{
8-
protected RestClient Client { get; set; }
7+
protected HttpClient Client { get; set; }
98
protected ScenarioContext Context { get; set; }
109

1110
protected TestBase(ScenarioContext context)
1211
{
1312
var webApplicationFactory = new WebApplicationFactory<Program>();
1413

15-
Client = new RestClient(webApplicationFactory.CreateClient());
14+
Client = webApplicationFactory.CreateClient();
1615
Context = context;
1716
}
1817
}

0 commit comments

Comments
 (0)