Beispiel #1
0
        public void ThenSetValue_Perform_SetHttpRequestUriTest()
        {
            string textValue = "/Path";

            Mock <EventInfo>             mockEventInfo      = new Mock <EventInfo>();
            Mock <HttpMessage>           mockHttpMessage    = new Mock <HttpMessage>();
            Mock <HttpRequestStatusLine> mockHttpStatusLine = new Mock <HttpRequestStatusLine>();
            Mock <VariableString>        mockTextString     = new Mock <VariableString>(It.IsAny <string>(), null);

            EventInfo             eventInfo      = mockEventInfo.Object;
            HttpMessage           httpMessage    = mockHttpMessage.Object;
            HttpRequestStatusLine httpStatusLine = mockHttpStatusLine.Object;
            VariableString        textString     = mockTextString.Object;

            mockTextString.Setup(mock => mock.GetText(It.IsAny <Variables>())).Returns(textValue);
            mockEventInfo.Setup(mock => mock.Message).Returns(httpMessage);
            mockHttpMessage.Setup(mock => mock.StatusLine).Returns(httpStatusLine);


            ThenSetValue then = new ThenSetValue()
            {
                Text = textString,
                DestinationMessageValue = MessageValue.HttpRequestUri
            };

            Assert.AreEqual(ThenResponse.Continue, then.Perform(eventInfo));

            mockHttpStatusLine.VerifySet(mock => mock.Uri = textValue);
        }
Beispiel #2
0
        public void WhenHasEntity_IsMatch_HttpStatusLineTest()
        {
            WhenHasEntity when = new WhenHasEntity()
            {
                MessageValue = MessageValue.HttpStatusLine
            };

            Mock <EventInfo>             mockEventInfo             = new Mock <EventInfo>();
            Mock <HttpMessage>           mockHttpMessage           = new Mock <HttpMessage>();
            Mock <HttpRequestStatusLine> mockHttpRequestStatusLine = new Mock <HttpRequestStatusLine>();

            EventInfo             eventInfo             = mockEventInfo.Object;
            HttpMessage           httpMessage           = mockHttpMessage.Object;
            HttpRequestStatusLine httpRequestStatusLine = mockHttpRequestStatusLine.Object;

            mockEventInfo.Setup(mock => mock.Message).Returns(httpMessage);

            Assert.IsFalse(when.IsMatch(eventInfo));

            mockHttpRequestStatusLine.Reset();

            mockHttpMessage.Setup(mock => mock.StatusLine).Returns(httpRequestStatusLine);

            Assert.IsTrue(when.IsMatch(eventInfo));
        }
        private HttpStatusLine ParseRequestStatusLine(string[] sections)
        {
            HttpStatusLine statusLine = new HttpRequestStatusLine
            {
                Method  = sections[0],
                Uri     = sections[1],
                Version = sections[2]
            };

            return(statusLine);
        }
