From 60fe79f2d898757f4f20aa89015e86cd63ef7871 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sat, 3 Nov 2018 13:32:38 +0100 Subject: Switch site loader to async DevTools communication --- crocoite/test_browser.py | 232 ++++++++++++++++++++++------------------------- 1 file changed, 110 insertions(+), 122 deletions(-) (limited to 'crocoite/test_browser.py') diff --git a/crocoite/test_browser.py b/crocoite/test_browser.py index 5c7fc69..030ffb1 100644 --- a/crocoite/test_browser.py +++ b/crocoite/test_browser.py @@ -18,13 +18,19 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +import logging +import asyncio import pytest from operator import itemgetter +from aiohttp import web from http.server import BaseHTTPRequestHandler -from pychrome.exceptions import TimeoutException -from .browser import Item, SiteLoader, ChromeService, NullService, BrowserCrashed -from .logger import Logger, Consumer +from .browser import Item, SiteLoader, ChromeService, NullService +from .logger import Logger, Consumer, JsonPrintConsumer +from .devtools import Crashed + +# if you want to know what’s going on: +#logging.basicConfig(level=logging.DEBUG) class TItem (Item): """ This should be as close to Item as possible """ @@ -32,21 +38,14 @@ class TItem (Item): __slots__ = ('bodySend', '_body', '_requestBody') base = 'http://localhost:8000/' - def __init__ (self, path, status, headers, bodyReceive, bodySend=None, requestBody=None, failed=False): + def __init__ (self, path, status, headers, bodyReceive, bodySend=None, requestBody=None, failed=False, isRedirect=False): super ().__init__ (tab=None) self.chromeResponse = {'response': {'headers': headers, 'status': status, 'url': self.base + path}} - self._body = bodyReceive, False + self.body = bodyReceive, False self.bodySend = bodyReceive if not bodySend else bodySend - self._requestBody = requestBody, False + self.requestBody = requestBody, False self.failed = failed - - @property - def body (self): - return self._body - - @property - def requestBody (self): - return self._requestBody + self.isRedirect = isRedirect testItems = [ TItem ('binary', 200, {'Content-Type': 'application/octet-stream'}, b'\x00\x01\x02', failed=True), @@ -66,15 +65,15 @@ testItems = [ TItem ('image', 200, {'Content-Type': 'image/png'}, # 1×1 png image b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x00\x00\x00\x00:~\x9bU\x00\x00\x00\nIDAT\x08\x1dc\xf8\x0f\x00\x01\x01\x01\x006_g\x80\x00\x00\x00\x00IEND\xaeB`\x82'), - TItem ('empty', 200, {}, b''), - TItem ('redirect/301/empty', 301, {'Location': '/empty'}, b''), - TItem ('redirect/301/redirect/301/empty', 301, {'Location': '/redirect/301/empty'}, b''), + TItem ('empty', 200, {'Content-Type': 'text/plain'}, b''), + TItem ('redirect/301/empty', 301, {'Location': '/empty'}, b'', isRedirect=True), + TItem ('redirect/301/redirect/301/empty', 301, {'Location': '/redirect/301/empty'}, b'', isRedirect=True), TItem ('nonexistent', 404, {}, b''), - TItem ('html', 200, {'Content-Type': 'html'}, + TItem ('html', 200, {'Content-Type': 'text/html'}, ''.encode ('utf8')), - TItem ('html/alert', 200, {'Content-Type': 'html'}, - ''.encode ('utf8')), - TItem ('html/fetchPost', 200, {'Content-Type': 'html'}, + TItem ('html/alert', 200, {'Content-Type': 'text/html'}, + ''.encode ('utf8')), + TItem ('html/fetchPost', 200, {'Content-Type': 'text/html'}, r"""