Beispiel #1
0
    class RenderTest(BaseRenderTest):
        @unittest.skipIf(NON_EXISTING_RESOLVABLE,
                         "non existing hosts are resolvable")
        def test_render_error(self):
            r = self.request({"url": "http://non-existent-host/"})
            self.assertStatusCode(r, 502)

        def test_timeout(self):
            r = self.request({
                "url": self.mockurl("delay?n=10"),
                "timeout": "0.5"
            })
            self.assertStatusCode(r, 504)

        def test_timeout_out_of_range(self):
            r = self.request({
                "url": self.mockurl("delay?n=10"),
                "timeout": "999"
            })
            self.assertStatusCode(r, 400)

        @skip_proxy
        def test_missing_url(self):
            r = self.request({})
            self.assertStatusCode(r, 400)
            self.assertTrue("url" in r.text)

        def test_jsalert(self):
            r = self.request({"url": self.mockurl("jsalert"), "timeout": "3"})
            self.assertStatusCode(r, 200)

        def test_jsconfirm(self):
            r = self.request({
                "url": self.mockurl("jsconfirm"),
                "timeout": "3"
            })
            self.assertStatusCode(r, 200)

        def test_iframes(self):
            r = self.request({"url": self.mockurl("iframes"), "timeout": "3"})
            self.assertStatusCode(r, 200)

        def test_wait(self):
            r1 = self.request({"url": self.mockurl("jsinterval")})
            r2 = self.request({"url": self.mockurl("jsinterval")})
            r3 = self.request({
                "url": self.mockurl("jsinterval"),
                "wait": "0.2"
            })
            self.assertStatusCode(r1, 200)
            self.assertStatusCode(r2, 200)
            self.assertStatusCode(r3, 200)
            self.assertEqual(r1.content, r2.content)
            self.assertNotEqual(r1.content, r3.content)

        def test_invalid_wait(self):
            for wait in ['foo', '11', '11.0']:
                r = self.request({
                    'url': self.mockurl("jsrender"),
                    'wait': wait
                })
                self.assertStatusCode(r, 400)

        @pytest.mark.skipif(
            not qt_551_plus(),
            reason=
            "resource_timeout doesn't work in Qt5 < 5.5.1. See issue #269 for details."
        )
        def test_resource_timeout(self):
            resp = self.request({
                'url': self.mockurl("show-image?n=10"),
                'timeout': "3",
                'resource_timeout': "0.5",
            })
            self.assertStatusCode(resp, 200)

        @pytest.mark.skipif(
            not qt_551_plus(),
            reason=
            "resource_timeout doesn't work in Qt5 < 5.5.1. See issue #269 for details."
        )
        def test_resource_timeout_abort_first(self):
            resp = self.request({
                'url': self.mockurl("slow.gif?n=3"),
                'resource_timeout': "0.5",
            })
            self.assertStatusCode(resp, 502)
