Skip to content

Commit

Permalink
Updating BodyComparer to display actual and expected in the correct o…
Browse files Browse the repository at this point in the history
…rder and refactoring the naming inside the comparer to be more explicit.
  • Loading branch information
neilcampbell committed Aug 28, 2014
1 parent 809e27d commit cf05c8f
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 61 deletions.
2 changes: 1 addition & 1 deletion PactNet/Comparers/IComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface IComparer<in T>
{
void Compare(T item1, T item2);
void Compare(T expected, T actual);
}
}
20 changes: 10 additions & 10 deletions PactNet/Mocks/MockHttpService/Comparers/HttpBodyComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,36 @@ public HttpBodyComparer(string messagePrefix, IReporter reporter)
}

//TODO: Remove boolean and add "matching" functionality
public void Validate(dynamic body1, dynamic body2, bool useStrict = false)
public void Validate(dynamic expected, dynamic actual, bool useStrict = false)
{
_comparisonPasses = 0;

if (body1 == null)
if (expected == null)
{
return;
}

if (body1 != null && body2 == null)
if (expected != null && actual == null)
{
_reporter.ReportError("Body is null");
return;
}

string body1Json = JsonConvert.SerializeObject(body1);
string body2Json = JsonConvert.SerializeObject(body2);
var httpBody1 = JsonConvert.DeserializeObject<JToken>(body1Json);
var httpBody2 = JsonConvert.DeserializeObject<JToken>(body2Json);
string expectedJson = JsonConvert.SerializeObject(expected);
string actualJson = JsonConvert.SerializeObject(actual);
var expectedToken = JsonConvert.DeserializeObject<JToken>(expectedJson);
var actualToken = JsonConvert.DeserializeObject<JToken>(actualJson);

if (useStrict)
{
if (!JToken.DeepEquals(httpBody1, httpBody2))
if (!JToken.DeepEquals(expectedToken, actualToken))
{
_reporter.ReportError(expected: httpBody1, actual: httpBody2);
_reporter.ReportError(expected: expectedToken, actual: actualToken);
}
return;
}

AssertPropertyValuesMatch(httpBody1, httpBody2);
AssertPropertyValuesMatch(expectedToken, actualToken);
}


Expand Down
32 changes: 16 additions & 16 deletions PactNet/Mocks/MockHttpService/Comparers/HttpHeaderComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,44 @@ public HttpHeaderComparer(string messagePrefix, IReporter reporter)
_reporter = reporter;
}