Beispiel #4
0
        public void WhenMatchesText_IsMatch_MessageValue_SimpleStringTests()
        {
            string sourceString = "TestString";

            List <MessageValue> testableValues = new List <MessageValue>
            {
                MessageValue.DestinationRemoteAddress,
                MessageValue.HttpMethod,
                MessageValue.HttpRequestUri,
                MessageValue.LocalAddress,
                MessageValue.Protocol,
                MessageValue.SourceRemoteAddress,
                MessageValue.HttpBody,
                MessageValue.Message,
                MessageValue.HttpHeaders,
                MessageValue.HttpStatusLine,
                MessageValue.HttpVersion,
                MessageValue.HttpStatusMessage
            };

            Mock <EventInfo>              mockEventInfo              = new Mock <EventInfo>();
            Mock <ProxyInfo>              mockProxyInfo              = new Mock <ProxyInfo>();
            Mock <ProxyConnection>        mockProxyConnection        = new Mock <ProxyConnection>(It.IsAny <ProxyHost>(), It.IsAny <ProxyInfo>(), It.IsAny <TcpClient>());
            Mock <HttpMessage>            mockHttpMessage            = new Mock <HttpMessage>();
            Mock <HttpResponseStatusLine> mockHttpResponseStatusLine = new Mock <HttpResponseStatusLine>();
            Mock <HttpRequestStatusLine>  mockHttpRequestStatusLine  = new Mock <HttpRequestStatusLine>();
            Mock <HttpBody>    mockHttpBody    = new Mock <HttpBody>();
            Mock <HttpHeaders> mockHttpHeaders = new Mock <HttpHeaders>();
            Mock <Channel>     mockChannel     = new Mock <Channel>(It.IsAny <TcpClient>());
            Mock <IPEndPoint>  mockIPEndPoint  = new Mock <IPEndPoint>(It.IsAny <long>(), It.IsAny <int>());
            Mock <IPAddress>   mockIPAddress   = new Mock <IPAddress>(It.IsAny <long>());

            EventInfo              eventInfo          = mockEventInfo.Object;
            ProxyConnection        proxyConnection    = mockProxyConnection.Object;
            ProxyInfo              proxyInfo          = mockProxyInfo.Object;
            HttpMessage            httpMessage        = mockHttpMessage.Object;
            HttpResponseStatusLine responseStatusLine = mockHttpResponseStatusLine.Object;
            HttpRequestStatusLine  requestStatusLine  = mockHttpRequestStatusLine.Object;
            HttpBody    httpBody    = mockHttpBody.Object;
            HttpHeaders httpHeaders = mockHttpHeaders.Object;
            Channel     channel     = mockChannel.Object;
            IPEndPoint  ipEndpoint  = mockIPEndPoint.Object;
            IPAddress   ipAddress   = mockIPAddress.Object;

            mockEventInfo.Setup(mock => mock.ProxyConnection).Returns(proxyConnection);
            mockEventInfo.Setup(mock => mock.Message).Returns(httpMessage);
            mockProxyConnection.Setup(mock => mock.HasTargetConnection).Returns(true);
            mockProxyConnection.Setup(mock => mock.HasOriginConnection).Returns(true);
            mockProxyConnection.Setup(mock => mock.OriginChannel).Returns(channel);
            mockProxyConnection.Setup(mock => mock.TargetChannel).Returns(channel);
            mockProxyConnection.Setup(mock => mock.ProxyInfo).Returns(proxyInfo);
            mockChannel.Setup(mock => mock.LocalEndpoint).Returns(ipEndpoint);
            mockChannel.Setup(mock => mock.RemoteEndpoint).Returns(ipEndpoint);
            ipEndpoint.Address = ipAddress;
            mockHttpHeaders.Setup(mock => mock.ToString()).Returns(sourceString);
            mockHttpBody.Setup(mock => mock.ToString()).Returns(sourceString);
            mockIPAddress.Setup(mock => mock.ToString()).Returns(sourceString);
            mockHttpResponseStatusLine.Setup(mock => mock.ToString()).Returns(sourceString);
            mockHttpResponseStatusLine.Setup(mock => mock.StatusMessage).Returns(sourceString);
            mockHttpResponseStatusLine.Setup(mock => mock.Version).Returns(sourceString);
            mockHttpRequestStatusLine.Setup(mock => mock.ToString()).Returns(sourceString);
            mockHttpRequestStatusLine.Setup(mock => mock.Method).Returns(sourceString);
            mockHttpRequestStatusLine.Setup(mock => mock.Uri).Returns(sourceString);
            mockHttpRequestStatusLine.Setup(mock => mock.Version).Returns(sourceString);

            var testCases = new[]
            {
                new
                {
                    MatchString = GetMockVariableString("Test"),
                    MatchType   = MatchType.BeginsWith,
                    Matches     = true
                },
                new
                {
                    MatchString = GetMockVariableString("est"),
                    MatchType   = MatchType.Contains,
                    Matches     = true
                },
                new
                {
                    MatchString = GetMockVariableString("ing"),
                    MatchType   = MatchType.EndsWith,
                    Matches     = true
                },
                new
                {
                    MatchString = GetMockVariableString("TestString"),
                    MatchType   = MatchType.Equals,
                    Matches     = true
                },
                new
                {
                    MatchString = GetMockVariableString(".*Str.*"),
                    MatchType   = MatchType.Regex,
                    Matches     = true
                },
                new
                {
                    MatchString = GetMockVariableString("MatchString"),
                    MatchType   = MatchType.BeginsWith,
                    Matches     = false
                },
                new
                {
                    MatchString = GetMockVariableString("MatchString"),
                    MatchType   = MatchType.Contains,
                    Matches     = false
                },
                new
                {
                    MatchString = GetMockVariableString("MatchString"),
                    MatchType   = MatchType.EndsWith,
                    Matches     = false
                },
                new
                {
                    MatchString = GetMockVariableString("MatchString"),
                    MatchType   = MatchType.Equals,
                    Matches     = false
                },
                new
                {
                    MatchString = GetMockVariableString("MatchString"),
                    MatchType   = MatchType.Regex,
                    Matches     = false
                }
            };

            foreach (MessageValue messageValue in testableValues)
            {
                mockHttpMessage.Reset();

                switch (messageValue)
                {
                case MessageValue.HttpStatusLine:
                case MessageValue.HttpVersion:
                case MessageValue.HttpStatusMessage:
                    mockHttpMessage.Setup(mock => mock.Body).Returns(httpBody);
                    mockHttpMessage.Setup(mock => mock.Headers).Returns(httpHeaders);
                    mockHttpMessage.Setup(mock => mock.RawText).Returns(sourceString);
                    mockHttpMessage.Setup(mock => mock.Protocol).Returns(sourceString);
                    mockHttpMessage.Setup(mock => mock.ToString()).Returns(sourceString);
                    mockHttpMessage.Setup(mock => mock.StatusLine).Returns(responseStatusLine);
                    break;

                default:
                    mockHttpMessage.Setup(mock => mock.Body).Returns(httpBody);
                    mockHttpMessage.Setup(mock => mock.Headers).Returns(httpHeaders);
                    mockHttpMessage.Setup(mock => mock.RawText).Returns(sourceString);
                    mockHttpMessage.Setup(mock => mock.Protocol).Returns(sourceString);
                    mockHttpMessage.Setup(mock => mock.ToString()).Returns(sourceString);
                    mockHttpMessage.Setup(mock => mock.StatusLine).Returns(requestStatusLine);
                    break;
                }

                foreach (var testCase in testCases)
                {
                    WhenMatchesText when = new WhenMatchesText()
                    {
                        MessageValue    = messageValue,
                        UseMessageValue = true,
                        MatchText       = testCase.MatchString,
                        MatchType       = testCase.MatchType
                    };

                    Assert.AreEqual(testCase.Matches, when.IsMatch(eventInfo));
                }
            }
        }