Beispiel #2
0
class HarRenderTest(BaseHarRenderTest):
    """ Tests for HAR data in render.har endpoint """

    def test_jsrender(self):
        url = self.mockurl("jsrender")
        data = self.assertValidHar(url)
        self.assertRequestedUrlsStatuses(data, [(url, 200)])

    def test_jsalert(self):
        self.assertValidHar(self.mockurl("jsalert"), timeout=3)

    def test_jsconfirm(self):
        self.assertValidHar(self.mockurl("jsconfirm"), timeout=3)

    def test_iframes(self):
        data = self.assertValidHar(self.mockurl("iframes"), timeout=3)
        self.assertRequestedUrls(data, [
            self.mockurl("iframes"),
            self.mockurl('iframes/1.html'),
            self.mockurl('iframes/2.html'),
            self.mockurl('iframes/3.html'),
            # self.mockurl('iframes/4.html'),  # wait is zero, delayed iframe
            self.mockurl('iframes/5.html'),
            self.mockurl('iframes/6.html'),
            self.mockurl('iframes/script.js'),
            self.mockurl('iframes/script2.js', host="0.0.0.0"),
            self.mockurl('iframes/nested.html'),
        ])

    def test_iframes_wait(self):
        data = self.assertValidHar(self.mockurl("iframes"), timeout=3, wait=0.5)
        self.assertRequestedUrls(data, [
            self.mockurl("iframes"),
            self.mockurl('iframes/1.html'),
            self.mockurl('iframes/2.html'),
            self.mockurl('iframes/3.html'),
            self.mockurl('iframes/4.html'),  # wait is not zero, delayed iframe
            self.mockurl('iframes/5.html'),
            self.mockurl('iframes/6.html'),
            self.mockurl('iframes/script.js'),
            self.mockurl('iframes/script2.js', host="0.0.0.0"),
            self.mockurl('iframes/nested.html'),
        ])

    def test_timeout(self):
        r = self.request({"url": self.mockurl("delay?n=10"), "timeout": 0.5})
        self.assertStatusCode(r, 504)

    def test_wait(self):
        self.assertValidHar(self.mockurl("jsinterval"))
        self.assertValidHar(self.mockurl("jsinterval"), wait=0.2)

    def test_meta_redirect_nowait(self):
        data = self.assertValidHar(self.mockurl('meta-redirect0'))
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('meta-redirect0'), 200),
        ])

    def test_meta_redirect_wait(self):
        data = self.assertValidHar(self.mockurl('meta-redirect0'), wait=0.1)
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('meta-redirect0'), 200),
            (self.mockurl('meta-redirect-target/'), 200),
        ])

    def test_meta_redirect_delay_wait(self):
        data = self.assertValidHar(self.mockurl('meta-redirect1'), wait=0.1)
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('meta-redirect1'), 200),
        ])

    def test_meta_redirect_delay_wait_enough(self):
        data = self.assertValidHar(self.mockurl('meta-redirect1'), wait=0.3)
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('meta-redirect1'), 200),
            (self.mockurl('meta-redirect-target/'), 200),
        ])

    def test_meta_redirect_slowload2_wait_more(self):
        data = self.assertValidHar(self.mockurl('meta-redirect-slowload2'), wait=0.3)
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('meta-redirect-slowload2'), 200),
            (self.mockurl('slow.gif?n=2'), 200),
            (self.mockurl('meta-redirect-target/'), 200),
        ])

    def test_redirect_nowait(self):
        data = self.assertValidHar(self.mockurl('jsredirect'))
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('jsredirect'), 200),
        ])

    def test_redirect_wait(self):
        data = self.assertValidHar(self.mockurl('jsredirect'), wait=0.1)
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('jsredirect'), 200),
            (self.mockurl('jsredirect-target'), 200),
        ])

    def test_redirect_onload_nowait(self):
        data = self.assertValidHar(self.mockurl('jsredirect-onload'))
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('jsredirect-onload'), 200)  # not redirected
        ])

    def test_redirect_onload_wait(self):
        data = self.assertValidHar(self.mockurl('jsredirect-onload'), wait=0.1)
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('jsredirect-onload'), 200),
            (self.mockurl('jsredirect-target'), 200),
        ])

    def test_redirect_chain_nowait(self):
        data = self.assertValidHar(self.mockurl('jsredirect-chain'))
        # not redirected
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('jsredirect-chain'), 200),
        ])

    def test_response_body(self):
        url = self.mockurl('show-image')
        data = self.assertValidHar(url)
        for entry in data['log']['entries']:
            assert 'text' not in entry['response']['content']

        data = self.assertValidHar(url, response_body=1)
        entries = data['log']['entries']
        assert len(entries) == 2
        for entry in entries:
            assert 'text' in entry['response']['content']

        img_gif = requests.get(self.mockurl('slow.gif?n=0')).content
        b64_data = entries[1]['response']['content']['text']
        assert base64.b64decode(b64_data) == img_gif

    def test_redirect_chain_wait(self):
        data = self.assertValidHar(self.mockurl('jsredirect-chain'), wait=0.2)
        # redirected
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('jsredirect-chain'), 200),
            (self.mockurl('jsredirect'), 200),
            (self.mockurl('jsredirect-target'), 200),
        ])

    @pytest.mark.xfail(reason=qt_551_plus())  # why is it failing?
    def test_redirect_slowimage_nowait(self):
        data = self.assertValidHar(self.mockurl('jsredirect-slowimage'))
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('jsredirect-slowimage'), 200),
            (self.mockurl('jsredirect-target'), 200),
            (self.mockurl('slow.gif?n=2'), 0),
        ])

        pages = entries2pages(data["log"]["entries"])
        self.assertEqual(len(pages), 2)
        self.assertEqual(len(pages[0]), 2)  # jsredirect-slowimage and slow.gif?n=2
        self.assertEqual(len(pages[1]), 1)  # jsredirect-target
        self.assertEqual(pages[0][1]["response"]["statusText"], "cancelled")

    @pytest.mark.xfail(reason=qt_551_plus())  # why is it failing?
    def test_redirect_slowimage_wait(self):
        data = self.assertValidHar(self.mockurl('jsredirect-slowimage'), wait=0.1)
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('jsredirect-slowimage'), 200),
            (self.mockurl('jsredirect-target'), 200),
            (self.mockurl('slow.gif?n=2'), 0),
        ])

    @unittest.skipIf(NON_EXISTING_RESOLVABLE, "non existing hosts are resolvable")
    def test_bad_related(self):
        data = self.assertValidHar(self.mockurl("bad-related"))
        self.assertRequestedUrlsStatuses(data, [
            (self.mockurl('bad-related'), 200),
            ('http://non-existing/', 0),
        ])
        pages = entries2pages(data["log"]["entries"])
        self.assertEqual(len(pages), 1)
        self.assertEqual(len(pages[0]), 2)
        self.assertEqual(pages[0][1]["response"]["statusText"], "invalid_hostname")

    def test_cookies(self):
        data = self.assertValidHar(self.mockurl("set-cookie?key=foo&value=bar"))
        entry = data['log']['entries'][0]
        self.assertEqual(entry['response']['cookies'], [
            {
                'path': '',
                'name': 'foo',
                'httpOnly': False,
                'domain': '',
                'value': 'bar',
                'secure': False
             }
        ])