예제 #1
0
    def test_probing_binary_async(self):
        """
        Tests the general working cases for probing a page with binary content
        using an asynchronous method with Pyppeteer
        """

        # mock of the page to be accessed, with a binary MIME type and a 200
        # status code (the text value of "entry found" is included so we can
        # check if it is properly discarded)
        page = create_mock_pyp_page("application/octet-stream", 200,
                                    "entry found")

        # checks the page for a 200 code and binary content
        probe = EntryProbing(PyppeteerProbingRequest(page))
        probe.add_response_handler(HTTPStatusProbingResponse(200))\
             .add_response_handler(BinaryFormatProbingResponse())
        self.assertTrue(self.loop.run_until_complete(
            probe.async_check_entry()))

        # the same as above but checks for text content
        probe = EntryProbing(PyppeteerProbingRequest(page))
        probe.add_response_handler(HTTPStatusProbingResponse(200))\
             .add_response_handler(BinaryFormatProbingResponse(opposite=True))
        self.assertFalse(
            self.loop.run_until_complete(probe.async_check_entry()))

        # checks for the string "entry found" in the content (should fail
        # since the text is ignored)
        probe = EntryProbing(PyppeteerProbingRequest(page))
        probe.add_response_handler(TextMatchProbingResponse("entry found"))
        self.assertFalse(
            self.loop.run_until_complete(probe.async_check_entry()))
예제 #2
0
    def test_probing_not_found_async(self):
        """
        Tests the general working cases for probing a not found entry using an
        asynchronous method
        """

        # mock of the page to be accessed
        page = create_mock_pyp_page("text/html", 404, "entry not found")

        # checks the page for a 200 code and the string "entry found"
        probe = EntryProbing(PyppeteerProbingRequest(page))
        probe.add_response_handler(HTTPStatusProbingResponse(200))\
             .add_response_handler(TextMatchProbingResponse("entry found"))
        self.assertFalse(
            self.loop.run_until_complete(probe.async_check_entry())
        )

        # checks the page for a non-404 code and the string "entry found"
        probe = EntryProbing(PyppeteerProbingRequest(page))
        probe.add_response_handler(HTTPStatusProbingResponse(404,
                                                             opposite=True))\
            .add_response_handler(TextMatchProbingResponse("entry found"))
        self.assertFalse(
            self.loop.run_until_complete(probe.async_check_entry())
        )

        # checks the page for a 404 code and the string "not found"
        probe = EntryProbing(PyppeteerProbingRequest(page))
        probe.add_response_handler(HTTPStatusProbingResponse(404))\
             .add_response_handler(TextMatchProbingResponse("not found"))
        self.assertTrue(
            self.loop.run_until_complete(probe.async_check_entry())
        )

        # checks the page for a non-503 code
        probe = EntryProbing(PyppeteerProbingRequest(page))
        probe.add_response_handler(HTTPStatusProbingResponse(503,
                                                             opposite=True))
        self.assertTrue(
            self.loop.run_until_complete(probe.async_check_entry())
        )

        # check if response is stored properly
        probe = EntryProbing(PyppeteerProbingRequest(page))
        self.assertIsNone(probe.response)
        self.loop.run_until_complete(probe.async_check_entry())
        self.assertTrue(isinstance(probe.response, ResponseData))
예제 #3
0
    def test_invalid_req_pyp(self):
        """
        Tests invalid requests with Pyppeteer
        """

        # initializing the Probing class with the wrong type
        self.assertRaises(TypeError, PyppeteerProbingRequest, 100)

        # calling the process() method without requesting a page after the
        # constructor is called
        # setting the trigger_response parameter to False we make sure the
        # response callback is not called, simulating this scenario
        pyp_handler = create_mock_pyp_page("text/html", 200, "test content",
                                           trigger_response=False)
        probe = PyppeteerProbingRequest(pyp_handler)

        with self.assertRaises(ValueError):
            # process() should raise a ValueError
            result = self.loop.run_until_complete(probe.process())
