tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_pagesource.py (2090B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 from urllib.parse import quote
      6 
      7 from marionette_harness import MarionetteTestCase
      8 
      9 
     10 def inline(doc, mime=None, charset=None):
     11    mime = "html" if mime is None else mime
     12    charset = "utf-8" if (charset is None) else charset
     13    return "data:text/{};charset={},{}".format(mime, charset, quote(doc))
     14 
     15 
     16 class TestPageSource(MarionetteTestCase):
     17    def testShouldReturnTheSourceOfAPage(self):
     18        test_html = inline("<body><p> Check the PageSource</body>")
     19        self.marionette.navigate(test_html)
     20        source = self.marionette.page_source
     21        from_web_api = self.marionette.execute_script(
     22            "return document.documentElement.outerHTML"
     23        )
     24        self.assertTrue("<html" in source)
     25        self.assertTrue("PageSource" in source)
     26        self.assertEqual(source, from_web_api)
     27 
     28    def testShouldReturnTheSourceOfAPageWhenThereAreUnicodeChars(self):
     29        test_html = inline(
     30            '<head><meta http-equiv="pragma" content="no-cache"/></head><body><!-- the \u00ab section[id^="wifi-"] \u00bb selector.--></body>'
     31        )
     32        self.marionette.navigate(test_html)
     33        # if we don't throw on the next line we are good!
     34        source = self.marionette.page_source
     35        from_web_api = self.marionette.execute_script(
     36            "return document.documentElement.outerHTML"
     37        )
     38        self.assertEqual(source, from_web_api)
     39 
     40    def testShouldReturnAXMLDocumentSource(self):
     41        test_xml = inline("<xml><foo><bar>baz</bar></foo></xml>", "xml")
     42        self.marionette.navigate(test_xml)
     43        source = self.marionette.page_source
     44        from_web_api = self.marionette.execute_script(
     45            "return document.documentElement.outerHTML"
     46        )
     47        import re
     48 
     49        self.assertEqual(
     50            re.sub(r"\s", "", source), "<xml><foo><bar>baz</bar></foo></xml>"
     51        )
     52        self.assertEqual(source, from_web_api)