Ejemplo n.º 1
0
        public void CopyAllAttributesFromTheOriginalIFrameElementToTheAmpElement_Always()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateIFrame();

            htmlElement.Source        = "https://www.example.com";
            htmlElement.Id            = "iframeId";
            htmlElement.ClassName     = "someClassName";
            htmlElement.DisplayWidth  = 100;
            htmlElement.DisplayHeight = 200;
            ElementFactory.Document.Body.Append(htmlElement);

            var runContext = new RunContext(new RunConfiguration {
                RelativeUrlsHost = "http://test-domain.com"
            });

            var iframeSanitizer = new IFrameSanitizer();

            iframeSanitizer.Configure(runContext);

            // Act
            var actualResult = iframeSanitizer.Sanitize(ElementFactory.Document, htmlElement);

            // Assert
            Assert.AreEqual("https://www.example.com", actualResult.GetAttribute("src"));
            Assert.AreEqual("iframeId", actualResult.Id);
            Assert.AreEqual("someClassName", actualResult.ClassName);
            Assert.AreEqual(100, int.Parse(actualResult.GetAttribute("width")));
            Assert.AreEqual(200, int.Parse(actualResult.GetAttribute("height")));
        }
Ejemplo n.º 2
0
        public void CopyAllChildrenFromTheOriginalIFrameElementToTheAmpElement_Always()
        {
            // Arrange
            const int ExpectedResult = 2;
            var       htmlElement    = ElementFactory.CreateIFrame();
            var       firstChild     = ElementFactory.Create("input");
            var       secondChild    = ElementFactory.Create("p");

            htmlElement.Append(firstChild);
            htmlElement.Append(secondChild);
            ElementFactory.Document.Body.Append(htmlElement);

            var runContext = new RunContext(new RunConfiguration {
                RelativeUrlsHost = "http://test-domain.com"
            });

            var iframeSanitizer = new IFrameSanitizer();

            iframeSanitizer.Configure(runContext);

            // Act
            var actualResult = iframeSanitizer.Sanitize(ElementFactory.Document, htmlElement);

            // Assert
            Assert.AreEqual(ExpectedResult, actualResult.Children.Length);
        }
Ejemplo n.º 3
0
        public void ReturnAmpIFrameElementWithLayoutAttributeSetToResponsive_IfTheOriginalIFrameElementHasBothWidthAndHeightAttributes()
        {
            // Arrange
            const string ExpectedResult = "responsive";
            var          iframeElement  = ElementFactory.CreateIFrame();

            iframeElement.Source        = "http://www.mywebsite.com/example-resource";
            iframeElement.DisplayWidth  = 100;
            iframeElement.DisplayHeight = 100;
            ElementFactory.Document.Body.Append(iframeElement);

            var runContext = new RunContext(new RunConfiguration {
                RelativeUrlsHost = "http://test-domain.com"
            });

            var iframeSanitizer = new IFrameSanitizer();

            iframeSanitizer.Configure(runContext);

            // Act
            var actualResult = iframeSanitizer.Sanitize(ElementFactory.Document, iframeElement);

            // Assert
            Assert.AreEqual(ExpectedResult, actualResult.GetAttribute("layout"));
        }
        public void ReturnFalse_WhenIFrameHasNotSourceAttribute()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateIFrame();

            // Act
            var actualResult = new YouTubeVideoSanitizer().CanSanitize(htmlElement);

            // Assert
            Assert.IsFalse(actualResult);
        }
        private IHtmlInlineFrameElement CreateIFrame()
        {
            var iframe = ElementFactory.CreateIFrame();

            iframe.Source = string.Format("http://youtube.com/embed/{0}", VideoId);

            var parent = ElementFactory.CreateAnchor();

            parent.AppendChild(iframe);

            return(iframe);
        }
Ejemplo n.º 6
0
        public void SetLayoutAttributeToFill_WhenTheAmpElementHasNoWidthAndHeightAttributes()
        {
            // Arrange
            const string ExpectedResult = "fill";
            var          element        = ElementFactory.CreateIFrame();
            var          ampElement     = ElementFactory.Create("amp-iframe");

            // Act
            new MediaSanitizerAccessor().SetMediaElementLayout(element, ampElement);

            // Assert
            Assert.AreEqual(ExpectedResult, ampElement.GetAttribute("layout"));
        }
        public void ReturnTrue_WhenSourceAttributeIsYouTubeDomainAndStartsWithWWW()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateIFrame();

            htmlElement.Source = "http://www.youtube.com/embed/d8fr3AdK_tQ4";

            // Act
            var actualResult = new YouTubeVideoSanitizer().CanSanitize(htmlElement);

            // Assert
            Assert.IsTrue(actualResult);
        }
        public void ReturnFalse_WhenSourceAttributeIsNotYouTubeDomain()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateIFrame();

            htmlElement.Source = "mydomain.com";

            // Act
            var actualResult = new YouTubeVideoSanitizer().CanSanitize(htmlElement);

            // Assert
            Assert.IsFalse(actualResult);
        }
        public void ReturnFalse_WhenSourceAttributeIsNotEmbedUrlPath()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateIFrame();

            htmlElement.Source = "http://youtube.com/not-valid/url-path";

            // Act
            var actualResult = new YouTubeVideoSanitizer().CanSanitize(htmlElement);

            // Assert
            Assert.IsFalse(actualResult);
        }
