Example #1
0
        public async void TestGetMediaWithPdfExtension()
        {
            const string ItemPath  = "/sitecore/media library/Images/Files/pdf example";
            const string MediaPath = "~/media/Images/Files/pdf example.pdf";
            const string Db        = "master";

            var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(MediaPath)
                          .Database(Db)
                          .Build();

            using (var response = await this.session.DownloadMediaResourceAsync(request))
                using (var ms = new MemoryStream())
                {
                    await response.CopyToAsync(ms);

                    var expectedItem = await this.GetItemByPath(ItemPath, Db);

                    Assert.AreEqual(expectedItem["size"].RawValue, ms.Length.ToString(CultureInfo.InvariantCulture));
                }
        }
Example #2
0
        public async void TestGetItemWithTildaInPath()
        {
            var options = new MediaOptionsBuilder().Set
                          .DisplayAsThumbnail(false)
                          .AllowStrech(true)
                          .Height(150)
                          .Build();

            var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("~/media/Images/green_mineraly1")
                          .DownloadOptions(options)
                          .Build();

            using (var response = await this.session.DownloadMediaResourceAsync(request))
                using (var ms = new MemoryStream())
                {
                    await response.CopyToAsync(ms);

                    Assert.AreEqual(16284, ms.Length);
                }
        }
        public async void TestGetMediaFromDifferentDb()
        {
            const string Path = SitecoreMouseIconPath;
            var          requestFromMasterDb = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(Path)
                                               .Database("master")
                                               .Build();

            using (var responseFromMasterDb = await this.session.DownloadMediaResourceAsync(requestFromMasterDb))
                using (var ms = new MemoryStream())
                {
                    await responseFromMasterDb.CopyToAsync(ms);

                    // @adk : changed since different size has been received
                    // * Mac OS
                    // * IOS Simulator
                    //Assert.IsTrue(141750 == ms.Length);
                }

            var requestFromWebDb = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(Path)
                                   .Database("web")
                                   .Build();

            TestDelegate testCode = async() =>
            {
                var   task = this.session.DownloadMediaResourceAsync(requestFromWebDb);
                await task;
            };

            Exception exception = Assert.Throws <LoadDataFromNetworkException>(testCode);

            Assert.IsTrue(exception.Message.Contains("Unable to download data from the internet"));
            Assert.AreEqual("System.Net.Http.HttpRequestException", exception.InnerException.GetType().ToString());

            // Windows : "Response status code does not indicate success: 404 (Not Found)"
            // iOS     : "404 (Not Found)"
            Assert.IsTrue(exception.InnerException.Message.Contains("Not Found"));

            //@adk : fails because CMS 6.6u6, 7.1u3 returns HTTP 500 instead of HTTP 404
            //      500 Internal Server Error
            //Assert.IsTrue(exception.InnerException.Message.Contains("Internal Server Error"));
        }
Example #4
0
        public void TestCancelGetMedia()
        {
            var    request     = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("/sitecore/media library/Images/test image").Build();
            var    cancelToken = CreateCancelTokenWithDelay(5);
            Stream response    = null;

            TestDelegate testCode = async() =>
            {
                var task = session.DownloadMediaResourceAsync(request, cancelToken);
                response = await task;
            };
            var exception = Assert.Catch <OperationCanceledException>(testCode);

            Assert.IsNull(response);
            //      Desktop (Windows) : "A task was canceled."
            //      iOS               : "The Task was canceled"
            Assert.IsTrue(exception.Message.ToLowerInvariant().Contains("task was canceled"));

            // @adk : CancellationToken class comparison or scheduling works differently on iOS
            // Assert.AreEqual(cancelToken, exception.CancellationToken);
        }
        private static async void __Main_Media(string[] args)
        {
            string instanceUrl = "http://mobiledev1ua1.dk.sitecore.net:7220";

            using
            (
                var session =
                    SitecoreWebApiSessionBuilder.AnonymousSessionWithHost(instanceUrl)
                    .Site("/sitecore/shell")
                    .DefaultDatabase("web")
                    .DefaultLanguage("en")
                    .MediaLibraryRoot("/sitecore/media library")
                    .MediaPrefix("~/media/")
                    .DefaultMediaResourceExtension("ashx")
                    .BuildReadonlySession()
            )
            {
                string mediaPath = "/sitecore/media library/Images/butterfly2_large";
                var    options   = new MediaOptionsBuilder().Set
                                   .MaxWidth(1920)
                                   .MaxHeight(1080)
                                   .Width(1024)
                                   .Height(768)
                                   .BackgroundColor("red")
                                   .DisableMediaCache(false)
                                   .AllowStrech(false)
                                   .Scale(1.0f)
                                   .DisplayAsThumbnail(false)
                                   .Build();

                var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(mediaPath)
                              .Database("master")
                              .Language("fr")
                              .Version(1)
                              .DownloadOptions(options)
                              .Build();

                Stream response = await session.DownloadMediaResourceAsync(request);
            }
        }
        public void TestGetMediaWithNotExistentPathReturnsError()
        {
            var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("/sitecore/media library/images/not existent")
                          .Build();

            TestDelegate testCode = async() =>
            {
                var   task = this.session.DownloadMediaResourceAsync(request);
                await task;
            };
            Exception exception = Assert.Throws <LoadDataFromNetworkException>(testCode);

            Assert.IsTrue(exception.Message.Contains("Unable to download data from the internet"));
            Assert.AreEqual("System.Net.Http.HttpRequestException", exception.InnerException.GetType().ToString());

            // Windows : "Response status code does not indicate success: 404 (Not Found)"
            // iOS     : "404 (Not Found)"
            Assert.IsTrue(exception.InnerException.Message.Contains("Not Found"));

            //@adk : fails because CMS 7.1u3 returns HTTP 500 instead of HTTP 404
            //      500 Internal Server Error
        }
        public async void TestGetMediaWithHeightAndWidthAndAllowSrtech()
        {
            const int Height = 200;
            const int Width  = 300;

            var options = new MediaOptionsBuilder().Set
                          .Height(Height)
                          .Width(Width)
                          .AllowStrech(true)
                          .Build();

            var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("/sitecore/media library/Images/kirkorov")
                          .DownloadOptions(options)
                          .Build();

            using (var response = await this.session.DownloadMediaResourceAsync(request))
                using (var ms = new MemoryStream())
                {
                    await response.CopyToAsync(ms);

                    Assert.IsTrue(14300 > ms.Length);
                }
        }
