[Test] //ExSkip public void WebRequestTimeout() { // Create a new HtmlLoadOptions object and verify its timeout threshold for a web request. HtmlLoadOptions options = new HtmlLoadOptions(); // When loading an Html document with resources externally linked by a web address URL, // Aspose.Words will abort web requests that fail to fetch the resources within this time limit, in milliseconds. Assert.AreEqual(100000, options.WebRequestTimeout); // Set a WarningCallback that will record all warnings that occur during loading. ListDocumentWarnings warningCallback = new ListDocumentWarnings(); options.WarningCallback = warningCallback; // Load such a document and verify that a shape with image data has been created. // This linked image will require a web request to load, which will have to complete within our time limit. string html = $@" <html> <img src=""{AsposeLogoUrl}"" alt=""Aspose logo"" style=""width:400px;height:400px;""> </html> "; Document doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), options); Shape imageShape = (Shape)doc.GetChild(NodeType.Shape, 0, true); Assert.AreEqual(7498, imageShape.ImageData.ImageBytes.Length); Assert.AreEqual(0, warningCallback.Warnings().Count); // Set an unreasonable timeout limit and try load the document again. options.WebRequestTimeout = 0; doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), options); // A web request that fails to obtain an image within the time limit will still produce an image. // However, the image will be the red 'x' that commonly signifies missing images. imageShape = (Shape)doc.GetChild(NodeType.Shape, 0, true); Assert.AreEqual(924, imageShape.ImageData.ImageBytes.Length); // We can also configure a custom callback to pick up any warnings from timed out web requests. Assert.AreEqual(WarningSource.Html, warningCallback.Warnings()[0].Source); Assert.AreEqual(WarningType.DataLoss, warningCallback.Warnings()[0].WarningType); Assert.AreEqual($"Couldn't load a resource from \'{AsposeLogoUrl}\'.", warningCallback.Warnings()[0].Description); Assert.AreEqual(WarningSource.Html, warningCallback.Warnings()[1].Source); Assert.AreEqual(WarningType.DataLoss, warningCallback.Warnings()[1].WarningType); Assert.AreEqual("Image has been replaced with a placeholder.", warningCallback.Warnings()[1].Description); doc.Save(ArtifactsDir + "HtmlLoadOptions.WebRequestTimeout.docx"); }
[Test] //ExSkip public void WebRequestTimeout() { // Create a new HtmlLoadOptions object and verify its timeout threshold for a web request HtmlLoadOptions options = new HtmlLoadOptions(); // When loading an Html document with resources externally linked by a web address URL, // web requests that fetch these resources that fail to complete within this time limit will be aborted Assert.AreEqual(100000, options.WebRequestTimeout); // Set a WarningCallback that will record all warnings that occur during loading ListDocumentWarnings warningCallback = new ListDocumentWarnings(); options.WarningCallback = warningCallback; // Load such a document and verify that a shape with image data has been created, // provided the request to get that image took place within the timeout limit string html = $@" <html> <img src=""{AsposeLogoUrl}"" alt=""Aspose logo"" style=""width:400px;height:400px;""> </html> "; Document doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), options); Shape imageShape = (Shape)doc.GetChild(NodeType.Shape, 0, true); Assert.AreEqual(7498, imageShape.ImageData.ImageBytes.Length); Assert.AreEqual(0, warningCallback.Warnings().Count); // Set an unreasonable timeout limit and load the document again options.WebRequestTimeout = 0; doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), options); // If a request fails to complete within the timeout limit, a shape with image data will still be produced // However, the image will be the red 'x' that commonly signifies missing images imageShape = (Shape)doc.GetChild(NodeType.Shape, 0, true); Assert.AreEqual(924, imageShape.ImageData.ImageBytes.Length); // A timeout like this will also accumulate warnings that can be picked up by a WarningCallback implementation Assert.AreEqual(WarningSource.Html, warningCallback.Warnings()[0].Source); Assert.AreEqual(WarningType.DataLoss, warningCallback.Warnings()[0].WarningType); Assert.AreEqual($"The resource \'{AsposeLogoUrl}\' couldn't be loaded.", warningCallback.Warnings()[0].Description); Assert.AreEqual(WarningSource.Html, warningCallback.Warnings()[1].Source); Assert.AreEqual(WarningType.DataLoss, warningCallback.Warnings()[1].WarningType); Assert.AreEqual("Image has been replaced with a placeholder.", warningCallback.Warnings()[1].Description); doc.Save(ArtifactsDir + "HtmlLoadOptions.WebRequestTimeout.docx"); }