Ejemplo n.º 1
0
        // All unit tests run under DNX451
#if NET40 || DNX451
        // Seam used for unit testing. During unit testing, gets an xml element created by the test.
        // During production get the jsnlog element from web.config.
        //
        // >>>>>>
        // Note that calling this method with a given xe is a way to cache that xe's config
        // for the next call to GetJsnlogConfiguration().
        internal static JsnlogConfiguration GetJsnlogConfiguration(Func <XmlElement> lxe)
        {
            if (_jsnlogConfiguration == null)
            {
                XmlElement xe = lxe();
                if (xe != null)
                {
                    _jsnlogConfiguration = XmlHelpers.DeserialiseXml <JsnlogConfiguration>(xe);
                }
            }

            return(GetJsnlogConfigurationWithoutWebConfig());
        }
Ejemplo n.º 2
0
        private void RunTest(string configXml, string json, string requestId, string userAgent, string userHostAddress,
                             DateTime serverSideTimeUtc, string url, IEnumerable <LogData> expected)
        {
            XmlElement xe = CommonTestHelpers.ConfigToXe(configXml);

            // Act

            var jsnlogConfiguration    = XmlHelpers.DeserialiseXml <JsnlogConfiguration>(xe);
            List <FinalLogData> actual =
                LoggerProcessor.ProcessLogRequestExec(
                    json,
                    new LogRequestBase(userAgent, userHostAddress, requestId, url, null, null, null),
                    serverSideTimeUtc, jsnlogConfiguration);

            TestLogDatasEqual(expected, actual, serverSideTimeUtc);
        }