public void Compare(IDictionary<string, string> headers1, IDictionary<string, string> headers2)
public void Compare(IDictionary<string, string> expected, IDictionary<string, string> actual)
{
if (headers2 == null)
if (actual == null)
{
_reporter.ReportError("Headers are null");
return;
}

headers2 = MakeDictionaryCaseInsensitive(headers2);
actual = MakeDictionaryCaseInsensitive(actual);

foreach (var header in headers1)
foreach (var header in expected)
{
_reporter.ReportInfo(String.Format("{0} includes header {1} with value {2}", _messagePrefix, header.Key, header.Value));

string value2;
string actualValue;

if (headers2.TryGetValue(header.Key, out value2))
if (actual.TryGetValue(header.Key, out actualValue))
{
var value1 = header.Value;
var expectedValue = header.Value;

var value2Split = value2.Split(new[] {',', ';'});
if (value2Split.Length == 1)
var actualValueSplit = actualValue.Split(new[] { ',', ';' });
if (actualValueSplit.Length == 1)
{
if (!header.Value.Equals(value2))
if (!header.Value.Equals(actualValue))
{
_reporter.ReportError(expected: header.Value, actual: value2);
_reporter.ReportError(expected: expectedValue, actual: actualValue);
return;
}
}
else
{
var value1Split = value1.Split(new[] {',', ';'});
var value1SplitJoined = String.Join(",", value1Split.Select(x => x.Trim()));
var value2SplitJoined = String.Join(",", value2Split.Select(x => x.Trim()));
var expectedValueSplit = expectedValue.Split(new[] {',', ';'});
var expectedValueSplitJoined = String.Join(",", expectedValueSplit.Select(x => x.Trim()));
var actualValueSplitJoined = String.Join(",", actualValueSplit.Select(x => x.Trim()));

if (!value1SplitJoined.Equals(value2SplitJoined))
if (!expectedValueSplitJoined.Equals(actualValueSplitJoined))
{
_reporter.ReportError(expected: header.Value, actual: value2);
_reporter.ReportError(expected: expectedValue, actual: actualValue);
return;
}
}
Expand Down
8 changes: 4 additions & 4 deletions PactNet/Mocks/MockHttpService/Comparers/HttpMethodComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public HttpMethodComparer(string messagePrefix, IReporter reporter)
_reporter = reporter;
}

public void Compare(HttpVerb method1, HttpVerb method2)
public void Compare(HttpVerb expected, HttpVerb actual)
{
_reporter.ReportInfo(String.Format("{0} has method set to {1}", _messagePrefix, method1));
if (!method1.Equals(method2))
_reporter.ReportInfo(String.Format("{0} has method set to {1}", _messagePrefix, expected));
if (!expected.Equals(actual))
{
_reporter.ReportError(expected: method1, actual: method2);
_reporter.ReportError(expected: expected, actual: actual);
return;
}
}
Expand Down
10 changes: 5 additions & 5 deletions PactNet/Mocks/MockHttpService/Comparers/HttpPathComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ public HttpPathComparer(string messagePrefix, IReporter reporter)
_reporter = reporter;
}

public void Compare(string path1, string path2)
public void Compare(string expected, string actual)
{
if (path1 == null)
if (expected == null)
{
return;
}

_reporter.ReportInfo(String.Format("{0} has path set to {1}", _messagePrefix, path1));
_reporter.ReportInfo(String.Format("{0} has path set to {1}", _messagePrefix, expected));

if (!path1.Equals(path2))
if (!expected.Equals(actual))
{
_reporter.ReportError(expected: path1, actual: path2);
_reporter.ReportError(expected: expected, actual: actual);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public HttpQueryStringComparer(string messagePrefix, IReporter reporter)
_reporter = reporter;
}

public void Compare(string expectedQuery, string actualQuery)
public void Compare(string expected, string actual)
{
if (expectedQuery == null)
if (expected == null)
{
return;
}

var normalisedExpectedQuery = ConvertUrlEncodingToUpperCase(expectedQuery);
var normalisedActualQuery = ConvertUrlEncodingToUpperCase(actualQuery);
var normalisedExpectedQuery = ConvertUrlEncodingToUpperCase(expected);
var normalisedActualQuery = ConvertUrlEncodingToUpperCase(actual);

_reporter.ReportInfo(String.Format("{0} has query set to {1}", _messagePrefix, normalisedExpectedQuery));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface IHttpBodyComparer
{
void Validate(dynamic body1, dynamic body2, bool useStrict = false);
void Validate(dynamic expected, dynamic actual, bool useStrict = false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,33 @@ public ProviderServiceRequestComparer(IReporter reporter)
_httpBodyComparer = new HttpBodyComparer(MessagePrefix, _reporter);
}

public void Compare(ProviderServiceRequest request1, ProviderServiceRequest request2)
public void Compare(ProviderServiceRequest expected, ProviderServiceRequest actual)
{
if (request1 == null)
if (expected == null)
{
_reporter.ReportError("Expected request cannot be null");
return;
}

_httpMethodComparer.Compare(request1.Method, request2.Method);
_httpMethodComparer.Compare(expected.Method, actual.Method);

_httpPathComparer.Compare(request1.Path, request2.Path);
_httpPathComparer.Compare(expected.Path, actual.Path);

_httpQueryStringComparer.Compare(request1.Query, request2.Query);
_httpQueryStringComparer.Compare(expected.Query, actual.Query);

if (request1.Headers != null && request1.Headers.Any())
if (expected.Headers != null && expected.Headers.Any())
{
if (request2.Headers == null)
if (actual.Headers == null)
{
_reporter.ReportError("Headers are null");
}

_httpHeaderComparer.Compare(request1.Headers, request2.Headers);
_httpHeaderComparer.Compare(expected.Headers, actual.Headers);
}

if (request1.Body != null)
if (expected.Body != null)
{
_httpBodyComparer.Validate(request2.Body, request1.Body, true);
_httpBodyComparer.Validate(expected.Body, actual.Body, true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ public ProviderServiceResponseComparer(IReporter reporter)
_httpBodyComparer = new HttpBodyComparer(MessagePrefix, _reporter); //TODO: MessagePrefix isn't real nice
}

public void Compare(ProviderServiceResponse response1, ProviderServiceResponse response2)
public void Compare(ProviderServiceResponse expected, ProviderServiceResponse actual)
{
if (response1 == null)
if (expected == null)
{
_reporter.ReportError("Expected response cannot be null");
return;
}

_reporter.ReportInfo(String.Format("{0} has status code of {1}", MessagePrefix, response1.Status));
if (!response1.Status.Equals(response2.Status))
_reporter.ReportInfo(String.Format("{0} has status code of {1}", MessagePrefix, expected.Status));
if (!expected.Status.Equals(actual.Status))
{
_reporter.ReportError(expected: response1.Status, actual: response2.Status);
_reporter.ReportError(expected: expected.Status, actual: actual.Status);
}

if (response1.Headers != null && response1.Headers.Any())
if (expected.Headers != null && expected.Headers.Any())
{
_httpHeaderComparer.Compare(response1.Headers, response2.Headers);
_httpHeaderComparer.Compare(expected.Headers, actual.Headers);
}

if (response1.Body != null)
if (expected.Body != null)
{
string expectedResponseBodyJson = JsonConvert.SerializeObject(response1.Body);
string actualResponseBodyJson = JsonConvert.SerializeObject(response2.Body);
string expectedResponseBodyJson = JsonConvert.SerializeObject(expected.Body);
string actualResponseBodyJson = JsonConvert.SerializeObject(actual.Body);
var actualResponseBody = JsonConvert.DeserializeObject<JToken>(actualResponseBodyJson);
var expectedResponseBody = JsonConvert.DeserializeObject<JToken>(expectedResponseBodyJson);

Expand Down

0 comments on commit cf05c8f

Please sign in to comment.