Example #8
0
        public void TestGetMediaFromInvalidImageReturnsError()
        {
            var options = new MediaOptionsBuilder().Set
                          .Height(100)
                          .Build();

            var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("/sitecore/media library/Images/nexus")
                          .DownloadOptions(options)
                          .Build();

            TestDelegate testCode = async() =>
            {
                var   task = this.session.DownloadMediaResourceAsync(request);
                await task;
            };
            Exception exception = Assert.Throws <LoadDataFromNetworkException>(testCode);

            Assert.IsTrue(exception.Message.Contains("Unable to download data from the internet"));
            Assert.AreEqual("System.Net.Http.HttpRequestException", exception.InnerException.GetType().ToString());

            // Windows : "Response status code does not indicate success: 404 (Not Found)"
            // iOS     : "404 (Not Found)"
            Assert.IsTrue(exception.InnerException.Message.Contains("Not Found"));
        }
        public async Task <Byte[]> GetMediaByUrl(string mediaUrl)
        {
            try {
                mediaUrl = CleanUpMediaUrlByReplacingWeirdTildeSignWithCorrect(mediaUrl);

                using (ISitecoreWebApiSession session = await SitecoreSession)
                {
                    IMediaResourceDownloadRequest request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(mediaUrl)
                                                            .Language("en")
                                                            .Build();

                    byte[] data = null;

                    using (Stream response = await session.DownloadMediaResourceAsync(request))

                        using (MemoryStream responseInMemory = new MemoryStream())
                        {
                            await response.CopyToAsync(responseInMemory);

                            data = responseInMemory.ToArray();

                            return(data);
                        }
                }
            }
            catch (SitecoreMobileSdkException ex)
            {
                this._loggingService.Log("Error in GetMediaByUrl,  url {0} . Error: {1}", mediaUrl, ex.Message);
                throw ex;
            }
            catch (Exception ex)
            {
                this._loggingService.Log("Error in GetMediaByUrl,  url {0} . Error: {1}", mediaUrl, ex.Message);
                throw ex;
            }
        }
        [Test] //ALR: Argument exception should appear
        public void TestGetMediaWithNullVersionReturnsException()
        {
            Exception exception = Assert.Throws <ArgumentNullException>(() => ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("~/media/test").Version(null));

            Assert.IsTrue(exception.Message.Contains("DownloadMediaResourceRequestBuilder.Version"));
        }
        [Test] //ALR: Argument exception should appear
        public void TestGetMediaWithNegativeVersionReturnsException()
        {
            Exception exception = Assert.Throws <ArgumentException>(() => ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("~/media/test").Version(-1));

            Assert.AreEqual("DownloadMediaResourceRequestBuilder.Version : Positive number expected", exception.Message);
        }
        [Test] //ALR: Argument exception should appear
        public void TestGetMediaWithSpacesInLanguageReturnsException()
        {
            Exception exception = Assert.Throws <ArgumentException>(() => ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("~/media/test").Language("  "));

            Assert.AreEqual("DownloadMediaResourceRequestBuilder.Language : The input cannot be empty.", exception.Message);
        }