private static bool IsMockForRequest(RequestMock mock, HttpRequestMessage request, string requestContent) { // It checks if the request method is the expected. if (mock.RequestHttpMethod.ToString() != request.Method.Method) { return(false); } // It validates the URI. if (mock.IsRequestUriARegex) { if (!mock.RequestRegex.IsMatch(request.RequestUri.PathAndQuery)) { return(false); } } else { // https://msdn.microsoft.com/en-us/library/system.uri.pathandquery(v=vs.110).aspx var comparisonResult = Uri.Compare( mock.RequestUri, request.RequestUri, UriComponents.PathAndQuery, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase); if (comparisonResult != 0) { return(false); } // https://msdn.microsoft.com/en-us/library/system.uri.userinfo(v=vs.110).aspx comparisonResult = Uri.Compare( mock.RequestUri, request.RequestUri, UriComponents.UserInfo, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase); if (comparisonResult != 0) { return(false); } // https://msdn.microsoft.com/en-us/library/system.uri.fragment(v=vs.110).aspx comparisonResult = Uri.Compare( mock.RequestUri, request.RequestUri, UriComponents.Fragment, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase); if (comparisonResult != 0) { return(false); } } // It validates the request headers. foreach (var expectedHeader in mock.ExpectedRequestHeaders) { IEnumerable <string> headerValues; if (request.Headers.TryGetValues(expectedHeader.Key, out headerValues) || request.Content.Headers.TryGetValues(expectedHeader.Key, out headerValues)) { if (headerValues.Contains(expectedHeader.Value)) { continue; } } return(false); } // It validates the request content type. if (string.IsNullOrWhiteSpace(mock.ExpectedRequestContentType) == false) { if (request.Content.Headers.ContentType == null) { return(false); } var expectedContentTypes = mock.ExpectedRequestContentType.Split(','); if (!expectedContentTypes.Contains(request.Content.Headers.ContentType.ToString())) { return(false); } ////if (!request.Content.Headers.ContentType.Equals(new MediaTypeHeaderValue(mock.ExpectedRequestContentType))) ////{ //// return false; ////} } // It validates the expected request content. // TODO: At this moment, it only compares JSON requests, in the future, it should be able to compare XML, FormUrlEncode and Text requests. if (mock.ExpectedRequestContent != null) { if (string.IsNullOrWhiteSpace(requestContent)) { return(false); } if (Helper.IsJsonRequest(request.Content.Headers.ContentType.ToString())) { var requestJson = JToken.Parse(requestContent); JToken expectedJson; if (mock.ExpectedRequestContent is string) { expectedJson = JToken.Parse(((string)mock.ExpectedRequestContent)); } else { expectedJson = JToken.FromObject(mock.ExpectedRequestContent); } if (!JToken.DeepEquals(requestJson, expectedJson)) { return(false); } } else if (Helper.IsXmlRequest(request.Content.Headers.ContentType.ToString())) { // TODO: Shit code. It must be improved. At this moment, it works but it's a shit. // ---------------------------------------------------------------------------------------------------------------------- JToken expectedJson; if (mock.ExpectedRequestContent is string) { var xmlContent = XDocument.Parse((string)mock.ExpectedRequestContent); expectedJson = JToken.Parse(JsonConvert.SerializeXNode(xmlContent.Root)).First.First; } else { var xmlContent = JsonConvert.DeserializeXNode(JsonConvert.SerializeObject(mock.ExpectedRequestContent), "Root"); expectedJson = JToken.Parse(JsonConvert.SerializeXNode(xmlContent.Root)).First.First; } var requestJson = JToken.Parse(JsonConvert.SerializeXNode(XDocument.Parse(requestContent))).First.First; if (!JToken.DeepEquals(requestJson, expectedJson)) { return(false); } // ---------------------------------------------------------------------------------------------------------------------- } else { if (mock.ExpectedRequestContent is string) { if (requestContent != mock.ExpectedRequestContent.ToString()) { return(false); } } // TODO: Throw an exception??? } ////case HContentType.FormUrlEncoded: //// { //// // TODO: Shit code. It must be improved. At this moment, it works but it's a shit. //// // ---------------------------------------------------------------------------------------------------------------------- //// var urlEncodedContent = request.Content.ReadAsFormDataAsync().Result; //// for (int i = 0; i < urlEncodedContent.Count; i++) //// { //// } //// var json = JToken.Parse(JsonConvert.SerializeObject(urlEncodedContent)); //// break; //// // ---------------------------------------------------------------------------------------------------------------------- //// } } // It executes the custom request validator configured by the user. if (mock.RequestValidator != null && !mock.RequestValidator(request)) { return(false); } return(true); }