Beispiel #5
0
        public void SetValue(EventInfo eventInfo, MessageValue messageValue, MessageValueType valueType, VariableString identifier, string replacementText)
        {
            switch (messageValue)
            {
            case MessageValue.DataDirection:
            {
                DataDirection dataDirection;
                if (Enum.TryParse(replacementText, out dataDirection))
                {
                    eventInfo.Direction = dataDirection;
                }
            }
            break;

            case MessageValue.HttpBody:
            {
                HttpMessage httpMessage = eventInfo.Message as HttpMessage;
                if (httpMessage != null)
                {
                    if (valueType != MessageValueType.Text && identifier != null)
                    {
                        switch (valueType)
                        {
                        case MessageValueType.Json:
                            replacementText = httpMessage.Body.Text.SetJsonValue(identifier.GetText(eventInfo.Variables), replacementText);
                            break;

                        case MessageValueType.Xml:
                            replacementText = httpMessage.Body.Text.SetXmlValue(identifier.GetText(eventInfo.Variables), replacementText);
                            break;
                        }
                    }
                    httpMessage.Body = new HttpBody()
                    {
                        Text = replacementText
                    };
                    if (GeneralSettings.AutoUpdateContentLength)
                    {
                        if (httpMessage.Headers.Contains("Content-Length"))
                        {
                            httpMessage.Headers["Content-Length"] = httpMessage.Body.Text.Length.ToString();
                        }
                    }
                }
            }
            break;

            case MessageValue.HttpHeader:
            {
                HttpMessage httpMessage = eventInfo.Message as HttpMessage;
                if (httpMessage != null && identifier != null)
                {
                    httpMessage.Headers[identifier.GetText(eventInfo.Variables)] = replacementText;
                }
            }
            break;

            case MessageValue.HttpHeaders:
            {
                HttpMessage httpMessage = eventInfo.Message as HttpMessage;
                if (httpMessage != null)
                {
                    _httpHeadersParser.Parse(eventInfo, replacementText, true);
                }
            }
            break;

            case MessageValue.HttpStatusLine:
            {
                HttpMessage httpMessage = eventInfo.Message as HttpMessage;
                if (httpMessage != null)
                {
                    HttpStatusLine newHttpStatusLine = _httpStatusLineParser.Parse(replacementText);
                    httpMessage.StatusLine = newHttpStatusLine;
                }
            }
            break;

            case MessageValue.HttpMethod:
            {
                HttpRequestStatusLine httpStatusLine = (eventInfo.Message as HttpMessage)?.StatusLine as HttpRequestStatusLine;
                if (httpStatusLine != null)
                {
                    httpStatusLine.Method = replacementText;
                }
            }
            break;

            case MessageValue.HttpRequestUri:
            {
                HttpRequestStatusLine httpStatusLine = (eventInfo.Message as HttpMessage)?.StatusLine as HttpRequestStatusLine;
                if (httpStatusLine != null)
                {
                    httpStatusLine.Uri = replacementText;
                }
            }
            break;

            case MessageValue.HttpStatusMessage:
            {
                HttpResponseStatusLine httpStatusLine = (eventInfo.Message as HttpMessage)?.StatusLine as HttpResponseStatusLine;
                if (httpStatusLine != null)
                {
                    httpStatusLine.StatusMessage = replacementText;
                }
            }
            break;

            case MessageValue.HttpVersion:
            {
                HttpStatusLine httpStatusLine = (eventInfo.Message as HttpMessage)?.StatusLine;
                if (httpStatusLine != null)
                {
                    httpStatusLine.Version = replacementText;
                }
            }
            break;

            case MessageValue.HttpStatusCode:
            {
                HttpResponseStatusLine httpStatusLine = (eventInfo.Message as HttpMessage)?.StatusLine as HttpResponseStatusLine;
                if (httpStatusLine != null)
                {
                    int replacementVal;
                    if (int.TryParse(replacementText, out replacementVal))
                    {
                        httpStatusLine.StatusCode = replacementVal;
                    }
                }
            }
            break;

            case MessageValue.Message:
            {
                if (eventInfo.Message is HttpMessage)
                {
                    HttpMessage newMessage = _httpMessageParser.Parse(replacementText, true).Item2;
                    if (newMessage != null)
                    {
                        eventInfo.Message = newMessage;
                    }
                }
                else if (eventInfo.Message != null)
                {
                    eventInfo.Message.ResetCheckedEntity(HttpMessage.EntityFlag);
                    if (valueType != MessageValueType.Text && identifier != null)
                    {
                        switch (valueType)
                        {
                        case MessageValueType.Json:
                            replacementText = eventInfo.Message.ToString().SetJsonValue(identifier.GetText(eventInfo.Variables), replacementText);
                            break;

                        case MessageValueType.Xml:
                            replacementText = eventInfo.Message.ToString().SetXmlValue(identifier.GetText(eventInfo.Variables), replacementText);
                            break;
                        }
                    }
                    eventInfo.Message.RawText = replacementText + eventInfo.Message.Delimiter;
                }
            }
            break;
            }
        }