The phun api.
Наследование: IPhunApi
Пример #1
0
        public void TestPhunApiContentUrlReturnsContentDownloadPath()
        {
            // Arrange
            var connector = new ContentConnector();
            var fakeUtility = new Mock<ResourcePathUtility>();
            var fakeRepository = new Mock<IContentRepository>();
            var mockRequest = new Mock<HttpRequestBase>();
            var context = new Mock<HttpContextBase>();
            var fakeConfig = new Mock<ICmsConfiguration>();
            var expected = "/asdf/download/content/testz";

            fakeRepository.Setup(r => r.Exists(It.IsAny<ContentModel>())).Returns(true);
            mockRequest.Setup(rq => rq.Url).Returns(new Uri("http://localhost/testz"));
            mockRequest.Setup(rq => rq.Path).Returns("/testz");
            context.Setup(ctx => ctx.Request).Returns(mockRequest.Object);
            context.Setup(c => c.Cache).Returns(HttpRuntime.Cache);
            mockRequest.Setup(rq => rq.QueryString).Returns(new NameValueCollection());
            fakeConfig.Setup(cf => cf.ContentRouteNormalized).Returns("asdf");
            fakeUtility.Setup(u => u.Config).Returns(fakeConfig.Object);

            var api = new PhunApi(context.Object, connector);
            api.utility = fakeUtility.Object;

            // Act
            var result = api.contentUrl("/testz");

            // Assert
            Assert.AreEqual(expected, result);
        }
Пример #2
0
        public void TestPhunApiBundlesCallUtilityRenderBundles()
        {
            // Arrange
            var connector = new ContentConnector();
            var fakeUtility = new Mock<ResourcePathUtility>();
            var mockRequest = new Mock<HttpRequestBase>();
            var context = new Mock<HttpContextBase>();

            mockRequest.Setup(rq => rq.Url).Returns(new Uri("http://localhost/testz"));
            mockRequest.Setup(rq => rq.Path).Returns("/testz");
            context.Setup(ctx => ctx.Request).Returns(mockRequest.Object);
            fakeUtility.Setup(u => u.PhunCmsRenderBundles(true, true, true, true, true)).Verifiable();

            var api = new PhunApi(context.Object, connector);
            api.utility = fakeUtility.Object;

            // Act
            api.bundles();

            // Assert
            fakeUtility.VerifyAll();
        }
