Skip to content

Commit

Permalink
Improving HttpMockHelper comparison (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddynaka authored Aug 13, 2021
1 parent 336afda commit 28b53ca
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion Src/Plugins/Tests.Security/Helpers/HttpMockHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -44,6 +45,28 @@ public static HttpMessageHandler Mock(HttpStatusCode httpStatusCode, HttpContent
return mockMessageHandler.Object;
}

public bool CompareHeaders(HttpRequestHeaders headers1, HttpRequestHeaders headers2)
{
foreach (KeyValuePair<string, IEnumerable<string>> header in headers1)
{
string headerName = header.Key;
if (!headers2.TryGetValues(headerName, out IEnumerable<string> values))
{
return false;
}

string headerContent1 = string.Join(",", header.Value);
string headerContent2 = string.Join(",", values);

if (headerContent1 != headerContent2)
{
return false;
}
}

return true;
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Tuple<HttpRequestMessage, HttpStatusCode, HttpContent> fakeResponse;
Expand All @@ -58,7 +81,7 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
{
fakeResponse = _fakeResponses.Find(fr =>
fr.Item1.RequestUri == request.RequestUri
&& request.Headers.Authorization.Equals(fr.Item1.Headers.Authorization));
&& CompareHeaders(request.Headers, fr.Item1.Headers));
}

return Task.FromResult(
Expand Down

0 comments on commit 28b53ca

Please sign in to comment.