private double IsMatch(RequestMessage requestMessage)
 {
     return(MatchScores.ToScore(Methods.Contains(requestMessage.Method)));
 }
예제 #2
0
 private double IsMatch(IRequestMessage requestMessage)
 {
     return(MatchScores.ToScore(Methods.Contains(requestMessage.Method, StringComparer.OrdinalIgnoreCase)));
 }
예제 #3
0
        private double IsMatch(RequestMessage requestMessage)
        {
            // Check if the matcher is a IObjectMatcher
            if (Matcher is IObjectMatcher objectMatcher)
            {
                // If the body is a JSON object, try to match.
                if (requestMessage.BodyAsJson != null)
                {
                    return(objectMatcher.IsMatch(requestMessage.BodyAsJson));
                }

                // If the body is a byte array, try to match.
                if (requestMessage.BodyAsBytes != null)
                {
                    return(objectMatcher.IsMatch(requestMessage.BodyAsBytes));
                }
            }

            // Check if the matcher is a IStringMatcher
            if (Matcher is IStringMatcher stringMatcher)
            {
                // If the  body is a JSON object, try to use Body (string) to match.
                if (requestMessage.BodyAsJson != null && requestMessage.Body != null)
                {
                    return(stringMatcher.IsMatch(requestMessage.Body));
                }

                // If the string body is defined, try to match.
                if (requestMessage.Body != null)
                {
                    return(stringMatcher.IsMatch(requestMessage.Body));
                }

                // If the body is a byte array, attempt to convert it and match on that
                if (requestMessage.BodyAsBytes != null)
                {
                    try // to convert from the byte[] to a string
                    {
                        var input = Encoding.UTF8.GetString(requestMessage.BodyAsBytes);
                        return(stringMatcher.IsMatch(input));
                    }
                    catch
                    {
                        // just ignore the exception
                    }
                }
            }

            if (Func != null)
            {
                return(MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body)));
            }

            if (DataFunc != null)
            {
                return(MatchScores.ToScore(requestMessage.BodyAsBytes != null && DataFunc(requestMessage.BodyAsBytes)));
            }

            if (JsonFunc != null)
            {
                return(MatchScores.ToScore(requestMessage.BodyAsJson != null && JsonFunc(requestMessage.BodyAsJson)));
            }

            return(MatchScores.Mismatch);
        }