Ejemplo n.º 1
0
        public void CheckIfRestRequest_Cookie_WhenAjaxCSRFTokenIsValid_ThenGetRequestShouldBeAjax()
        {
            var doc = new XmlDocument();
            doc.LoadXml("<r><template id='a' mode='Server' /></r>");

            var validToken = "valid-token";

            var httpContextInfo = new HttpContextInfo();
            httpContextInfo.QueryString.Add("xtags-xajax", "xtags-xajax");
            httpContextInfo.QueryString.Add("xtags-http-method", "GET");
            httpContextInfo.QueryString.Add("xtags-id", "a");
            httpContextInfo.QueryString.Add("xtags-token", validToken);

            httpContextInfo.Cookies.Add(new HttpCookie("a"));
            httpContextInfo.Cookies["a"].Value = validToken;

            var result =
                new xContext(httpContextInfo)
                    .Do(new LoadLibrary(doc))
                    .Do(new CreateTag("template"))
                    .DoFirst(x => x != null, new CheckIfRestRequest(onGet: (context, isAjax) =>
                    {
                        Assert.IsTrue(isAjax);
                        Assert.AreEqual(context.xTag.Id, "a");
                    }, useCsrfCookies: true), new RenderHtml());

            var responseText = result.ResponseText.ToString();

            Assert.IsTrue(responseText.StartsWith("(function(){"));
            Assert.IsTrue(responseText.Contains("'a'"));
            Assert.IsTrue(responseText.Contains("'" + httpContextInfo.PageUri() + "'"));
            Assert.IsTrue(responseText.EndsWith("})();"));
            Assert.AreEqual(result.ContentType, "text/plain");
        }
Ejemplo n.º 2
0
        public void CheckIfRestRequest_Session_WhenAjaxHasCallback_ThenJsonpResponseRendered()
        {
            var doc = new XmlDocument();
            doc.LoadXml("<r><template id='a' mode='Server' /></r>");

            var validToken = "valid-token";

            var httpContextInfo = new HttpContextInfo();
            httpContextInfo.QueryString.Add("xtags-xajax", "xtags-xajax");
            httpContextInfo.QueryString.Add("xtags-http-method", "GET");
            httpContextInfo.QueryString.Add("xtags-id", "a");
            httpContextInfo.QueryString.Add("xtags-token", validToken);
            httpContextInfo.QueryString.Add("callback", "callbackMethod");

            httpContextInfo.Session("a", validToken);

            var result =
                new xContext(httpContextInfo)
                    .Do(new LoadLibrary(doc))
                    .Do(new CreateTag("template"))
                    .DoFirst(x => x != null, new CheckIfRestRequest(onGet: (context, isAjax) =>
                    {
                        Assert.IsTrue(isAjax);
                        Assert.AreEqual(context.xTag.Id, "a");
                    }), new RenderHtml())
                    .Do(new RenderJsonpIfRequested());

            var responseText = result.ResponseText.ToString();

            Assert.IsTrue(responseText.StartsWith("callbackMethod(\"(function(){"));
            Assert.IsTrue(responseText.Contains("'a'"));
            Assert.IsTrue(responseText.Contains("'" + httpContextInfo.PageUri() + "'"));
            Assert.IsTrue(responseText.EndsWith("})();\");"));
            Assert.AreEqual(result.ContentType, "text/javascript");
        }