예제 #4
0
    def test_succesful_req_pyp(self):
        """
        Tests valid requests with Pyppeteer
        """

        # creates a mock of the Pyppeteer page handler
        pyp_handler = create_mock_pyp_page("text/html", 200, "test content")
        probe = PyppeteerProbingRequest(pyp_handler)
        # in a real scenario this is where we would request the URL in
        # pyp_handler, e.g.:
        # await pyp_handler.goto('https://www.example.com')
        result = self.loop.run_until_complete(probe.process())

        # check if the response event was setup correctly (we need to use name
        # mangling to access the private method for this specific purpose)
        expected = [('response',
                    probe._PyppeteerProbingRequest__intercept_response), {}]
        self.assertEqual(list(pyp_handler.on.call_args), expected)

        self.assertEqual(result.headers, {'content-type': 'text/html'})
        self.assertEqual(result.status_code, 200)
        self.assertEqual(result.text, "test content")

        # Another test with a different response
        pyp_handler = create_mock_pyp_page("text/json", 404, "")
        probe = PyppeteerProbingRequest(pyp_handler)
        result = self.loop.run_until_complete(probe.process())

        expected = [('response',
                    probe._PyppeteerProbingRequest__intercept_response), {}]
        self.assertEqual(list(pyp_handler.on.call_args), expected)

        self.assertEqual(result.headers, {'content-type': 'text/json'})
        self.assertEqual(result.status_code, 404)
        self.assertEqual(result.text, "")
예제 #5
0
    def test_probing_found_async(self):
        """
        Tests the general working cases for probing a found entry using an
        asynchronous method with Pyppeteer
        """

        # mock of the page to be accessed
        page = create_mock_pyp_page("text/html", 200, "entry found")

        # checks the page for a 200 code, the string "entry found" and a text
        # type
        probe = EntryProbing(PyppeteerProbingRequest(page))
        probe.add_response_handler(BinaryFormatProbingResponse(opposite=True))\
             .add_response_handler(HTTPStatusProbingResponse(200))\
             .add_response_handler(TextMatchProbingResponse("entry found"))

        self.assertTrue(
            self.loop.run_until_complete(probe.async_check_entry())
        )

        # the same as above but checks for a binary file
        probe = EntryProbing(PyppeteerProbingRequest(page))
        probe.add_response_handler(HTTPStatusProbingResponse(200))\
             .add_response_handler(BinaryFormatProbingResponse())\
             .add_response_handler(TextMatchProbingResponse("entry found"))
        self.assertFalse(
            self.loop.run_until_complete(probe.async_check_entry())
        )

        # checks the page for a non-404 code and the string "entry found"
        probe = EntryProbing(PyppeteerProbingRequest(page))
        probe.add_response_handler(HTTPStatusProbingResponse(404,
                                                             opposite=True))\
            .add_response_handler(TextMatchProbingResponse("entry found"))
        self.assertTrue(
            self.loop.run_until_complete(probe.async_check_entry())
        )

        # checks the page for a 404 code, a 200 code, and the string
        # "entry found" (should always fail)
        probe = EntryProbing(PyppeteerProbingRequest(page))
        probe.add_response_handler(HTTPStatusProbingResponse(404))\
             .add_response_handler(HTTPStatusProbingResponse(200))\
             .add_response_handler(TextMatchProbingResponse("entry found"))
        self.assertFalse(
            self.loop.run_until_complete(probe.async_check_entry())
        )

        # just requests without any checks (should default to True)
        probe = EntryProbing(PyppeteerProbingRequest(page))
        self.assertTrue(
            self.loop.run_until_complete(probe.async_check_entry())
        )

        # check if response is stored properly
        probe = EntryProbing(PyppeteerProbingRequest(page))
        self.assertIsNone(probe.response)
        self.loop.run_until_complete(probe.async_check_entry())
        self.assertTrue(isinstance(probe.response, ResponseData))