tests: Adjust FakeNetworkReply to work with all headers.

This commit is contained in:
Florian Bruhin 2014-08-12 09:03:38 +02:00
parent be811e5c62
commit 157f5b211f
2 changed files with 40 additions and 26 deletions

View File

@ -25,6 +25,7 @@ from unittest.mock import Mock
from PyQt5.QtCore import QPoint, QProcess from PyQt5.QtCore import QPoint, QProcess
from PyQt5.QtWebKit import QWebElement from PyQt5.QtWebKit import QWebElement
from PyQt5.QtNetwork import QNetworkRequest
class ConfigStub: class ConfigStub:
@ -205,10 +206,17 @@ class FakeNetworkReply:
"""QNetworkReply stub which provides a Content-Disposition header.""" """QNetworkReply stub which provides a Content-Disposition header."""
def __init__(self, content_disposition=None, url=None): KNOWN_HEADERS = {
QNetworkRequest.ContentTypeHeader: 'Content-Type',
}
def __init__(self, headers=None, url=None):
if url is None: if url is None:
url = FakeUrl() url = FakeUrl()
self._content_disposition = content_disposition if headers is None:
self.headers = {}
else:
self.headers = headers
self.url = Mock(return_value=url) self.url = Mock(return_value=url)
def hasRawHeader(self, name): def hasRawHeader(self, name):
@ -219,15 +227,8 @@ class FakeNetworkReply:
Return: Return:
True if the header is present, False if not. True if the header is present, False if not.
Raise:
ValueError: If a header other than Content-Disposition is
requested.
""" """
if name == 'Content-Disposition': return name in self.headers
return self._content_disposition is not None
else:
raise ValueError("Invalid header {}".format(name))
def rawHeader(self, name): def rawHeader(self, name):
"""Get the raw header data of a header. """Get the raw header data of a header.
@ -237,17 +238,30 @@ class FakeNetworkReply:
Return: Return:
The header data, as ISO-8859-1 encoded bytes() object. The header data, as ISO-8859-1 encoded bytes() object.
Raise:
ValueError: If a header other than Content-Disposition is
requested.
""" """
if name != 'Content-Disposition': return self.headers[name].encode('iso-8859-1')
raise ValueError("Invalid header {}".format(name))
cd = self._content_disposition def header(self, known_header):
if cd is None: """Get a known header.
raise ValueError("Content-Disposition is None!")
return cd.encode('iso-8859-1') Args:
known_header: A QNetworkRequest::KnownHeaders member.
"""
key = self.KNOWN_HEADERS[known_header]
try:
return self.headers[key]
except KeyError:
return None
def setHeader(self, known_header, value):
"""Set a known header.
Args:
known_header: A QNetworkRequest::KnownHeaders member.
value: The value to set.
"""
key = self.KNOWN_HEADERS[known_header]
self.headers[key] = value
class FakeQProcess: class FakeQProcess:

View File

@ -39,7 +39,7 @@ class AttachmentTestCase(unittest.TestCase):
def _check_filename(self, header, filename): def _check_filename(self, header, filename):
"""Check if the passed header has the given filename.""" """Check if the passed header has the given filename."""
reply = FakeNetworkReply(header) reply = FakeNetworkReply(headers={'Content-Disposition': header})
cd_inline, cd_filename = httputils.parse_content_disposition(reply) cd_inline, cd_filename = httputils.parse_content_disposition(reply)
self.assertIsNotNone(cd_filename) self.assertIsNotNone(cd_filename)
self.assertEqual(cd_filename, filename) self.assertEqual(cd_filename, filename)
@ -47,14 +47,14 @@ class AttachmentTestCase(unittest.TestCase):
def _check_ignored(self, header): def _check_ignored(self, header):
"""Check if the passed header is ignored.""" """Check if the passed header is ignored."""
reply = FakeNetworkReply(header) reply = FakeNetworkReply(headers={'Content-Disposition': header})
cd_inline, cd_filename = httputils.parse_content_disposition(reply) cd_inline, cd_filename = httputils.parse_content_disposition(reply)
self.assertEqual(cd_filename, DEFAULT_NAME) self.assertEqual(cd_filename, DEFAULT_NAME)
self.assertTrue(cd_inline) self.assertTrue(cd_inline)
def _check_unnamed(self, header): def _check_unnamed(self, header):
"""Check if the passed header results in an unnamed attachment.""" """Check if the passed header results in an unnamed attachment."""
reply = FakeNetworkReply(header) reply = FakeNetworkReply(headers={'Content-Disposition': header})
cd_inline, cd_filename = httputils.parse_content_disposition(reply) cd_inline, cd_filename = httputils.parse_content_disposition(reply)
self.assertEqual(cd_filename, DEFAULT_NAME) self.assertEqual(cd_filename, DEFAULT_NAME)
self.assertFalse(cd_inline) self.assertFalse(cd_inline)
@ -69,14 +69,14 @@ class InlineTests(unittest.TestCase):
def _check_filename(self, header, filename): def _check_filename(self, header, filename):
"""Check if the passed header has the given filename.""" """Check if the passed header has the given filename."""
reply = FakeNetworkReply(header) reply = FakeNetworkReply(headers={'Content-Disposition': header})
cd_inline, cd_filename = httputils.parse_content_disposition(reply) cd_inline, cd_filename = httputils.parse_content_disposition(reply)
self.assertEqual(cd_filename, filename) self.assertEqual(cd_filename, filename)
self.assertTrue(cd_inline) self.assertTrue(cd_inline)
def _check_ignored(self, header): def _check_ignored(self, header):
"""Check if the passed header is ignored.""" """Check if the passed header is ignored."""
reply = FakeNetworkReply(header) reply = FakeNetworkReply(headers={'Content-Disposition': header})
cd_inline, cd_filename = httputils.parse_content_disposition(reply) cd_inline, cd_filename = httputils.parse_content_disposition(reply)
self.assertEqual(cd_filename, DEFAULT_NAME) self.assertEqual(cd_filename, DEFAULT_NAME)
self.assertTrue(cd_inline) self.assertTrue(cd_inline)
@ -134,7 +134,7 @@ class AttachmentTests(AttachmentTestCase):
UA should offer to download the resource. UA should offer to download the resource.
""" """
reply = FakeNetworkReply('attachment') reply = FakeNetworkReply(headers={'Content-Disposition': 'attachment'})
cd_inline, cd_filename = httputils.parse_content_disposition(reply) cd_inline, cd_filename = httputils.parse_content_disposition(reply)
self.assertFalse(cd_inline) self.assertFalse(cd_inline)
self.assertEqual(cd_filename, DEFAULT_NAME) self.assertEqual(cd_filename, DEFAULT_NAME)