public void GetLibaryPage_ReturnsLibraryPage()
 {
     var b = CreateBloomServer();
     var transaction = new PretendRequestInfo(ServerBase.ServerUrlWithBloomPrefixEndingInSlash + "library/library.htm");
     b.MakeReply(transaction);
     Assert.IsTrue(transaction.ReplyContents.Contains("library.css"));
 }
        public void CorrectedLocalPath_HandlesNetworkDriveCorrectly()
        {
            var path = "//myserver/myfolder/myfile.htm";
            var url = path.ToLocalhost();
            var request = new PretendRequestInfo(url);
            var result = ServerBase.CorrectedLocalPath(request);
            Assert.That(result, Is.EqualTo(ServerBase.BloomUrlPrefix + path));

            // This tests the condition leading to BL-2932.
            request = new PretendRequestInfo(url, forPrinting: true);
            result = ServerBase.CorrectedLocalPath(request);
            Assert.That(result, Is.EqualTo(ServerBase.BloomUrlPrefix + "OriginalImages/" + path));
        }
        public void CanGetPdf()
        {
            // Setup
            using (var server = CreateImageServer())
            using (var file = TempFile.WithExtension(".pdf"))
            {
                var transaction = new PretendRequestInfo(ServerBase.ServerUrlWithBloomPrefixEndingInSlash + file.Path);

                // Execute
                server.MakeReply(transaction);

                // Verify
                Assert.IsTrue(transaction.ReplyImagePath.Contains(".pdf"));
            }
        }
        public void RetrieveFileWithSpecialCharacters()
        {
            const string fileContents = @"\&<'@?>/" + "\r\n\"";
            using (var asciiFile = MakeTempFile(Encoding.ASCII.GetBytes(fileContents)))
            {
                using (var utf8File = MakeTempFile(Encoding.UTF8.GetBytes(fileContents)))
                {
                    var request = new PretendRequestInfo(ServerBase.ServerUrlWithBloomPrefixEndingInSlash);

                    request.WriteCompleteOutput(File.ReadAllText(asciiFile.Path));
                    var asciiString = request.ReplyContents;

                    Assert.AreEqual(asciiString.Length, 11);
                    Assert.AreEqual(asciiString[0], '\\');
                    Assert.AreEqual(asciiString[1], '&');
                    Assert.AreEqual(asciiString[2], '<');
                    Assert.AreEqual(asciiString[3], '\'');
                    Assert.AreEqual(asciiString[4], '@');
                    Assert.AreEqual(asciiString[5], '?');
                    Assert.AreEqual(asciiString[6], '>');
                    Assert.AreEqual(asciiString[7], '/');
                    Assert.AreEqual(asciiString[8], '\r');
                    Assert.AreEqual(asciiString[9], '\n');
                    Assert.AreEqual(asciiString[10], '"');

                    request.WriteCompleteOutput(File.ReadAllText(utf8File.Path));
                    var utf8String = request.ReplyContents;
                    Assert.AreEqual(utf8String.Length, 11);
                    Assert.AreEqual(utf8String[0], '\\');
                    Assert.AreEqual(utf8String[1], '&');
                    Assert.AreEqual(utf8String[2], '<');
                    Assert.AreEqual(utf8String[3], '\'');
                    Assert.AreEqual(utf8String[4], '@');
                    Assert.AreEqual(utf8String[5], '?');
                    Assert.AreEqual(utf8String[6], '>');
                    Assert.AreEqual(utf8String[7], '/');
                    Assert.AreEqual(utf8String[8], '\r');
                    Assert.AreEqual(utf8String[9], '\n');
                    Assert.AreEqual(utf8String[10], '"');
                }
            }
        }
        public void CorrectedLocalPath_HandlesLocalDriveCorrectly()
        {
            var path = "C:/Users/myname/Documents/Bloom/My Collection/My Book/My Book.htm";
            var url = path.ToLocalhost();
            var request = new PretendRequestInfo(url);
            var result = ServerBase.CorrectedLocalPath(request);
            Assert.That(result, Is.EqualTo(ServerBase.BloomUrlPrefix + path));

            request = new PretendRequestInfo(url, forPrinting: true);
            result = ServerBase.CorrectedLocalPath(request);
            Assert.That(result, Is.EqualTo(ServerBase.BloomUrlPrefix + "OriginalImages/" + path));

            path = "/home/myname/Bloom/My Collection/My Book/My Book.htm";
            url = path.ToLocalhost();
            request = new PretendRequestInfo(url);
            result = ServerBase.CorrectedLocalPath(request);
            Assert.That(result, Is.EqualTo(ServerBase.BloomUrlPrefix + path));

            request = new PretendRequestInfo(url, forPrinting: true);
            result = ServerBase.CorrectedLocalPath(request);
            Assert.That(result, Is.EqualTo(ServerBase.BloomUrlPrefix + "OriginalImages/" + path));
        }
        private PretendRequestInfo CreateServerMakeSimPageMakeReply(HtmlDom dom, bool simulateCallingFromJavascript = false)
        {
            PretendRequestInfo transaction;
            using (var server = CreateImageServer())
            {
                using (var fakeTempFile = EnhancedImageServer.MakeSimulatedPageFileInBookFolder(dom, simulateCallingFromJavascript))
                {
                    var url = fakeTempFile.Key;
                    transaction = new PretendRequestInfo(url, forPrinting: false, forSrcAttr: simulateCallingFromJavascript);

                    // Execute
                    server.MakeReply(transaction);
                }
            }
            return transaction;
        }
 public void GetVernacularBookList_ThereAre2_Returns2ListItems()
 {
     var b = CreateBloomServer();
     var transaction = new PretendRequestInfo(ServerBase.ServerUrlWithBloomPrefixEndingInSlash + "libraryContents");
     AddBook("1","one");
     AddBook("2", "two");
     b.MakeReply(transaction);
     AssertThatXmlIn.String(transaction.ReplyContentsAsXml).HasSpecifiedNumberOfMatchesForXpath("//li", 2);
 }
 public void GetVernacularBookList_ThereAreNone_ReturnsNoListItems()
 {
     var b = CreateBloomServer();
     var transaction = new PretendRequestInfo(ServerBase.ServerUrlWithBloomPrefixEndingInSlash + "libraryContents");
     _bookInfoList.Clear();
     b.MakeReply(transaction);
     AssertThatXmlIn.String(transaction.ReplyContentsAsXml).HasNoMatchForXpath("//li");
 }
        public void CanRetrieveContentOfFakeTempFile_ButOnlyUntilDisposed()
        {
            using (var server = CreateImageServer())
            {
                var html = @"<html ><head></head><body>here it is</body></html>";
                var dom = new HtmlDom(html);
                dom.BaseForRelativePaths =_folder.Path.ToLocalhost();
                string url;
                using (var fakeTempFile = EnhancedImageServer.MakeSimulatedPageFileInBookFolder(dom))
                {
                    url = fakeTempFile.Key;
                    var transaction = new PretendRequestInfo(url);

                    // Execute
                    server.MakeReply(transaction);

                    // Verify
                    // Whitespace inserted by CreateHtml5StringFromXml seems to vary across versions and platforms.
                    // I would rather verify the actual output, but don't want this test to be fragile, and the
                    // main point is that we get a file with the DOM content.
                    Assert.That(transaction.ReplyContents,
                        Is.EqualTo(TempFileUtils.CreateHtml5StringFromXml(dom.RawDom)));
                }
                var transactionFail = new PretendRequestInfo(url);

                // Execute
                server.MakeReply(transactionFail);

                // Verify
                Assert.That(transactionFail.StatusCode, Is.EqualTo(404));
            }
        }
 public void GetLocalPathWithoutQuery_HandlesNetworkDriveCorrectly()
 {
     var path = @"//someserver/somefolder/somebook.htm";
     var url = path.ToLocalhost() + "?thumbnail=1";
     var request = new PretendRequestInfo(url);
     var result = ServerBase.GetLocalPathWithoutQuery(request);
     Assert.That(result, Is.EqualTo(path));
 }
        public void GetCorrect_MiscStylesCss()
        {
            using (var server = CreateImageServer())
            {
                SetupCssTests();
                var cssFile = Path.Combine(_folder.Path, "TestCollection", "TestBook", "miscStyles.css");

                var url = cssFile.ToLocalhost();
                var transaction = new PretendRequestInfo(url);

                server.MakeReply(transaction);

                Assert.That(transaction.ReplyContents, Is.EqualTo(".miscStylesCssTest{}"));
            }
        }
        public void GetCorrect_SettingsCollectionStylesCss_WhenMakingPdf()
        {
            using (var server = CreateImageServer())
            {
                SetupCssTests();
                // Let's do it the way BookStorage.EnsureHasLinksToStylesheets() does it
                var filePath = ".." + Path.DirectorySeparatorChar + "settingsCollectionStyles.css";
                var cssFile = Path.Combine(_folder.Path, "TestCollection", "TestBook", filePath);

                var url = cssFile.ToLocalhost();
                var transaction = new PretendRequestInfo(url, forPrinting: true);

                server.MakeReply(transaction);

                Assert.That(transaction.ReplyContents, Is.EqualTo(".settingsCollectionStylesCssTest{}"));
            }
        }
        public void GetCorrect_XmatterStylesCss()
        {
            using (var server = CreateImageServer())
            {
                SetupCssTests();
                var cssFile = Path.Combine(_folder.Path, "TestCollection", "TestBook", "Factory-XMatter.css");

                var url = cssFile.ToLocalhost();
                var transaction = new PretendRequestInfo(url);

                server.MakeReply(transaction);

                Assert.AreNotEqual(transaction.ReplyContents, ".factoryXmatterCssTest{}");
            }
        }
        public void SupportsHandlerInjection()
        {
            // Setup
            using (var server = CreateImageServer())
            {
                var transaction = new PretendRequestInfo(ServerBase.ServerUrlWithBloomPrefixEndingInSlash + "api/thisWontWorkWithoutInjectionButWillWithIt");
                server.CurrentCollectionSettings = new CollectionSettings();
                EndpointHandler testFunc = (request) =>
                    {
                        Assert.That(request.LocalPath(), Is.StringContaining("thisWontWorkWithoutInjection"));
                        Assert.That(request.CurrentCollectionSettings, Is.EqualTo(server.CurrentCollectionSettings));
                        request.ReplyWithText("Did It!");
                    };
                server.RegisterEndpointHandler("thisWontWorkWithoutInjection", testFunc);

                // Execute
                server.MakeReply(transaction);

                // Verify
                Assert.That(transaction.ReplyContents, Is.EqualTo("Did It!"));
            }
        }
        public void RequestXMatter_OnlyExistsInBookAndDistFiles_ReturnsTheOneInDistFiles()
        {
            using (var server = CreateImageServer())
            {
                SetupCssTests();
                var cssFile = Path.Combine(_folder.Path, "TestCollection", "TestBook", "ForUnitTest-XMatter.css");

                var url = cssFile.ToLocalhost();
                var transaction = new PretendRequestInfo(url);

                server.MakeReply(transaction);

                Assert.AreEqual(transaction.ReplyContents.Trim(), "This is the one in DistFiles");
            }
        }
        public void ReportsMissingFile()
        {
            // Setup
            using (var server = CreateImageServer())
            {
                var transaction = new PretendRequestInfo(ServerBase.ServerUrlWithBloomPrefixEndingInSlash + "/non-existing-file.pdf");

                // Execute
                server.MakeReply(transaction);

                // Verify
                Assert.That(transaction.StatusCode, Is.EqualTo(404));
                Assert.That(Logger.LogText, Contains.Substring("**EnhancedImageServer: File Missing: /non-existing-file.pdf"));
            }
        }
        public void HandleDoubleEncodedUrls()
        {
            // https://silbloom.myjetbrains.com/youtrack/issue/BL-3835 describes a problem that can occur when
            // Url encoded filenames are stored for the coverImage data.  One of the uploaded books
            // in the library has coverImage data stored as
            // <div data-book="coverImage" lang="*">
            //     The%20Moon%20and%20The%20Cap_Cover.png
            // </div>
            // and the image file was not being found by the server because a second level of encoding was
            // applied before requesting the file.  So this test arbitrarily applies a double level of Url
            // encoding (the third time) to ensure that the server can handle it.
            using (var server = CreateImageServer())
            {
                Directory.CreateDirectory(_collectionPath);
                var txtFile = Path.Combine(_collectionPath, "File With Spaces.txt");
                const string testData = @"This is a test!\r\n";
                File.WriteAllText(txtFile, testData);

                // no Url encoding of spaces fed to server
                var url = txtFile.ToLocalhost();
                var transaction = new PretendRequestInfo(url);
                server.MakeReply(transaction);
                Assert.That(transaction.ReplyContents, Is.EqualTo(testData));

                // single level of Url encoding fed to server
                var encUrl = txtFile.ToLocalhost().Replace(" ", "%20");		// ToLocalHost() does partial encoding, but not for spaces.
                var encTransaction = new PretendRequestInfo(encUrl);
                Assert.That(encTransaction.RawUrl.Contains("%20"), Is.True);
                server.MakeReply(encTransaction);
                Assert.That(encTransaction.ReplyContents, Is.EqualTo(testData));

                // double level of Url encoding fed to server
                var enc2TxtFile = txtFile.Replace(" ", "%20");		// encodes spaces
                var enc2Url = enc2TxtFile.ToLocalhost();			// double encodes spaces
                var enc2Transaction = new PretendRequestInfo(enc2Url);
                Assert.That(enc2Transaction.RawUrl.Contains("%2520"), Is.True);
                server.MakeReply(enc2Transaction);
                Assert.That(enc2Transaction.ReplyContents, Is.EqualTo(testData));
            }
        }
        public void GetLocalPathWithoutQuery_HandlesLocalDriveCorrectly()
        {
            var path = @"C:/Users/myname/Documents/Bloom/Collection/Some Book/Some Book.htm";
            var url = path.ToLocalhost() + "?thumbnail=1";
            var request = new PretendRequestInfo(url);
            var result = ServerBase.GetLocalPathWithoutQuery(request);
            Assert.That(result, Is.EqualTo(path));

            path = @"/home/myname/Bloom/Collection/Some Book/Some Book.htm";
            url = path.ToLocalhost() + "?thumbnail=1";
            request = new PretendRequestInfo(url);
            result = ServerBase.GetLocalPathWithoutQuery(request);
            Assert.That(result, Is.EqualTo(path));
        }
 private Dictionary<string, string> QueryServerForJson(string query)
 {
     using (var server = CreateImageServer())
     {
         var transaction = new PretendRequestInfo(ServerBase.ServerUrlWithBloomPrefixEndingInSlash + query);
         server.MakeReply(transaction);
         Debug.WriteLine(transaction.ReplyContents);
         var jss = new System.Web.Script.Serialization.JavaScriptSerializer();
         return jss.Deserialize<Dictionary<string, string>>(transaction.ReplyContents);
     }
 }