Ejemplo n.º 10
0
        public void CallSetMediaElementLayoutMethod_Always()
        {
            // Arrange
            var element        = ElementFactory.CreateIFrame();
            var ampElement     = ElementFactory.Create("amp-iframe");
            var mediaSanitizer = new MediaSanitizerAccessor();

            // Act
            mediaSanitizer.SetElementLayout(element, ampElement);

            // Assert
            Assert.IsTrue(mediaSanitizer.SetMediaElementLayoutMethodIsCalled);
        }
        public void ReturnAmpElementWithTagEqualToAmpElementTagNameParameter_Always()
        {
            // Arrange
            const string ExpectedResult = "AMP-IFRAME";
            var          htmlElement    = ElementFactory.CreateIFrame();

            ElementFactory.Document.Body.Append(htmlElement);

            // Act
            var actualResult = new MediaSanitizerAccessor().SanitizeCore <IHtmlInlineFrameElement>(ElementFactory.Document, htmlElement, "amp-iframe");

            // Assert
            Assert.AreEqual(ExpectedResult, actualResult.TagName);
        }
        public void CallSetElementLayoutMethod_Always()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateIFrame();

            ElementFactory.Document.Body.Append(htmlElement);
            var mediaSanitizer = new MediaSanitizerAccessor();

            // Act
            var actualResult = mediaSanitizer.SanitizeCore <IHtmlInlineFrameElement>(ElementFactory.Document, htmlElement, "amp-iframe");

            // Assert
            Assert.IsTrue(mediaSanitizer.SetElementLayoutMethodIsCalled);
        }
        public void NotRewriteSourceAttribute_WhenResourcesCanBeRequestOnlyViaHttpsEqualsFalse()
        {
            // Arrange
            const string ExpectedResult = "http://www.example.com";
            var          htmlElement    = ElementFactory.CreateIFrame();

            htmlElement.Source = "http://www.example.com";
            ElementFactory.Document.Body.Append(htmlElement);

            // Act
            var actualResult = new MediaSanitizerAccessor().SanitizeCore <IHtmlInlineFrameElement>(ElementFactory.Document, htmlElement, "amp-iframe");

            // Assert
            Assert.AreEqual(ExpectedResult, actualResult.GetAttribute("src"));
        }
Ejemplo n.º 14
0
        public void SetLayoutAttributeToNoDisplay_WhenTheHtmlElementHasStyleAttributeVisibilityHidden()
        {
            // Arrange
            const string ExpectedResult = "nodisplay";
            var          element        = ElementFactory.CreateIFrame();

            element.SetAttribute("style", "visibility:hidden");
            var ampElement = ElementFactory.Create("amp-iframe");

            // Act
            new MediaSanitizerAccessor().SetElementLayout(element, ampElement);

            // Assert
            Assert.AreEqual(ExpectedResult, ampElement.GetAttribute("layout"));
        }
        public void ReturnFalse_WhenElementIsIFrameElementButRunConfigurationIsMissing()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateIFrame();

            htmlElement.Source = "http://www.example.com/example-resource";

            var iframeSanitizer = new IFrameSanitizer();

            // Act
            var actualResult = iframeSanitizer.CanSanitize(htmlElement);

            // Assert
            Assert.IsFalse(actualResult);
        }
Ejemplo n.º 16
0
        public void NotSetLayoutAttribute_WhenTheAmpElementAlreadyHasALayoutAttribute()
        {
            // Arrange
            const string ExpectedResult = "nodisplay";
            var          element        = ElementFactory.CreateIFrame();

            var ampElement = ElementFactory.Create("amp-iframe");

            ampElement.SetAttribute("layout", "nodisplay");

            // Act
            new MediaSanitizerAccessor().SetMediaElementLayout(element, ampElement);

            // Assert
            Assert.AreEqual(ExpectedResult, ampElement.GetAttribute("layout"));
        }
