private void RunTestHttp( string httpMethod, string origin, string configXml, string json, string requestId, string userAgent, string userHostAddress, DateTime serverSideTimeUtc, string url, int expectedResponseCode, Dictionary <string, string> expectedResponseHeaders, List <LogEntry> expectedLogEntries) { // Arrange LogResponse response = new LogResponse(); TestLogger logger = new TestLogger(); CommonTestHelpers.SetConfigCache(configXml, logger); // Act LoggerProcessor.ProcessLogRequest( json, new LogRequestBase(userAgent, userHostAddress, requestId, url, null, null, null), serverSideTimeUtc, httpMethod, origin, response); // Assert Assert.Equal(expectedResponseCode, response.StatusCode); TestLogEntries(expectedLogEntries, logger.LogEntries); TestResponseHeaders(expectedResponseHeaders, response.Headers); }
private void RunTest(string configXml) { var sb = new StringBuilder(); CommonTestHelpers.SetConfigCache(configXml); var configProcessor = new ConfigProcessor(); configProcessor.ProcessRootExec(sb, s => s, "23.89.450.1", "req", true); }
public void IsLoggingUrl_NoUrl() { string configXml = @" <jsnlog></jsnlog> "; CommonTestHelpers.SetConfigCache(configXml, null); Exception ex = Assert.Throws <ArgumentNullException>(() => LoggingUrlHelpers.IsLoggingUrl(null)); }
private string ToJavaScript(string configXml) { var sb = new StringBuilder(); CommonTestHelpers.SetConfigCache(configXml); var configProcessor = new ConfigProcessor(); configProcessor.ProcessRootExec(sb, s => s, "23.89.450.1", "req", false); return(sb.ToString()); }
public void IsLoggingUrl_NoUrlsConfigured() { string configXml = @" <jsnlog></jsnlog> "; CommonTestHelpers.SetConfigCache(configXml, null); Assert.True(LoggingUrlHelpers.IsLoggingUrl("/jsnlog.logger")); Assert.True(LoggingUrlHelpers.IsLoggingUrl("http://abc.com/jsnlog.logger")); Assert.False(LoggingUrlHelpers.IsLoggingUrl("http://abc.com/jsnlog.css")); }
public void IsLoggingUrl_DefaultConfigured_NoAppenderUrlsConfigured() { string configXml = @" <jsnlog defaultAjaxUrl=""/jsnlogger""></jsnlog> "; CommonTestHelpers.SetConfigCache(configXml, null); Assert.True(LoggingUrlHelpers.IsLoggingUrl("/jsnlogger")); Assert.True(LoggingUrlHelpers.IsLoggingUrl("/abc/def/jsnlogger")); Assert.False(LoggingUrlHelpers.IsLoggingUrl("/jsnlog.logger")); Assert.False(LoggingUrlHelpers.IsLoggingUrl("http://abc.com/jsnlog.logger")); Assert.False(LoggingUrlHelpers.IsLoggingUrl("http://abc.com/jsnlog.css")); }
public void IsLoggingUrl_NoDefaultConfigured_AppenderUrlsConfiguredWithTilde() { string configXml = @" <jsnlog> <ajaxAppender url=""~/jsn2logger"" /> </jsnlog> "; CommonTestHelpers.SetConfigCache(configXml, null); Assert.True(LoggingUrlHelpers.IsLoggingUrl("/jsn2logger")); Assert.True(LoggingUrlHelpers.IsLoggingUrl("//abc.com/jsn2logger?a=b;c=d")); // url of the default appender should also be regarded as a logging url Assert.True(LoggingUrlHelpers.IsLoggingUrl("/jsnlog.logger")); Assert.True(LoggingUrlHelpers.IsLoggingUrl("http://abc.com/jsnlog.logger")); Assert.False(LoggingUrlHelpers.IsLoggingUrl("http://abc.com/jsnlog.css")); }
public void IsLoggingUrl_NoDefaultConfigured_AppenderUrlsConfigured() { string configXml = @" <jsnlog> <ajaxAppender name=""a1"" url=""/jsn2logger"" /> </jsnlog> "; CommonTestHelpers.SetConfigCache(configXml, null); Assert.True(LoggingUrlHelpers.IsLoggingUrl("/jsn2logger")); Assert.True(LoggingUrlHelpers.IsLoggingUrl("//abc.com/jsn2logger?a=b;c=d")); // Should also the url of the default appender Assert.True(LoggingUrlHelpers.IsLoggingUrl("/jsnlog.logger")); Assert.True(LoggingUrlHelpers.IsLoggingUrl("http://abc.com/jsnlog.logger")); Assert.False(LoggingUrlHelpers.IsLoggingUrl("http://abc.com/jsnlog.css")); }
public void IsLoggingUrl_DefaultConfigured_MultipleAppenderUrlsConfigured() { string configXml = @" <jsnlog defaultAjaxUrl=""/jsnlogger""> <ajaxAppender name=""a1"" url=""/jsn2logger"" /> <ajaxAppender name=""a2"" url =""/jsn3logger"" /> </jsnlog> "; CommonTestHelpers.SetConfigCache(configXml, null); Assert.True(LoggingUrlHelpers.IsLoggingUrl("/jsn2logger")); Assert.True(LoggingUrlHelpers.IsLoggingUrl("//abc.com/jsn2logger?a=b;c=d")); Assert.True(LoggingUrlHelpers.IsLoggingUrl("/jsn3logger")); Assert.True(LoggingUrlHelpers.IsLoggingUrl("//abc.com/jsn3logger?a=b;c=d")); Assert.True(LoggingUrlHelpers.IsLoggingUrl("/jsnlogger")); Assert.True(LoggingUrlHelpers.IsLoggingUrl("/abc/def/jsnlogger")); Assert.False(LoggingUrlHelpers.IsLoggingUrl("/jsnlog.logger")); Assert.False(LoggingUrlHelpers.IsLoggingUrl("http://abc.com/jsnlog.logger")); Assert.False(LoggingUrlHelpers.IsLoggingUrl("http://abc.com/jsnlog.css")); }
/// <summary> /// Returns all javascript to set up a test. /// The generated javascript is within an immediately executing function, so it sits in its own namespace. /// /// </summary> /// <param name="configXml"> /// String with xml with the JSNLog root element, as would be used in a web.config file. /// </param> /// <param name="userIp"> /// Simulated IP address of the client. /// </param> /// <param name="requestId"> /// Simulated request id. /// </param> /// <returns></returns> public static string SetupTest(string userIp, string requestId, string configXml, IEnumerable <T> tests) { var sb = new StringBuilder(); // Set config cache in JavascriptLogging to contents of xe CommonTestHelpers.SetConfigCache(configXml); var configProcessor = new ConfigProcessor(); configProcessor.ProcessRootExec(sb, s => s, userIp, requestId, false); sb.AppendLine(@"<script type=""text/javascript"">"); sb.AppendLine("(function () {"); sb.AppendLine("JL.setOptions({ 'defaultBeforeSend': TestUtils.beforeSend });"); int seq = 0; foreach (T t in tests) { if (t.Level > -1) { // Level given, so generate call to logger. string msg = t.LogObject ?? @"""" + Msg(seq, t.Level, t.Logger) + @""""; string logCallJs = string.Format(@"JL(""{0}"").log({1}, {2});", t.Logger, t.Level, msg); string storeTimestampJs = StoreTimestampJs(seq); sb.AppendLine(logCallJs + " " + storeTimestampJs); } if (t.CheckNbr > -1) { // CheckNbr given, so do a check // Create JSON object with all expected log entries string expected = Expected(t.CheckNbr, tests); // Generate check js string checkJs = string.Format("TestUtils.Check({0}, {1}, {2});", t.CheckAppender, t.CheckNbr, expected); sb.AppendLine(""); sb.AppendLine(checkJs); sb.AppendLine("// ----------------------"); sb.AppendLine(""); } if (!string.IsNullOrEmpty(t.Header)) { sb.AppendLine(string.Format("$('body').append('<h3>{0}</h3>');", t.Header)); } seq++; } // Remove the "running" heading. If the tests somehow crash, we won't get here and the running header will remain, // showing something is wrong. sb.AppendLine("$('#running').remove();"); sb.AppendLine("})();"); sb.AppendLine("</script>"); string js = sb.ToString(); return(js); }
/// <summary> /// Returns all javascript to set up a test. /// The generated javascript is within an immediately executing function, so it sits in its own namespace. /// /// </summary> /// <param name="configXml"> /// String with xml with the JSNLog root element, as would be used in a web.config file. /// </param> /// <param name="userIp"> /// Simulated IP address of the client. /// </param> /// <param name="requestId"> /// Simulated request id. /// </param> /// <returns></returns> public static string SetupTest(string userIp, string requestId, string configXml, IEnumerable <T> tests) { var sb = new StringBuilder(); // Set config cache in JavascriptLogging to contents of xe // This essentially injects the config XML into JSNLog (the same way as when reading from web.config). CommonTestHelpers.SetConfigCache(configXml); // Generate configuration JavaScript var jsnlogJavaScriptConfig = JSNLog.JavascriptLogging.Configure(); sb.AppendLine(jsnlogJavaScriptConfig); AddSetXMLHttpRequest(sb); AddLoadJsnlogJs(sb); sb.AppendLine(@"<script type=""text/javascript"">"); sb.AppendLine("function start() {"); sb.AppendLine(string.Format( "JL.setOptions({{ 'clientIP': '{0}', 'requestId': '{1}' }});", userIp, requestId)); int seq = 0; foreach (T t in tests) { if (t.Level > -1) { // Level given, so generate call to logger. string msg = t.LogObject ?? @"""" + Msg(seq, t.CheckExpected, t.Level, t.Logger) + @""""; string logCallJs = string.Format(@"JL(""{0}"").log({1}, {2});", t.Logger, t.Level, msg); string storeTimestampJs = StoreTimestampJs(seq); string consoleLog = $"console.log('log Logger: {t.Logger}, Level: {t.Level}, msg: ' + {msg});"; sb.AppendLine(logCallJs + " " + storeTimestampJs + " " + consoleLog); } if (t.CheckNbr > -1) { // CheckNbr given, so do a check // Create JSON object with all expected log entries string expected = Expected(t.CheckNbr, tests); // Generate check js string checkJs = string.Format("JLTestUtils.Check('{0}', {1}, {2});", t.CheckAppender, t.CheckNbr, expected); sb.AppendLine(""); sb.AppendLine(checkJs); sb.AppendLine("// ----------------------"); sb.AppendLine(""); } if (!string.IsNullOrEmpty(t.Header)) { sb.AppendLine(string.Format("$('body').append('<h3>{0}</h3>');", t.Header)); } if (t.DelayMs > 0) { // Delay execution of the steps by // creating a new function (below the current one), creating a setTimeout to run // that function after the delay. Then continue adding steps to that new function. string nextFunctionName = $"continue{seq}"; sb.AppendLine($"setTimeout({nextFunctionName}, {t.DelayMs});"); // End curent function sb.AppendLine("}"); // Start new function sb.AppendLine($"function {nextFunctionName}() {{"); } seq++; } // The testdone object signals to the C# that it can stop waiting for the test to finish sb.AppendLine(@"$('body').append('<div id=""testdone""></div>');"); sb.AppendLine("}"); sb.AppendLine("start();"); // Remove the "running" heading. If the tests somehow crash, we won't get here and the running header will remain, // showing something is wrong. sb.AppendLine("$('#running').remove();"); sb.AppendLine("</script>"); string js = sb.ToString(); return(js); }