コード例 #1
0
        public static void Mock()
        {
            BHttpContext.SetCurrent();

            MHttpRequest          request  = new MHttpRequest();
            MHttpResponse         response = new MHttpResponse();
            MHttpCookieCollection cookies  = new MHttpCookieCollection();

            response.CookiesGet            = () => cookies;
            response.OnCookieAddHttpCookie = (cookie) => Nothing();
            response.RequestGet            = () => request;
            cookies.GetString = (key) => null;

            Dictionary <object, object> items    = new Dictionary <object, object>();
            Dictionary <string, string> reqItems = new Dictionary <string, string>();

            request.ItemGetString = (key) => reqItems.ContainsKey(key) ? reqItems[key] : null;


            new MHttpContext(BHttpContext.Current)
            {
                ItemsGet    = () => items,
                RequestGet  = () => request,
                ResponseGet = () => response
            };


            MHttpUtility.ParseQueryStringString = ParseQueryString;
            MHttpUtility.UrlEncodeString        = (url) => Uri.EscapeDataString(url).Replace("%20", "+");
        }
コード例 #2
0
        public void LogMessageShouldContainContextualInformation()
        {
            // arrange
            var context = BHttpContext.SetCurrent();

            MSPContext.CurrentGet = () => null;

            context.Timestamp = new DateTime(2000, 1, 1);
            var user     = context.SetUser();
            var identity = user.SetIdentity();

            identity.Name = "TestUser";
            var request = context.SetRequest();

            request.Url             = new Uri("http://localhost/mypage.aspx");
            request.UserHostAddress = "1.1.1.1.1";
            request.UserAgent       = "MyAgent";
            var       eventLogger = SharePointServiceLocator.GetCurrent().GetInstance <IEventLogLogger>() as MockEventAndTraceLogger;
            Exception ex          = new Exception("message");

            // act
            var target = new SharePointLogger();

            target.LogToOperations(ex);

            var message = eventLogger.Messages[0].Message;

            Assert.IsTrue(message.Contains(@"Request URL: 'http://localhost/mypage.aspx'"));
            Assert.IsTrue(message.Contains("Request TimeStamp: '" + context.Timestamp.ToString("o", CultureInfo.CurrentCulture) + "'"));
            Assert.IsTrue(message.Contains("UserName: '******'"));
            Assert.IsTrue(message.Contains("Originating IP address: '1.1.1.1.1'"));
            Assert.IsTrue(message.Contains("User Agent: 'MyAgent'"));
        }