Ejemplo n.º 17
0
        public void SetLayoutAttributeToFixedHeight_WhenTheAmpElementHasOnlyHeightAttribute()
        {
            // Arrange
            const string ExpectedResult = "fixed-height";
            var          element        = ElementFactory.CreateIFrame();

            element.DisplayHeight = 100;

            var ampElement = ElementFactory.Create("amp-iframe");

            // Act
            new MediaSanitizerAccessor().SetMediaElementLayout(element, ampElement);

            // Assert
            Assert.AreEqual(ExpectedResult, ampElement.GetAttribute("layout"));
        }
        public void ReplaceTheHtmlElementWitTheAmpElement_Always()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateIFrame();
            var parent      = ElementFactory.Create("div");

            parent.AppendChild(htmlElement);
            ElementFactory.Document.Body.Append(parent);

            // Act
            var actualResult = new MediaSanitizerAccessor().SanitizeCore <IHtmlInlineFrameElement>(ElementFactory.Document, htmlElement, "amp-iframe");

            // Assert
            Assert.AreEqual(1, actualResult.Parent.ChildNodes.Length);
            Assert.AreEqual(actualResult.TagName, actualResult.Parent.ChildNodes[0].NodeName);
        }
Ejemplo n.º 19
0
        public void SetLayoutAttributeToResponsive_WhenTheAmpElementHasBothWidthAndHeightAttributes()
        {
            // Arrange
            const string ExpectedResult = "responsive";
            var          element        = ElementFactory.CreateIFrame();

            element.DisplayHeight = 100;
            element.DisplayWidth  = 200;

            var ampElement = ElementFactory.Create("amp-iframe");

            // Act
            new MediaSanitizerAccessor().SetMediaElementLayout(element, ampElement);

            // Assert
            Assert.AreEqual(ExpectedResult, ampElement.GetAttribute("layout"));
        }
Ejemplo n.º 20
0
        public void SetDataAttributes_WhenVideoParamsArePassed()
        {
            // Arrange
            var ampElement = ElementFactory.CreateIFrame();

            var videoParams = new NameValueCollection();

            videoParams["first"]  = "1";
            videoParams["second"] = "2";

            // Act
            new YouTubeVideoSanitizerAccessor().SetVideoParams(ampElement, videoParams);

            // Assert
            Assert.AreEqual("1", ampElement.GetAttribute("data-param-first"));
            Assert.AreEqual("2", ampElement.GetAttribute("data-param-second"));
        }
        public void CopyAllChildrenFromTheOriginalHtmlElementToTheAmpElement_Always()
        {
            // Arrange
            const int ExpectedResult = 2;
            var       htmlElement    = ElementFactory.CreateIFrame();
            var       firstChild     = ElementFactory.Create("input");
            var       secondChild    = ElementFactory.Create("p");

            htmlElement.Append(firstChild);
            htmlElement.Append(secondChild);
            ElementFactory.Document.Body.Append(htmlElement);

            // Act
            var actualResult = new MediaSanitizerAccessor().SanitizeCore <IHtmlInlineFrameElement>(ElementFactory.Document, htmlElement, "amp-iframe");

            // Assert
            Assert.AreEqual(ExpectedResult, actualResult.Children.Length);
        }
        public void ReturnTrue_WhenElementIsIFrameElementAndTheSourceIsDifferentThanTheDocumentsSource()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateIFrame();

            htmlElement.Source = "https://www.example.com/example-source";
            var iframeSanitizer = new IFrameSanitizer();
            var runContext      = new RunContext(new RunConfiguration {
                RelativeUrlsHost = "https://www.mywebsite.com"
            });

            iframeSanitizer.Configure(runContext);

            // Act
            var actualResult = iframeSanitizer.CanSanitize(htmlElement);

            // Assert
            Assert.IsTrue(actualResult);
        }
        public void ReturnFalse_WhenElementIsIFrameElementAndTheSourceEqualsTheDocumentSourceAndAllowSameOriginIsNotSpecified()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateIFrame();

            htmlElement.Source = "http://www.mywebsite.com/example-resource";
            var runContext = new RunContext(new RunConfiguration {
                RelativeUrlsHost = "https://www.mywebsite.com"
            });

            var iframeSanitizer = new IFrameSanitizer();

            iframeSanitizer.Configure(runContext);

            // Act
            var actualResult = iframeSanitizer.CanSanitize(htmlElement);

            // Assert
            Assert.IsFalse(actualResult);
        }
        public void CopyAllAttributesFromTheOriginalHtmlElementToTheAmpElement_Always()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateIFrame();

            htmlElement.Source        = "https://www.example.com";
            htmlElement.Id            = "iframeId";
            htmlElement.ClassName     = "someClassName";
            htmlElement.DisplayWidth  = 100;
            htmlElement.DisplayHeight = 200;
            ElementFactory.Document.Body.Append(htmlElement);

            // Act
            var actualResult = new MediaSanitizerAccessor().SanitizeCore <IHtmlInlineFrameElement>(ElementFactory.Document, htmlElement, "amp-iframe");

            // Assert
            Assert.AreEqual("https://www.example.com", actualResult.GetAttribute("src"));
            Assert.AreEqual("iframeId", actualResult.Id);
            Assert.AreEqual("someClassName", actualResult.ClassName);
            Assert.AreEqual(100, int.Parse(actualResult.GetAttribute("width")));
            Assert.AreEqual(200, int.Parse(actualResult.GetAttribute("height")));
        }
