summaryrefslogtreecommitdiff
path: root/crocoite
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2018-08-05 11:32:50 +0200
committerLars-Dominik Braun <lars@6xq.net>2018-08-05 11:32:50 +0200
commit6e3902be3cc8c5ef982885dad19701bb60214eb1 (patch)
treee64f15b65a2ed5f8ccd0c9000b5d3b61872bed28 /crocoite
parentfabd84cb10beab2b2e5aed7489fc04df9fda7e83 (diff)
downloadcrocoite-6e3902be3cc8c5ef982885dad19701bb60214eb1.tar.gz
crocoite-6e3902be3cc8c5ef982885dad19701bb60214eb1.tar.bz2
crocoite-6e3902be3cc8c5ef982885dad19701bb60214eb1.zip
test_browser: Properly handle failed requests
Fixes test failures. Very fragile code unfortunately.
Diffstat (limited to 'crocoite')
-rw-r--r--crocoite/browser.py9
-rw-r--r--crocoite/test_browser.py20
2 files changed, 14 insertions, 15 deletions
diff --git a/crocoite/browser.py b/crocoite/browser.py
index c3ef5ce..b5ea4e3 100644
--- a/crocoite/browser.py
+++ b/crocoite/browser.py
@@ -48,16 +48,15 @@ class Item:
self.failed = False
def __repr__ (self):
- return '<Item {}>'.format (self.request['url'])
+ return '<Item {}>'.format (self.url)
@property
def request (self):
- return self.chromeRequest['request']
+ return self.chromeRequest.get ('request', {})
@property
def response (self):
- assert not self.failed, "you must not access response if failed is set"
- return self.chromeResponse['response']
+ return self.chromeResponse.get ('response', {})
@property
def initiator (self):
@@ -73,7 +72,7 @@ class Item:
@property
def url (self):
- return self.response['url']
+ return self.response.get ('url', self.request.get ('url'))
@property
def parsedUrl (self):
diff --git a/crocoite/test_browser.py b/crocoite/test_browser.py
index 483a298..5c7fc69 100644
--- a/crocoite/test_browser.py
+++ b/crocoite/test_browser.py
@@ -32,12 +32,13 @@ class TItem (Item):
__slots__ = ('bodySend', '_body', '_requestBody')
base = 'http://localhost:8000/'
- def __init__ (self, path, status, headers, bodyReceive, bodySend=None, requestBody=None):
+ def __init__ (self, path, status, headers, bodyReceive, bodySend=None, requestBody=None, failed=False):
super ().__init__ (tab=None)
self.chromeResponse = {'response': {'headers': headers, 'status': status, 'url': self.base + path}}
self._body = bodyReceive, False
self.bodySend = bodyReceive if not bodySend else bodySend
self._requestBody = requestBody, False
+ self.failed = failed
@property
def body (self):
@@ -48,12 +49,12 @@ class TItem (Item):
return self._requestBody
testItems = [
- TItem ('binary', 200, {'Content-Type': 'application/octet-stream'}, b'\x00\x01\x02'),
+ TItem ('binary', 200, {'Content-Type': 'application/octet-stream'}, b'\x00\x01\x02', failed=True),
TItem ('attachment', 200,
{'Content-Type': 'text/plain; charset=utf-8',
'Content-Disposition': 'attachment; filename="attachment.txt"',
},
- 'This is a simple text file with umlauts. ÄÖU.'.encode ('utf8')),
+ 'This is a simple text file with umlauts. ÄÖU.'.encode ('utf8'), failed=True),
TItem ('encoding/utf8', 200, {'Content-Type': 'text/plain; charset=utf-8'},
'This is a test, äöü μνψκ ¥¥¥¿ýý¡'.encode ('utf8')),
TItem ('encoding/iso88591', 200, {'Content-Type': 'text/plain; charset=ISO-8859-1'},
@@ -153,11 +154,14 @@ def itemsLoaded (l, items):
item = l.queue.popleft ()
if isinstance (item, Exception):
raise item
- assert not item.failed
assert item.chromeResponse is not None
golden = items.pop (item.parsedUrl.path)
if not golden:
assert False, 'url {} not supposed to be fetched'.format (item.url)
+ assert item.failed == golden.failed
+ if item.failed:
+ # response will be invalid if request failed
+ continue
assert item.body[0] == golden.body[0]
assert item.requestBody[0] == golden.requestBody[0]
assert item.response['status'] == golden.response['status']
@@ -191,9 +195,7 @@ def test_encoding (loader):
def test_binary (loader):
""" Browser should ignore content it cannot display (i.e. octet-stream) """
- with loader ('/binary') as l:
- l.start ()
- itemsLoaded (l, [])
+ literalItem (loader, testItemMap['/binary'])
def test_image (loader):
""" Images should be displayed inline """
@@ -201,9 +203,7 @@ def test_image (loader):
def test_attachment (loader):
""" And downloads won’t work in headless mode, even if it’s just a text file """
- with loader ('/attachment') as l:
- l.start ()
- itemsLoaded (l, [])
+ literalItem (loader, testItemMap['/attachment'])
def test_html (loader):
literalItem (loader, testItemMap['/html'], [testItemMap['/image'], testItemMap['/nonexistent']])