Skip to content

Commit 42448e0

Browse files
authored
Change .Result into .GetAwaiter().GetResult() (#144)
1 parent 599f805 commit 42448e0

File tree

7 files changed

+30
-34
lines changed

7 files changed

+30
-34
lines changed

RestAssured.Net.Tests/JsonSchemaValidationTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// </copyright>
1616
namespace RestAssured.Tests
1717
{
18+
using System.Threading.Tasks;
1819
using NJsonSchema;
1920
using NUnit.Framework;
2021
using RestAssured.Response.Exceptions;
@@ -70,11 +71,11 @@ public void JsonSchemaCanBeSuppliedAndVerifiedAsStringPointingToFile()
7071
/// against a JSON schema supplied as a JsonSchema.
7172
/// </summary>
7273
[Test]
73-
public void JsonSchemaCanBeSuppliedAndVerifiedAsJsonSchema()
74+
public async Task JsonSchemaCanBeSuppliedAndVerifiedAsJsonSchema()
7475
{
7576
this.CreateStubForJsonSchemaValidation();
7677

77-
JsonSchema parsedSchema = JsonSchema.FromJsonAsync(JsonSchemaDefinitions.MatchingJsonSchema).Result;
78+
JsonSchema parsedSchema = await JsonSchema.FromJsonAsync(JsonSchemaDefinitions.MatchingJsonSchema);
7879

7980
Given()
8081
.When()

RestAssured.Net/Logging/RequestResponseLogger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private static void LogRequestBody(HttpRequestMessage request)
173173
return;
174174
}
175175

176-
string requestBodyAsString = request.Content.ReadAsStringAsync().Result;
176+
string requestBodyAsString = request.Content.ReadAsStringAsync().GetAwaiter().GetResult();
177177

178178
if (requestBodyAsString.Equals(string.Empty))
179179
{
@@ -250,7 +250,7 @@ private static void LogResponseBody(HttpResponseMessage response)
250250
return;
251251
}
252252

253-
string responseBodyAsString = response.Content.ReadAsStringAsync().Result;
253+
string responseBodyAsString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
254254

255255
if (responseBodyAsString.Equals(string.Empty))
256256
{

RestAssured.Net/Request/ExecutableRequest.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -774,19 +774,17 @@ private VerifiableResponse Send(HttpMethod httpMethod, string endpoint)
774774

775775
try
776776
{
777-
Task<VerifiableResponse> task = httpRequestProcessor.Send(this.request, this.cookieCollection, this.httpCompletionOption);
778-
VerifiableResponse verifiableResponse = task.Result;
777+
VerifiableResponse verifiableResponse = httpRequestProcessor.Send(this.request, this.cookieCollection, this.httpCompletionOption).GetAwaiter().GetResult();
779778
verifiableResponse = logger.LogResponse(verifiableResponse);
780779
return verifiableResponse;
781780
}
782-
catch (AggregateException ae)
781+
catch (TaskCanceledException)
783782
{
784-
if (ae.InnerException?.GetType() == typeof(TaskCanceledException))
785-
{
786-
throw new HttpRequestProcessorException($"Request timeout of {this.timeout ?? this.requestSpecification?.Timeout ?? TimeSpan.FromSeconds(100)} exceeded.");
787-
}
788-
789-
throw new HttpRequestProcessorException($"Unhandled exception {ae.Message}");
783+
throw new HttpRequestProcessorException($"Request timeout of {this.timeout ?? this.requestSpecification?.Timeout ?? TimeSpan.FromSeconds(100)} exceeded.");
784+
}
785+
catch (Exception ex)
786+
{
787+
throw new HttpRequestProcessorException($"Unhandled exception {ex.Message}");
790788
}
791789
}
792790

RestAssured.Net/Request/Logging/RequestLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private static void LogBody(HttpRequestMessage request)
104104
return;
105105
}
106106

107-
string requestBodyAsString = request.Content.ReadAsStringAsync().Result;
107+
string requestBodyAsString = request.Content.ReadAsStringAsync().GetAwaiter().GetResult();
108108

109109
if (requestBodyAsString.Equals(string.Empty))
110110
{

RestAssured.Net/Response/ExtractableResponse.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public ExtractableResponse(HttpResponseMessage response, CookieContainer cookieC
5656
[Obsolete("Please use BodyAsString(), BodyAsByteArray() or BodyAsStream() instead. This method is obsolete and will be removed in RestAssured.Net 5.0.0")]
5757
public string Body()
5858
{
59-
return this.response.Content.ReadAsStringAsync().Result;
59+
return this.response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
6060
}
6161

6262
/// <summary>
@@ -65,7 +65,7 @@ public string Body()
6565
/// <returns>The response body as a string.</returns>
6666
public string BodyAsString()
6767
{
68-
return this.response.Content.ReadAsStringAsync().Result;
68+
return this.response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
6969
}
7070

7171
/// <summary>
@@ -74,7 +74,7 @@ public string BodyAsString()
7474
/// <returns>The response body as a byte array.</returns>
7575
public byte[] BodyAsByteArray()
7676
{
77-
return this.response.Content.ReadAsByteArrayAsync().Result;
77+
return this.response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
7878
}
7979

8080
/// <summary>
@@ -83,7 +83,7 @@ public byte[] BodyAsByteArray()
8383
/// <returns>The response body as a <see cref="Stream"/>.</returns>
8484
public Stream BodyAsStream()
8585
{
86-
return this.response.Content.ReadAsStreamAsync().Result;
86+
return this.response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
8787
}
8888

8989
/// <summary>
@@ -96,7 +96,7 @@ public Stream BodyAsStream()
9696
/// <exception cref="ExtractionException">Thrown when evaluating the JsonPath did not yield any results.</exception>
9797
public object Body(string path, ExtractAs extractAs = ExtractAs.UseResponseContentTypeHeaderValue, ReturnAs returnAs = ReturnAs.Singular)
9898
{
99-
string responseBodyAsString = this.response.Content.ReadAsStringAsync().Result;
99+
string responseBodyAsString = this.response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
100100

101101
string responseMediaType = this.response.Content.Headers.ContentType?.MediaType ?? string.Empty;
102102

RestAssured.Net/Response/Logging/ResponseLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private static void LogBody(HttpResponseMessage response)
139139
return;
140140
}
141141

142-
string responseBodyAsString = response.Content.ReadAsStringAsync().Result;
142+
string responseBodyAsString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
143143

144144
if (responseBodyAsString.Equals(string.Empty))
145145
{

RestAssured.Net/Response/VerifiableResponse.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public VerifiableResponse Cookie(string name, IMatcher<string> matcher)
284284
/// <exception cref="ResponseVerificationException">Thrown when the actual response body does not match the expected one.</exception>
285285
public VerifiableResponse Body(string expectedResponseBody)
286286
{
287-
string actualResponseBody = this.Response.Content.ReadAsStringAsync().Result;
287+
string actualResponseBody = this.Response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
288288

289289
if (!actualResponseBody.Equals(expectedResponseBody))
290290
{
@@ -302,7 +302,7 @@ public VerifiableResponse Body(string expectedResponseBody)
302302
/// <exception cref="ResponseVerificationException">Thrown when the actual response body does not match the expected one.</exception>
303303
public VerifiableResponse Body(IMatcher<string> matcher)
304304
{
305-
string actualResponseBody = this.Response.Content.ReadAsStringAsync().Result;
305+
string actualResponseBody = this.Response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
306306

307307
if (!matcher.Matches(actualResponseBody))
308308
{
@@ -322,7 +322,7 @@ public VerifiableResponse Body(IMatcher<string> matcher)
322322
/// <returns>The current <see cref="VerifiableResponse"/> object.</returns>
323323
public VerifiableResponse Body<T>(string path, IMatcher<T> matcher, VerifyAs verifyAs = VerifyAs.UseResponseContentTypeHeaderValue)
324324
{
325-
string responseBodyAsString = this.Response.Content.ReadAsStringAsync().Result;
325+
string responseBodyAsString = this.Response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
326326

327327
string? responseMediaType = string.Empty;
328328

@@ -449,7 +449,7 @@ public VerifiableResponse Body<T>(string path, IMatcher<IEnumerable<T>> matcher,
449449
{
450450
List<T> elementValues = new List<T>();
451451

452-
string responseBodyAsString = this.Response.Content.ReadAsStringAsync().Result;
452+
string responseBodyAsString = this.Response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
453453

454454
string responseMediaType = this.Response.Content.Headers.ContentType?.MediaType ?? string.Empty;
455455

@@ -538,15 +538,12 @@ public VerifiableResponse MatchesJsonSchema(string jsonSchema)
538538

539539
try
540540
{
541-
JsonSchema parsedSchema = JsonSchema.FromJsonAsync(jsonSchema).Result;
541+
JsonSchema parsedSchema = JsonSchema.FromJsonAsync(jsonSchema).GetAwaiter().GetResult();
542542
return this.MatchesJsonSchema(parsedSchema);
543543
}
544-
catch (AggregateException ae)
544+
catch (JsonException je)
545545
{
546-
foreach (Exception ex in ae.InnerExceptions)
547-
{
548-
this.FailVerification($"Could not parse supplied JSON schema. Error: {ex.Message}");
549-
}
546+
this.FailVerification($"Could not parse supplied JSON schema. Error: {je.Message}");
550547
}
551548

552549
return this;
@@ -567,7 +564,7 @@ public VerifiableResponse MatchesJsonSchema(JsonSchema jsonSchema)
567564
this.FailVerification($"Expected response Content-Type header to contain 'json', but was '{responseMediaType}'");
568565
}
569566

570-
string responseBodyAsString = this.Response.Content.ReadAsStringAsync().Result;
567+
string responseBodyAsString = this.Response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
571568

572569
ICollection<ValidationError> schemaValidationErrors = jsonSchema.Validate(responseBodyAsString);
573570

@@ -618,7 +615,7 @@ public VerifiableResponse MatchesXsd(XmlSchemaSet schemas)
618615
settings.ValidationType = ValidationType.Schema;
619616
settings.Schemas = schemas;
620617

621-
string responseXmlAsString = this.Response.Content.ReadAsStringAsync().Result;
618+
string responseXmlAsString = this.Response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
622619
XmlReader reader = XmlReader.Create(new StringReader(responseXmlAsString), settings);
623620

624621
try
@@ -652,7 +649,7 @@ public VerifiableResponse MatchesInlineDtd()
652649
settings.DtdProcessing = DtdProcessing.Parse;
653650
settings.ValidationType = ValidationType.DTD;
654651

655-
string responseXmlAsString = this.Response.Content.ReadAsStringAsync().Result;
652+
string responseXmlAsString = this.Response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
656653
XmlReader reader = XmlReader.Create(new StringReader(responseXmlAsString), settings);
657654

658655
try
@@ -691,7 +688,7 @@ public VerifiableResponse ResponseTime(IMatcher<TimeSpan> matcher)
691688
/// <returns>The current <see cref="VerifiableResponse"/> object.</returns>
692689
public VerifiableResponse ResponseBodyLength(IMatcher<int> matcher)
693690
{
694-
string responseContentAsString = this.Response.Content.ReadAsStringAsync().Result;
691+
string responseContentAsString = this.Response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
695692

696693
if (!matcher.Matches(responseContentAsString.Length))
697694
{

0 commit comments

Comments
 (0)