Ejemplo n.º 3
0
        public void InternalError()
        {
            string configXml = @"
                <jsnlog></jsnlog>
";

            XmlElement xe = CommonTestHelpers.ConfigToXe(configXml);

            // Act

            var jsnlogConfiguration    = XmlHelpers.DeserialiseXml <JsnlogConfiguration>(xe);
            List <FinalLogData> actual =
                LoggerProcessor.ProcessLogRequestExec(
                    _json4,
                    new LogRequestBase("my browser", "12.345.98.7", "", "http://mydomain.com/main", null, null, null),
                    _dtServerUtc, jsnlogConfiguration);

            // Assert

            Assert.Equal(1, actual.Count);
            Assert.Equal(Constants.JSNLogInternalErrorLoggerName, actual.ElementAt(0).FinalLogger);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Ensures that the xml will be serialised by JSNLog to the code in csharp.
        /// Also writes HTML to d:\temp\demos.html with premade html for example tabs.
        ///
        /// In that HTML, you may want to apply strikethrough or the "new code" style.
        /// To do that, use these meta tags:
        ///
        /// <![CDATA[
        /// (S}...striked through text ...{S)"/>
        /// (N}...striked through text ...{N)"/>
        /// ]]>
        /// </summary>
        /// <param name="configXml"></param>
        /// <param name="csharp"></param>
        public void TestDemo(string configXml, string csharp, string demoId)
        {
            // Testing to ensure xml and code are the same

            XmlElement xe = CommonTestHelpers.ConfigToXe(CodeWithoutMeta(configXml));
            var        jsnlogConfigurationFromXml = XmlHelpers.DeserialiseXml <JsnlogConfiguration>(xe);

            JsnlogConfiguration jsnlogConfigurationFromCode = (JsnlogConfiguration)UnitTestHelpers.Eval(CodeWithoutMeta(csharp));

            UnitTestHelpers.EnsureEqualJsnlogConfiguration(jsnlogConfigurationFromXml, jsnlogConfigurationFromCode);

            // Write partial

            var sb = new StringBuilder();

            sb.AppendLine(string.Format("@* GENERATED CODE - by class DemoTests in JSNLog.Tests project. Demo {0}. *@", demoId));

            sb.AppendLine(@"<div class=""commontabs""><div data-tab=""Web.config"">");
            sb.AppendLine(@"");
            sb.AppendLine(string.Format(@"<pre>{0}</pre>", CodeToHtml(configXml)));
            sb.AppendLine(@"");
            sb.AppendLine(@"</div><div data-tab=""JsnlogConfiguration"">");
            sb.AppendLine(@"");
            sb.AppendLine(string.Format(@"<pre>JavascriptLogging.{0}({1});</pre>",
                                        LinkedText("SetJsnlogConfiguration", _setJsnlogConfigurationUrl),
                                        CodeToHtml(csharp)));
            sb.AppendLine(@"");
            sb.AppendLine(@"</div></div>");

            string path    = Path.Combine(TestConstants._demosDirectory, string.Format("_{0}.cshtml", demoId));
            string content = sb.ToString();

            bool fileExists = File.Exists(path);

            Assert.False(fileExists, string.Format("{0} already exists", path));

            System.IO.File.WriteAllText(path, content);
        }
Ejemplo n.º 5
0
        public void EnsureDefaultDateFormatAllowsJsonDeserialization()
        {
            // Arrange

            string configXml = @"
                <jsnlog serverSideMessageFormat=""{ 
                    'utcDate': '%utcDate', 'utcDateServer': '%utcDateServer', 'date': '%date', 'dateServer': '%dateServer' 
                    }""></jsnlog>";

            XmlElement xe = CommonTestHelpers.ConfigToXe(configXml);

            // Act

            var jsnlogConfiguration    = XmlHelpers.DeserialiseXml <JsnlogConfiguration>(xe);
            List <FinalLogData> actual =
                LoggerProcessor.ProcessLogRequestExec(
                    _json1,
                    new LogRequestBase("my browser", "12.345.98.7", "http://mydomain.com/main", "", null, null, null),
                    _dtServerUtc, jsnlogConfiguration);

            string messageToBeLogged = actual.FirstOrDefault().FinalMessage;

            var datesBag1 = LogMessageHelpers.DeserializeJson <DatesBag>(messageToBeLogged);

            TestDatesEqual(datesBag1.utcDate, _dtFirstLogUtc);
            TestDatesEqual(datesBag1.utcDateServer, _dtServerUtc);
            TestDatesEqual(datesBag1.date, _dtFirstLog);
            TestDatesEqual(datesBag1.dateServer, _dtServer);

            var datesBag2 = Newtonsoft.Json.JsonConvert.DeserializeObject <DatesBag>(messageToBeLogged);

            TestDatesEqual(datesBag2.utcDate, _dtFirstLogUtc);
            TestDatesEqual(datesBag2.utcDateServer, _dtServerUtc);
            TestDatesEqual(datesBag2.date, _dtFirstLog);
            TestDatesEqual(datesBag2.dateServer, _dtServer);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Ensures that the xml will be serialised by JSNLog to the code in csharp.
        /// Also writes HTML to d:\temp\demos.html with premade html for example tabs.
        ///
        /// In that HTML, you may want to apply strikethrough or the "new code" style.
        /// To do that, use these meta tags:
        ///
        /// <![CDATA[
        /// (S}...striked through text ...{S)"/>
        /// (N}...striked through text ...{N)"/>
        /// ]]>
        /// </summary>
        /// <param name="configXml"></param>
        /// <param name="csharp"></param>
        public void TestDemo(string configXml, string csharp, string demoId)
        {
            // Testing to ensure xml and code are the same

            XmlElement xe = CommonTestHelpers.ConfigToXe(CodeWithoutMeta(configXml));
            var        jsnlogConfigurationFromXml = XmlHelpers.DeserialiseXml <JsnlogConfiguration>(xe);

            JsnlogConfiguration jsnlogConfigurationFromCode = (JsnlogConfiguration)UnitTestHelpers.Eval(CodeWithoutMeta(csharp));

            UnitTestHelpers.EnsureEqualJsnlogConfiguration(jsnlogConfigurationFromXml, jsnlogConfigurationFromCode);

            // Write partial

            var sb = new StringBuilder();

            sb.AppendLine(string.Format("@* GENERATED CODE - by class DemoTests in JSNLog.Tests project. Demo {0}. *@", demoId));
            sb.AppendLine("@using ViewExtensions");
            sb.AppendLine(@"<div class=""commontabs""><div data-tab=""Web.config"">");
            sb.AppendLine(@"");
            sb.AppendLine(string.Format(@"<pre>{0}</pre>", CodeToHtml(configXml)));
            sb.AppendLine(@"");
            sb.AppendLine(@"</div><div data-tab=""JsnlogConfiguration"">");
            sb.AppendLine(@"");

            string html = CodeToHtml(csharp, 2);

            sb.AppendLine(@"
@{ 
	bool isNetCore;
	bool isNetFramework;
	PageVersions.GetPageVersion(out isNetCore, out isNetFramework);
}

");

            sb.AppendLine(@"
	@if (isNetFramework)
	{
        <pre class='net-framework-only'>JavascriptLogging." + LinkedText("SetJsnlogConfiguration", _setJsnlogConfigurationUrl) + "(" + html + @");</pre>
    }
");

            sb.AppendLine(@"
	@if (isNetCore)
	{
        <pre class='net-core-only'>// Use in " + LinkedText("Configure method in Startup class", _setJsnlogCoreConfigurationUrl) + @"
        var jsnlogConfiguration = " + html + @";</pre>
    }
");

            sb.AppendLine(@"");
            sb.AppendLine(@"</div></div>");

            string path    = Path.Combine(TestConstants._demosDirectory, string.Format("_{0}.cshtml", demoId));
            string content = sb.ToString();

            bool fileExists = File.Exists(path);

            Assert.False(fileExists, string.Format("{0} already exists", path));

            System.IO.File.WriteAllText(path, content);
        }