11namespace Captcha . FunctionalTests . StepDefinitions ;
22
33using System . Globalization ;
4+ using System . Text ;
5+ using System . Text . Json ;
46using Core . Models ;
57using NUnit . Framework ;
68using Reqnroll ;
7- using RestSharp ;
89using SkiaSharp ;
910using 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}
0 commit comments