Пример #3
0
        /// <summary>
        /// Executes the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="connector">The connector.</param>
        /// <param name="httpContext">The HTTP context.</param>
        public void Render(Data.ContentModel model, IContentConnector connector, HttpContextBase httpContext)
        {
            using (var ctx = new JavascriptContext())
            {
                // caching vashjs
                if (vashjsString == null)
                {
                    var file = this.Utility.Config.GetResourceFile(this.Utility.GetResourcePath("/scripts/vash.js"));

                    // load vash.js
                    using (var stream = file.Open())
                    {
                        vashjsString = System.Text.Encoding.UTF8.GetString(stream.ReadAll());
                    }

                    vashjsString = vashjsString.Replace(
                        "d.tplcache[e]||(d.tplcache[e]=b.compile(a.readFileSync(e,\"utf8\")))",
                        "d.tplcache.get(e)||(d.tplcache.set(e,b.compile(a.readFileSync(e,\"utf8\"))))");
                }

                var context = new PhunApi(httpContext, connector);
                context.FileModel = model;

                // set application start
                // set api object
                // set require method
                ctx.SetParameter("__httpcontext__", context);
                ctx.Run(
            @"phun = { api: __httpcontext__ }; module = { exports: {}, require: phun.api.require };
            require = phun.api.require;
            console = {
            log: function() {
            // we need this for vash and everybody else
            try {
            for(var i = 0; i < arguments.length; i++) {
            phun.api.trace.log('' + arguments[i]);
            }
            } catch(e) {
            // do nothing
            }
            }
            };" +
            vashjsString +
            @"
            vash = module.exports;
            vash.config.cache = true;
            vash.helpers.tplcache = {
            get: function(key) {
            var result = phun.api.cache.get('templateCache$' + phun.api.FileModel.Path + '$' + key);
            return result ? eval(result) : null;
            },
            set: function(key, value) {
            var result = phun.api.cache.set('templateCache$' + phun.api.FileModel.Path + '$' + key, value + '');
            return value;
            }
            };

            var vashHtmlReportError = vash.helpers.constructor.reportError;
            vashHtmlExceptionMessage = '';
            vash.helpers.constructor.reportError = function(e, lineno, chr, orig, lb) {
            try {
            vashHtmlReportError(e, lineno, chr, orig, lb);
            }
            catch(ee) {
               vashHtmlExceptionMessage = e.stack;
            }
            };
            ");

                if (apiScripts == null)
                {
                    var sb = new StringBuilder();

                    // load api scripts
                    foreach (var script in Bootstrapper.Default.ApiScripts.Values)
                    {
                        sb.AppendLine(script);
                    }

                    // execute file
                    sb.AppendLine(
            @"try {
            vash.renderFile(
            phun.api.FileModel.Path,
            { model : {} },
            function(err, html) {
            if (typeof(html) == 'string') {
                phun.api.response.write(html.split(''));
            }
            else {

                phun.api.response.write(err);
                phun.api.response.write(html);
            }
            phun.api.response.flush();
            }
            );
            } catch(ee) {
            // provide better exception message
            throw new Error(ee + '\r\n' + vashHtmlExceptionMessage);
            }");
                    apiScripts = sb.ToString();
                }

                // finally execute entire apiScripts
                ctx.Run(apiScripts);
                httpContext.Response.End();
            }
        }
Пример #4
0
        public void TestPhunApiTenantHostReturnInternalProperty()
        {
            // Arrange
            var connector = new ContentConnector();
            var fakeUtility = new Mock<ResourcePathUtility>();
            var mockRequest = new Mock<HttpRequestBase>();
            var context = new Mock<HttpContextBase>();
            var expected = "testing";

            mockRequest.Setup(rq => rq.Url).Returns(new Uri("http://localhost/testz"));
            mockRequest.Setup(rq => rq.Path).Returns("/testz");
            context.Setup(ctx => ctx.Request).Returns(mockRequest.Object);
            fakeUtility.Setup(u => u.PhunPartialEditable(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Verifiable();

            var api = new PhunApi(context.Object, connector);
            api.utility = fakeUtility.Object;
            api.host = expected;

            // Act
            var result = api.tenantHost;

            // Assert
            Assert.AreEqual(expected, result);
        }
Пример #5
0
        public void TestPhunApiResourceUrlCallUtilityGetResourcePath()
        {
            // Arrange
            var connector = new ContentConnector();
            var fakeUtility = new Mock<ResourcePathUtility>();
            var fakeRepository = new Mock<IContentRepository>();
            var mockRequest = new Mock<HttpRequestBase>();
            var context = new Mock<HttpContextBase>();

            fakeRepository.Setup(r => r.Exists(It.IsAny<ContentModel>())).Returns(true);
            mockRequest.Setup(rq => rq.Url).Returns(new Uri("http://localhost/testz"));
            mockRequest.Setup(rq => rq.Path).Returns("/testz");
            context.Setup(ctx => ctx.Request).Returns(mockRequest.Object);
            context.Setup(c => c.Cache).Returns(HttpRuntime.Cache);
            mockRequest.Setup(rq => rq.QueryString).Returns(new NameValueCollection());
            fakeUtility.Setup(u => u.GetResourcePath("/testz")).Verifiable();

            var api = new PhunApi(context.Object, connector);
            api.utility = fakeUtility.Object;

            // Act
            api.resourceUrl("/testz");

            // Assert
            fakeUtility.VerifyAll();
        }
Пример #6
0
        public void TestPhunApiRequireReturnInteralProperties()
        {
            // Arrange
            var connector = new ContentConnector();
            var fakeUtility = new Mock<ResourcePathUtility>();
            var mockRequest = new Mock<HttpRequestBase>();
            var context = new Mock<HttpContextBase>();
            var expectedFileSystem = new Mock<IFileSystem>();
            var expectedPath = new Mock<IPath>();

            mockRequest.Setup(rq => rq.Url).Returns(new Uri("http://localhost/testz"));
            mockRequest.Setup(rq => rq.Path).Returns("/testz");
            context.Setup(ctx => ctx.Request).Returns(mockRequest.Object);
            fakeUtility.Setup(u => u.PhunPartialEditable(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Verifiable();

            var api = new PhunApi(context.Object, connector);
            api.utility = fakeUtility.Object;
            api.phunFileSystem = expectedFileSystem.Object;
            api.phunPath = expectedPath.Object;

            // Act
            // Assert
            Assert.AreEqual(expectedFileSystem.Object, api.require("fs"));
            Assert.AreEqual(expectedPath.Object, api.require("path"));
            Assert.AreEqual(null, api.require("unknown"));
        }
Пример #7
0
        public void TestPhunApiRequireReturnAnInstanceOfRegisteredApiObject()
        {
            // Arrange
            var connector = new ContentConnector();
            var fakeUtility = new Mock<ResourcePathUtility>();
            var mockRequest = new Mock<HttpRequestBase>();
            var context = new Mock<HttpContextBase>();

            mockRequest.Setup(rq => rq.Url).Returns(new Uri("http://localhost/testz"));
            mockRequest.Setup(rq => rq.Path).Returns("/testz");
            context.Setup(ctx => ctx.Request).Returns(mockRequest.Object);
            fakeUtility.Setup(u => u.PhunPartialEditable(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Verifiable();

            var api = new PhunApi(context.Object, connector);
            api.utility = fakeUtility.Object;
            Bootstrapper.Default.RegisterRequireJsModule<ContentConnector>("connector");

            // Act
            var result = api.require("connector");
            Bootstrapper.Default.ApiList.Clear();

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ContentConnector));
        }
Пример #8
0
        public void TestPhunApiPartialEditableCallUtilityPhunPartialEditable()
        {
            // Arrange
            var connector = new ContentConnector();
            var fakeUtility = new Mock<ResourcePathUtility>();
            var mockRequest = new Mock<HttpRequestBase>();
            var context = new Mock<HttpContextBase>();

            mockRequest.Setup(rq => rq.Url).Returns(new Uri("http://localhost/testz"));
            mockRequest.Setup(rq => rq.Path).Returns("/testz");
            context.Setup(ctx => ctx.Request).Returns(mockRequest.Object);
            fakeUtility.Setup(u => u.PhunPartialEditable(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Verifiable();

            var api = new PhunApi(context.Object, connector);
            api.utility = fakeUtility.Object;

            // Act
            api.partialEditable("test", "test", new { title = "test" });

            // Assert
            fakeUtility.VerifyAll();
        }