Example #1
0
        internal static void StartOwinDocTest(LogDocController.TestContext testContext, Action testsFunc)
        {
            var stu = new StartupDoc(testContext);

            using (WebApp.Start(getWsAddress(testContext.PortOffset), stu.Configuration))
            {
                testsFunc();

                testContext.CountdownEvent.Wait(webserviceCheckTimeoutMs);
                Thread.Sleep(1000);
            }
        }
Example #2
0
        public void WebserviceTest_restapi_json()
        {
            var configuration = XmlLoggingConfiguration.CreateFromXmlString($@"
                <nlog throwExceptions='true'>
                    <targets>
                        <target type='WebService'
                                name='ws'
                                url='{getWsAddress(1)}{"api/logdoc/json"}'
                                protocol='JsonPost'
                                encoding='UTF-8'
                               >
                            <UserAgent>SecretAgent</UserAgent>
                            <header name='Authorization' layout='OpenBackDoor' />
                            <parameter name='param1' ParameterType='System.String' layout='${{message}}'/> 
                            <parameter name='param2' ParameterType='System.String' layout='${{level}}'/>
                            <parameter name='param3' ParameterType='System.Boolean' layout='True'/>
                            <parameter name='param4' ParameterType='System.DateTime' layout='${{date:universalTime=true:format=o}}'/>
                        </target>
                    </targets>
                    <rules>
                      <logger name='*' writeTo='ws'>
                       
                      </logger>
                    </rules>
                </nlog>");


            LogManager.Configuration = configuration;
            var logger = LogManager.GetCurrentClassLogger();

            var txt     = "message 1 with a JSON POST<hello><again\\>\"\b"; // Lets tease the JSON serializer and see it can handle valid and invalid xml chars
            var count   = 101;
            var context = new LogDocController.TestContext(1, count, false, new Dictionary <string, string>()
            {
                { "Authorization", "OpenBackDoor" }, { "User-Agent", "SecretAgent" }
            }, txt, "info", true, DateTime.UtcNow);

            StartOwinDocTest(context, () =>
            {
                for (int i = 0; i < count; i++)
                {
                    logger.Info(txt);
                }
            });

            Assert.Equal <int>(0, context.CountdownEvent.CurrentCount);
        }
        internal static void StartOwinDocTest(string url, LogDocController.TestContext testContext, Action testsFunc)
        {
            using (WebApp.Start(url, (appBuilder) =>
            {
                // Configure Web API for self-host.
                HttpConfiguration config = new HttpConfiguration();

                config.DependencyResolver = new ControllerResolver <LogDocController>(() => new LogDocController()
                {
                    Context = testContext
                });

                config.Routes.MapHttpRoute(
                    name: "ApiWithAction",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                    );
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                    );

                if (testContext.XmlInsteadOfJson)
                {
                    // Default Xml Formatter uses DataContractSerializer, changing it to XmlSerializer
                    config.Formatters.XmlFormatter.UseXmlSerializer = true;
                }
                else
                {
                    // Use ISO 8601 / RFC 3339 Date-Format (2012-07-27T18:51:45.53403Z), instead of Microsoft JSON date format ("\/Date(ticks)\/")
                    config.Formatters.JsonFormatter.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
                    config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;  // JSON.NET serializer instead of the ancient DataContractJsonSerializer
                }

                appBuilder.UseWebApi(config);
            }))
            {
                testsFunc();

                if (testContext.CountdownEvent != null)
                {
                    testContext.CountdownEvent.Wait(webserviceCheckTimeoutMs);
                    Thread.Sleep(1000);
                }
            }
        }
        public void WebserviceTest_restapi_json()
        {
            string wsAddress  = getNewWsAddress();
            var    logFactory = new LogFactory().Setup()
                                .SetupExtensions(ext => ext.RegisterAssembly(typeof(WebServiceTarget).Assembly))
                                .LoadConfigurationFromXml($@"
                <nlog throwExceptions='true'>
                    <targets>
                        <target type='WebService'
                                name='ws'
                                url='{wsAddress}{"api/logdoc/json"}'
                                protocol='JsonPost'
                                encoding='UTF-8'
                               >
                            <header name='Authorization' layout='OpenBackDoor' />
                            <parameter name='param1' ParameterType='System.String' layout='${{message}}'/> 
                            <parameter name='param2' ParameterType='System.String' layout='${{level}}'/>
                            <parameter name='param3' ParameterType='System.Boolean' layout='True'/>
                            <parameter name='param4' ParameterType='System.DateTime' layout='${{date:universalTime=true:format=o}}'/>
                        </target>
                    </targets>
                    <rules>
                      <logger name='*' writeTo='ws' />
                    </rules>
                </nlog>").LogFactory;

            var logger = logFactory.GetCurrentClassLogger();

            var txt     = "message 1 with a JSON POST<hello><again\\>\"\b"; // Lets tease the JSON serializer and see it can handle valid and invalid xml chars
            var count   = 101;
            var context = new LogDocController.TestContext(count, false, new Dictionary <string, string>()
            {
                { "Authorization", "OpenBackDoor" }
            }, txt, "info", true, DateTime.UtcNow);

            StartOwinDocTest(wsAddress, context, () =>
            {
                for (int i = 0; i < count; i++)
                {
                    logger.Info(txt);
                }
            });

            Assert.Equal <int>(0, context.CountdownEvent.CurrentCount);
        }
        public void WebserviceTest_soap11_custom_soapaction()
        {
            string wsAddress  = getNewWsAddress();
            var    logFactory = new LogFactory().Setup()
                                .SetupExtensions(ext => ext.RegisterAssembly(typeof(WebServiceTarget).Assembly))
                                .LoadConfigurationFromXml($@"
                <nlog throwExceptions='true'>
                    <targets>
                        <target type='WebService'
                                name='ws'
                                url='{wsAddress}{"api/logdoc/soap11"}'
                                protocol='Soap11'
                                namespace='http://tempuri.org/'
                                methodName ='Ping'
                                preAuthenticate='false'
                                encoding ='UTF-8'
                               >
                            <header name='SOAPAction' layout='http://tempuri.org/custom-namespace/Ping'/>
                            <parameter name='param1' ParameterType='System.String' layout='${{message}}'/> 
                            <parameter name='param2' ParameterType='System.String' layout='${{level}}'/>
                        </target>
                    </targets>
                    <rules>
                      <logger name='*' writeTo='ws' />
                    </rules>
                </nlog>").LogFactory;

            var logger = logFactory.GetCurrentClassLogger();

            var txt             = "test.message"; // Lets tease the Xml-Serializer, and see it can handle xml-tags
            var count           = 1;
            var expectedHeaders = new Dictionary <string, string>
            {
                { "SOAPAction", "http://tempuri.org/custom-namespace/Ping" }
            };
            var context = new LogDocController.TestContext(count, true, expectedHeaders, null, null, true, DateTime.UtcNow);

            StartOwinDocTest(wsAddress, context, () =>
            {
                logger.Info(txt);
            });

            Assert.Equal <int>(0, context.CountdownEvent.CurrentCount);
        }
Example #6
0
        public void WebserviceTest_restapi_xml()
        {
            var configuration = CreateConfigurationFromString(string.Format(@"
                <nlog throwExceptions='true'>
                    <targets>
                        <target type='WebService'
                                name='ws'
                                url='{0}{1}'
                                protocol='XmlPost'
                                XmlRoot='ComplexType'
                                encoding='UTF-8'
                               >
                            <parameter name='param1' ParameterType='System.String' layout='${{message}}'/> 
                            <parameter name='param2' ParameterType='System.String' layout='${{level}}'/>
                            <parameter name='param3' ParameterType='System.Boolean' layout='True'/>
                            <parameter name='param4' ParameterType='System.DateTime' layout='${{date:universalTime=true:format=o}}'/>
                        </target>
                    </targets>
                    <rules>
                      <logger name='*' writeTo='ws'>
                       
                      </logger>
                    </rules>
                </nlog>", getWsAddress(1), "api/logdoc/xml"));


            LogManager.Configuration = configuration;
            var logger = LogManager.GetCurrentClassLogger();

            var txt     = "message 1 with a XML POST<hello><again\\>\""; // Lets tease the Xml-Serializer, and see it can handle xml-tags
            var count   = 101;
            var context = new LogDocController.TestContext(1, count, true, txt, "info", true, DateTime.UtcNow);

            StartOwinDocTest(context, () =>
            {
                for (int i = 0; i < count; i++)
                {
                    logger.Info(txt + "\b");    // Lets tease the Xml-Serializer, and see it can remove invalid chars
                }
            });

            Assert.Equal <int>(0, context.CountdownEvent.CurrentCount);
        }
Example #7
0
        public void WebserviceTest_soap11_default_soapaction()
        {
            var configuration = XmlLoggingConfiguration.CreateFromXmlString($@"
                <nlog throwExceptions='true'>
                    <targets>
                        <target type='WebService'
                                name='ws'
                                url='{getWsAddress(1)}{"api/logdoc/soap11"}'
                                protocol='Soap11'
                                namespace='http://tempuri.org/'
                                methodName ='Ping'
                                preAuthenticate='false'
                                encoding ='UTF-8'
                               >
                            <parameter name='param1' ParameterType='System.String' layout='${{message}}'/> 
                            <parameter name='param2' ParameterType='System.String' layout='${{level}}'/>
                        </target>
                    </targets>
                    <rules>
                      <logger name='*' writeTo='ws' />
                    </rules>
                </nlog>");


            LogManager.Configuration = configuration;
            var logger = LogManager.GetCurrentClassLogger();

            var txt             = "test.message"; // Lets tease the Xml-Serializer, and see it can handle xml-tags
            var count           = 1;
            var expectedHeaders = new Dictionary <string, string>
            {
                { "SOAPAction", "http://tempuri.org/Ping" }
            };
            var context = new LogDocController.TestContext(1, count, true, expectedHeaders, null, null, true, DateTime.UtcNow);

            StartOwinDocTest(context, () =>
            {
                logger.Info(txt);
            });

            Assert.Equal <int>(0, context.CountdownEvent.CurrentCount);
        }
        public void WebserviceTest_restapi_xml()
        {
            var configuration = CreateConfigurationFromString(string.Format(@"
                <nlog throwExceptions='true'>
                    <targets>
                        <target type='WebService'
                                name='ws'
                                url='{0}{1}'
                                protocol='XmlPost'
                                XmlRoot='ComplexType'
                                encoding='UTF-8'
                               >
                            <parameter name='param1' type='System.String' layout='${{message}}'/> 
                            <parameter name='param2' type='System.String' layout='${{level}}'/>
     
                        </target>
                    </targets>
                    <rules>
                      <logger name='*' writeTo='ws'>
                       
                      </logger>
                    </rules>
                </nlog>", getWsAddress(1), "api/logdoc/xml"));


            LogManager.Configuration = configuration;
            var logger = LogManager.GetCurrentClassLogger();

            var txt     = "message 1 with a XML POST";
            var count   = 101;
            var context = new LogDocController.TestContext(1, count, true, txt, "info");

            StartOwinDocTest(context, () =>
            {
                for (int i = 0; i < count; i++)
                {
                    logger.Info(txt);
                }
            });

            Assert.Equal <int>(0, context.CountdownEvent.CurrentCount);
        }
        public void WebserviceTest_restapi_xml()
        {
            string wsAddress  = getNewWsAddress();
            var    logFactory = new LogFactory().Setup()
                                .SetupExtensions(ext => ext.RegisterAssembly(typeof(WebServiceTarget).Assembly))
                                .LoadConfigurationFromXml($@"
                <nlog throwExceptions='true'>
                    <targets>
                        <target type='WebService'
                                name='ws'
                                url='{wsAddress}{"api/logdoc/xml"}'
                                protocol='XmlPost'
                                XmlRoot='ComplexType'
                                encoding='UTF-8'
                               >
                            <parameter name='param1' ParameterType='System.String' layout='${{message}}'/> 
                            <parameter name='param2' ParameterType='System.String' layout='${{level}}'/>
                            <parameter name='param3' ParameterType='System.Boolean' layout='True'/>
                            <parameter name='param4' ParameterType='System.DateTime' layout='${{date:universalTime=true:format=o}}'/>
                        </target>
                    </targets>
                    <rules>
                      <logger name='*' writeTo='ws' />
                    </rules>
                </nlog>").LogFactory;

            var logger = logFactory.GetCurrentClassLogger();

            var txt     = "message 1 with a XML POST<hello><again\\>\""; // Lets tease the Xml-Serializer, and see it can handle xml-tags
            var count   = 101;
            var context = new LogDocController.TestContext(count, true, null, txt, "info", true, DateTime.UtcNow);

            StartOwinDocTest(wsAddress, context, () =>
            {
                for (int i = 0; i < count; i++)
                {
                    logger.Info(txt);
                }
            });

            Assert.Equal <int>(0, context.CountdownEvent.CurrentCount);
        }
Example #10
0
 public ControllerResolver(LogDocController.TestContext testContext)
 {
     _testContext = testContext;
 }
Example #11
0
 public StartupDoc(LogDocController.TestContext testContext)
 {
     _testContext = testContext;
 }
        public void WebserviceTest_restapi_group_json()
        {
            var configuration = CreateConfigurationFromString(string.Format(@"
                <nlog throwExceptions='true'>
                    <targets>
                        <target type='BufferingWrapper' bufferSize='6' name='ws'>
                            <target type='WebService'
                                    name='ws_wrapped'
                                    url='{0}{1}'
                                    protocol='JsonPost'
                                    encoding='UTF-8'
                                   >
                                <header name='Authorization' layout='OpenBackDoor' />
                                <parameter name='param1' enableGroupLayout='true' groupHeaderLayout='{{ ' groupItemSeparatorLayout=' , ' groupFooterLayout=' }}'>
                                    <layout type='JsonLayout'>
                                        <attribute name='level' layout='${{level}}'/>
                                        <attribute name='message' layout='${{message}}'/>
                                    </layout>
                                </parameter>
                                <parameter name='param2' ParameterType='System.String' layout='${{level}}'/>
                                <parameter name='param3' ParameterType='System.Boolean' layout='True'/>
                                <parameter name='param4' ParameterType='System.DateTime' layout='${{date:format=yyyy-MM-dd HH\:mm}}'/>
                            </target>
                        </target>
                    </targets>
                    <rules>
                      <logger name='*' writeTo='ws'>
                      </logger>
                    </rules>
                </nlog>", getWsAddress(1), "api/logdoc/json"));

            LogManager.Configuration = configuration;
            var logger = LogManager.GetCurrentClassLogger();

            var txt      = "message 1 with a JSON POST<hello><again\\>\"\b"; // Lets tease the JSON serializer and see it can handle valid and invalid xml chars
            var expected = new System.Text.StringBuilder();

            for (int i = 0; i < 3; ++i)
            {
                if (expected.Length == 0)
                {
                    expected.Append("{ ");
                }
                else
                {
                    expected.Append(" , ");
                }
                expected.Append("{ \"level\": \"Info\", \"message\": \"message 1 with a JSON POST<hello><again\\\\>\\\"\\b\" }");
            }
            expected.Append(" }");
            var count   = 2;
            var context = new LogDocController.TestContext(1, count, false, new Dictionary <string, string>()
            {
                { "Authorization", "OpenBackDoor" }
            }, expected.ToString(), "info", true, DateTime.UtcNow.Date);

            StartOwinDocTest(context, () =>
            {
                var defaultTimeSource = Time.TimeSource.Current;
                try
                {
                    var timeSource = new TimeSourceTests.ShiftedTimeSource(DateTimeKind.Local);
                    if (timeSource.Time.Minute == 59)
                    {
                        timeSource.AddToLocalTime(TimeSpan.FromMinutes(1));
                        timeSource.AddToSystemTime(TimeSpan.FromMinutes(1));
                    }
                    Time.TimeSource.Current = timeSource;

                    for (int i = 0; i < 3; i++)
                    {
                        logger.Info(txt);
                    }

                    timeSource.AddToLocalTime(TimeSpan.FromMinutes(2));

                    for (int i = 0; i < 3; i++)
                    {
                        logger.Info(txt);
                    }
                }
                finally
                {
                    Time.TimeSource.Current = defaultTimeSource; // restore default time source
                }
            });

            Assert.Equal <int>(0, context.CountdownEvent.CurrentCount);
        }
Example #13
0
 public ControllerResolver(LogDocController.TestContext testContext)
 {
     _testContext = testContext;
 }
Example #14
0
 public StartupDoc(LogDocController.TestContext testContext)
 {
     _testContext = testContext;
 }
Example #15
0
        public void WebserviceTest_restapi_xml()
        {


            var configuration = CreateConfigurationFromString(string.Format(@"
                <nlog throwExceptions='true'>
                    <targets>
                        <target type='WebService'
                                name='ws'
                                url='{0}{1}'
                                protocol='XmlPost'
                                XmlRoot='ComplexType'
                                encoding='UTF-8'
                               >
                            <parameter name='param1' type='System.String' layout='${{message}}'/> 
                            <parameter name='param2' type='System.String' layout='${{level}}'/>
     
                        </target>
                    </targets>
                    <rules>
                      <logger name='*' writeTo='ws'>
                       
                      </logger>
                    </rules>
                </nlog>", getWsAddress(1), "api/logdoc/xml"));


            LogManager.Configuration = configuration;
            var logger = LogManager.GetCurrentClassLogger();

            var txt = "message 1 with a XML POST";
            var count = 101;
            var context = new LogDocController.TestContext(1, count, true, txt, "info");

            StartOwinDocTest(context, () =>
            {
                for (int i = 0; i < count; i++)
                    logger.Info(txt);
            });

            Assert.Equal<int>(0, context.CountdownEvent.CurrentCount);
        }