コード例 #1
0
        public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_IStringMatchers()
        {
            // Assign
            var body = new BodyData
            {
                BodyAsString     = "b",
                DetectedBodyType = BodyType.String
            };
            var stringMatcherMock1 = new Mock <IStringMatcher>();

            stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny <string>())).Returns(0.2d);
            var stringMatcherMock2 = new Mock <IStringMatcher>();

            stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny <string>())).Returns(0.8d);
            var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object };

            var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);

            var matcher = new RequestMessageBodyMatcher(matchers.Cast <IMatcher>().ToArray());

            // Act
            var    result = new RequestMatchResult();
            double score  = matcher.GetMatchingScore(requestMessage, result);

            // Assert
            Check.That(score).IsEqualTo(0.8d);

            // Verify
            stringMatcherMock1.Verify(m => m.GetPatterns(), Times.Never);
            stringMatcherMock1.Verify(m => m.IsMatch("b"), Times.Once);
            stringMatcherMock2.Verify(m => m.GetPatterns(), Times.Never);
            stringMatcherMock2.Verify(m => m.IsMatch("b"), Times.Once);
        }
コード例 #2
0
        public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IStringMatcher()
        {
            // Assign
            var body = new BodyData
            {
                BodyAsBytes      = new byte[] { 1 },
                DetectedBodyType = BodyType.Bytes
            };
            var stringMatcherMock = new Mock <IStringMatcher>();

            stringMatcherMock.Setup(m => m.IsMatch(It.IsAny <string>())).Returns(0.5d);

            var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);

            var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);

            // Act
            var    result = new RequestMatchResult();
            double score  = matcher.GetMatchingScore(requestMessage, result);

            // Assert
            Check.That(score).IsEqualTo(0.0d);

            // Verify
            stringMatcherMock.Verify(m => m.GetPatterns(), Times.Never);
            stringMatcherMock.Verify(m => m.IsMatch(It.IsAny <string>()), Times.Never);
        }
コード例 #3
0
        public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IObjectMatcher(byte[] bytes)
        {
            // Assign
            var body = new BodyData
            {
                BodyAsBytes      = bytes,
                DetectedBodyType = BodyType.Bytes
            };
            var objectMatcherMock = new Mock <IObjectMatcher>();

            objectMatcherMock.Setup(m => m.IsMatch(It.IsAny <object>())).Returns(0.5d);

            var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);

            var matcher = new RequestMessageBodyMatcher(objectMatcherMock.Object);

            // Act
            var    result = new RequestMatchResult();
            double score  = matcher.GetMatchingScore(requestMessage, result);

            // Assert
            Check.That(score).IsEqualTo(0.5d);

            // Verify
            objectMatcherMock.Verify(m => m.IsMatch(It.IsAny <byte[]>()), Times.Once);
        }
コード例 #4
0
        public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_and_BodyAsString_IStringMatcher()
        {
            // Assign
            var body = new BodyData
            {
                BodyAsJson       = new { value = 42 },
                BodyAsString     = "orig",
                DetectedBodyType = BodyType.Json
            };
            var stringMatcherMock = new Mock <IStringMatcher>();

            stringMatcherMock.Setup(m => m.IsMatch(It.IsAny <string>())).Returns(0.5d);

            var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);

            var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);

            // Act
            var    result = new RequestMatchResult();
            double score  = matcher.GetMatchingScore(requestMessage, result);

            // Assert
            Check.That(score).IsEqualTo(0.5d);

            // Verify
            stringMatcherMock.Verify(m => m.IsMatch(It.IsAny <string>()), Times.Once);
        }
コード例 #5
0
        public async Task RequestMessageBodyMatcher_GetMatchingScore_Funcs_Matching(object body, RequestMessageBodyMatcher matcher, bool shouldMatch)
        {
            // assign
            BodyData bodyData;

            if (body is byte[] b)
            {
                bodyData = await BodyParser.Parse(new MemoryStream(b), null);
            }
            else if (body is string s)
            {
                bodyData = await BodyParser.Parse(new MemoryStream(Encoding.UTF8.GetBytes(s)), null);
            }
            else
            {
                throw new Exception();
            }

            var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", bodyData);

            // act
            var result = new RequestMatchResult();
            var score  = matcher.GetMatchingScore(requestMessage, result);

            // assert
            Check.That(score).IsEqualTo(shouldMatch ? 1d : 0d);
        }
コード例 #6
0
        public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_NotNullOrEmptyObjectMatcher(string data, double expected)
        {
            // Assign
            var body = new BodyData
            {
                BodyAsString     = data,
                DetectedBodyType = BodyType.String
            };
            var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);

            var matcher = new RequestMessageBodyMatcher(new NotNullOrEmptyMatcher());

            // Act
            var    result = new RequestMatchResult();
            double score  = matcher.GetMatchingScore(requestMessage, result);

            // Assert
            score.Should().Be(expected);
        }
コード例 #7
0
        public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_CSharpCodeMatcher()
        {
            // Assign
            var body = new BodyData
            {
                BodyAsJson       = new { value = 42 },
                DetectedBodyType = BodyType.Json
            };

            var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);

            var matcher = new RequestMessageBodyMatcher(new CSharpCodeMatcher(MatchBehaviour.AcceptOnMatch, "return it.value == 42;"));

            // Act
            var    result = new RequestMatchResult();
            double score  = matcher.GetMatchingScore(requestMessage, result);

            // Assert
            Check.That(score).IsEqualTo(1.0d);
        }