From a61146e5f2cd15278981c45546abc9cd09207ef8 Mon Sep 17 00:00:00 2001 From: Marcin Celej Date: Thu, 16 May 2024 09:51:55 +0200 Subject: [PATCH] #18: Synergy.Web.Api.Testing: Introduced reading of request headers from saved pattern instead of getting them from live request. This affects only WAPIT markdown generation. --- .../Assertions/CompareOperationWithPattern.cs | 28 ++++++++++++++++--- Web/Synergy.Web.Api.Testing/HttpExtensions.cs | 11 +++++++- ...Api.of.Synergy.Web.Api.Testing.verified.md | 6 ++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Web/Synergy.Web.Api.Testing/Assertions/CompareOperationWithPattern.cs b/Web/Synergy.Web.Api.Testing/Assertions/CompareOperationWithPattern.cs index e57845d..424c457 100644 --- a/Web/Synergy.Web.Api.Testing/Assertions/CompareOperationWithPattern.cs +++ b/Web/Synergy.Web.Api.Testing/Assertions/CompareOperationWithPattern.cs @@ -152,9 +152,9 @@ HttpRequestMessage IHttpRequestStorage.GetSavedRequest() { Fail.IfNull(_savedPattern); - var fullMethod = _savedPattern!.SelectToken("$.request.method").Value(); - var method = fullMethod.Substring(0, fullMethod.IndexOf(" ")); - var url = fullMethod.Substring(fullMethod.IndexOf(" ")); + var fullMethod = _savedPattern!.SelectToken("$.request.method").OrFail().Value(); + var method = fullMethod.OrFail().Substring(0, fullMethod.IndexOf(" ", StringComparison.InvariantCulture)); + var url = fullMethod.Substring(fullMethod.IndexOf(" ", StringComparison.InvariantCulture)); var request = new HttpRequestMessage(new HttpMethod(method), url); var body = _savedPattern!.SelectToken("$.request.body"); @@ -163,6 +163,26 @@ HttpRequestMessage IHttpRequestStorage.GetSavedRequest() var payload = body.ToString(); request.Content = new StringContent(payload, Encoding.UTF8, MediaTypeNames.Application.Json); } + + var headers = _savedPattern!.SelectTokens("$.request.headers.*"); + foreach (var header in headers) + { + var headerName = header.Path.Replace("request.headers.", ""); + var headerValue = header.Value(); + + if (headerName.StartsWith("Content") && request.Content != null) + { + if (request.Content.Headers.Contains(headerName)) + { + request.Content.Headers.Remove(headerName); + } + + request.Content.Headers.Add(headerName, headerValue); + continue; + } + + request.Headers.Add(headerName, headerValue); + } return request; } @@ -172,7 +192,7 @@ HttpResponseMessage IHttpResponseStorage.GetSavedResponse() Fail.IfNull(_savedPattern); var fullStatus = _savedPattern!.SelectToken("$.response.status").Value(); - var status = fullStatus.Substring(0, fullStatus.IndexOf(" ")); + var status = fullStatus.Substring(0, fullStatus.IndexOf(" ", StringComparison.InvariantCulture)); var statusCode = Enum.Parse(status); var response = new HttpResponseMessage(statusCode); var body = _savedPattern!.SelectToken("$.response.body")?.ToString(); diff --git a/Web/Synergy.Web.Api.Testing/HttpExtensions.cs b/Web/Synergy.Web.Api.Testing/HttpExtensions.cs index 679ffb9..a8dc107 100644 --- a/Web/Synergy.Web.Api.Testing/HttpExtensions.cs +++ b/Web/Synergy.Web.Api.Testing/HttpExtensions.cs @@ -84,6 +84,15 @@ public static List>> GetAllRequestHeade return headers.ToList(); } + public static List>> GetAllHeaders(this HttpRequestMessage response) + { + var headers = response.Headers.ToList(); + if (response.Content != null) + headers.AddRange(response.Content.Headers); + + return headers; + } + public static List>> GetAllHeaders(this HttpResponseMessage response) { var headers = response.Headers.ToList(); @@ -100,7 +109,7 @@ public static string ToHttpLook(this HttpRequestMessage request, HttpOperation o { var report = new StringBuilder(); report.AppendLine(request.GetRequestFullMethod()); - InsertHeaders(report, operation.GetAllRequestHeaders()); + InsertHeaders(report, request.GetAllHeaders()); var requestBody = request.Content.ReadJson(); if (requestBody != null) { diff --git a/Web/Synergy.Web.Api.Tests/Architecture/Public/Api.of.Synergy.Web.Api.Testing.verified.md b/Web/Synergy.Web.Api.Tests/Architecture/Public/Api.of.Synergy.Web.Api.Testing.verified.md index 349b012..0aa6c1e 100644 --- a/Web/Synergy.Web.Api.Tests/Architecture/Public/Api.of.Synergy.Web.Api.Testing.verified.md +++ b/Web/Synergy.Web.Api.Tests/Architecture/Public/Api.of.Synergy.Web.Api.Testing.verified.md @@ -218,6 +218,9 @@ ) : void ## HttpExtensions (abstract class) + - HttpExtensions.GetAllHeaders( + response: HttpRequestMessage + ) : List>> [Extension] - HttpExtensions.GetAllHeaders( response: HttpResponseMessage ) : List>> [Extension] @@ -286,6 +289,9 @@ nodes: params String[] [ParamArray] ) : Ignore - Ignore.RequestDescription() : Ignore + - Ignore.RequestHeaders( + headers: params String[] [ParamArray] + ) : Ignore - Ignore.RequestMethod() : Ignore - Ignore.ResponseBody( nodes: params String[] [ParamArray]