Ejemplo n.º 25
0
        public void ReturnAmpIFrameElementWithTheSameUrlExceptTheProtocolWhenTheUrlHasNoPorts_Always()
        {
            // Arrange
            const string ExpectedResult = "https://www.mysite.com";
            var          iframeElement  = ElementFactory.CreateIFrame();

            iframeElement.Source = "http://www.mysite.com";
            ElementFactory.Document.Body.Append(iframeElement);

            var runContext = new RunContext(new RunConfiguration {
                RelativeUrlsHost = "http://test-domain.com"
            });

            var iframeSanitizer = new IFrameSanitizer();

            iframeSanitizer.Configure(runContext);

            // Act
            var actualResult = iframeSanitizer.Sanitize(ElementFactory.Document, iframeElement);

            // Assert
            Assert.AreEqual(ExpectedResult, actualResult.GetAttribute("src"));
        }
Ejemplo n.º 26
0
        public void ReturnAmpIFrameElement_Always()
        {
            // Arrange
            const string ExpectedResult = "AMP-IFRAME";
            var          iframeElement  = ElementFactory.CreateIFrame();

            iframeElement.Source = "http://www.mysite.com";
            ElementFactory.Document.Body.Append(iframeElement);

            var runContext = new RunContext(new RunConfiguration {
                RelativeUrlsHost = "http://test-domain.com"
            });

            var iframeSanitizer = new IFrameSanitizer();

            iframeSanitizer.Configure(runContext);

            // Act
            var actualResult = iframeSanitizer.Sanitize(ElementFactory.Document, iframeElement);

            // Assert
            Assert.AreEqual(ExpectedResult, actualResult.TagName);
        }
Ejemplo n.º 27
0
        public void ReturnAmpIFrameElementWithSourceStartingWithHttps_Always()
        {
            // Arrange
            const string ExpectedResult = "https";
            var          iframeElement  = ElementFactory.CreateIFrame();

            iframeElement.Source = "http://www.mysite.com";
            ElementFactory.Document.Body.Append(iframeElement);

            var runContext = new RunContext(new RunConfiguration {
                RelativeUrlsHost = "http://test-domain.com"
            });

            var iframeSanitizer = new IFrameSanitizer();

            iframeSanitizer.Configure(runContext);

            // Act
            var actualResult     = iframeSanitizer.Sanitize(ElementFactory.Document, iframeElement);
            var ampElementSource = new Uri(actualResult.GetAttribute("src"));

            // Assert
            Assert.AreEqual(ExpectedResult, ampElementSource.Scheme);
        }
Ejemplo n.º 28
0
        public void ReturnAmpIFrameElementWithLayoutAttributeSetToNoDisplay_IfTheOriginalIFrameElementHasStyleVisibilityHidden()
        {
            // Arrange
            const string ExpectedResult = "nodisplay";
            var          iframeElement  = ElementFactory.CreateIFrame();

            iframeElement.SetAttribute("style", "visibility:hidden");
            iframeElement.Source = "http://www.mywebsite.com/example-resource";
            ElementFactory.Document.Body.Append(iframeElement);

            var runContext = new RunContext(new RunConfiguration {
                RelativeUrlsHost = "http://test-domain.com"
            });

            var iframeSanitizer = new IFrameSanitizer();

            iframeSanitizer.Configure(runContext);

            // Act
            var actualResult = iframeSanitizer.Sanitize(ElementFactory.Document, iframeElement);

            // Assert
            Assert.AreEqual(ExpectedResult, actualResult.GetAttribute("layout"));
        }
 public void ThowArgumentNullException_WhenAmpElementTagNameArgumentIsNull()
 {
     // Assert
     Ensure.ArgumentExceptionIsThrown(() => new MediaSanitizerAccessor().SanitizeCore <IHtmlInlineFrameElement>(ElementFactory.Document, ElementFactory.CreateIFrame(), null), "ampElementTagName");
 }