Skip to content

Commit

Permalink
#18: Synergy.Web.Api.Testing: Introduced reading of request headers f…
Browse files Browse the repository at this point in the history
…rom saved pattern instead of getting them from live request.

This affects only WAPIT markdown generation.
  • Loading branch information
MarcinCelej committed May 16, 2024
1 parent 7c70e0c commit a61146e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ HttpRequestMessage IHttpRequestStorage.GetSavedRequest()
{
Fail.IfNull(_savedPattern);

var fullMethod = _savedPattern!.SelectToken("$.request.method").Value<string>();
var method = fullMethod.Substring(0, fullMethod.IndexOf(" "));
var url = fullMethod.Substring(fullMethod.IndexOf(" "));
var fullMethod = _savedPattern!.SelectToken("$.request.method").OrFail().Value<string>();
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");
Expand All @@ -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<string>();

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;
}
Expand All @@ -172,7 +192,7 @@ HttpResponseMessage IHttpResponseStorage.GetSavedResponse()
Fail.IfNull(_savedPattern);

var fullStatus = _savedPattern!.SelectToken("$.response.status").Value<string>();
var status = fullStatus.Substring(0, fullStatus.IndexOf(" "));
var status = fullStatus.Substring(0, fullStatus.IndexOf(" ", StringComparison.InvariantCulture));
var statusCode = Enum.Parse<HttpStatusCode>(status);
var response = new HttpResponseMessage(statusCode);
var body = _savedPattern!.SelectToken("$.response.body")?.ToString();
Expand Down
11 changes: 10 additions & 1 deletion Web/Synergy.Web.Api.Testing/HttpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ public static List<KeyValuePair<string, IEnumerable<string>>> GetAllRequestHeade
return headers.ToList();
}

public static List<KeyValuePair<string, IEnumerable<string>>> GetAllHeaders(this HttpRequestMessage response)
{
var headers = response.Headers.ToList();
if (response.Content != null)
headers.AddRange(response.Content.Headers);

return headers;
}

public static List<KeyValuePair<string, IEnumerable<string>>> GetAllHeaders(this HttpResponseMessage response)
{
var headers = response.Headers.ToList();
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@
) : void

## HttpExtensions (abstract class)
- HttpExtensions.GetAllHeaders(
response: HttpRequestMessage
) : List<KeyValuePair<string, IEnumerable<string>>> [Extension]
- HttpExtensions.GetAllHeaders(
response: HttpResponseMessage
) : List<KeyValuePair<string, IEnumerable<string>>> [Extension]
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit a61146e

Please sign in to comment.