def test_Resource_content(): # noqa: N802 bndry = '---boundary1---' content = b'-' mhtarc = mhtml.MHTMLArchive(content, None, 0, bndry) # this should not happen # TODO: maybe later a way to construct a Resource without a MHTMLArchive? # but how to use standalone? (rather a construction method/factory) res = mhtml.Resource(mhtarc, None, 0, 0, 0) res._mhtml_file._content = None assert res.get_content() is None assert res.content is None assert res.content_with_headers is None res._mhtml_file = None assert res.get_content() is None assert res.content is None assert res.content_with_headers is None bndry_part = bytes('--' + bndry + '\r\n', 'ascii') bndry_end = bytes('--' + bndry + '--\r\n', 'ascii') content_header = b'H1: V1\r\n\r\n' content_content = b'Content\r\n' content = bndry_part + content_header + content_content + bndry_end # offsets in content offset = len(bndry_part) offset_content = offset + len(content_header) offset_end = offset_content + len(content_content) # objects mhtarc = mhtml.MHTMLArchive(content, None, 0, bndry) res = mhtml.Resource(mhtarc, None, offset, offset_content, offset_end) # check assert res.get_content() == content_content assert res.content_with_headers == content_header + content_content assert res.get_resource_range(-1) == (0, offset_end) assert res.get_resource_range(offset) == (0, offset_end) # TODO: may need an error case? # wrong offset or missing content? # update offsets wrong with pytest.raises(AssertionError): res._update_offsets('a') with pytest.raises(AssertionError): res._update_offsets(-3.4) with pytest.raises(AssertionError): res._update_offsets(None) # update offsets right res._update_offsets(-1) assert res._offset_start == offset - 1 assert res._offset_content == offset_content - 1 assert res._offset_end == offset_end - 1 res._update_offsets(3) assert res._offset_start == offset - 1 + 3 assert res._offset_content == offset_content - 1 + 3 assert res._offset_end == offset_end - 1 + 3
def test_Resource_properties(mocker): # noqa: N802 with pytest.raises(AssertionError, match='mhtml_file should be a MHTMLArchive'): mhtml.Resource(None, None, 0, 0, 0) mhtarc = mhtml.MHTMLArchive(b'', None, 0, '---boundary---') # default resource headers res = mhtml.Resource(mhtarc, None, 0, 0, 0) assert res.headers == mhtml.ResourceHeader() assert res.headers == res._headers assert mhtml.Resource(mhtarc, None, 0, 0, 0).headers == \ mhtml.ResourceHeader() assert mhtml.Resource(mhtarc, [], 0, 0, 0).headers == \ mhtml.ResourceHeader() assert mhtml.Resource(mhtarc, {}, 0, 0, 0).headers == \ mhtml.ResourceHeader() # properties mock_headers = mocker.Mock(content_type='content-abc', location='location-abc', encoding='encoding-123') res._headers = mock_headers assert res.content_type == 'content-abc' assert res.location == 'location-abc' assert res.encoding == 'encoding-123' # filename mock_method = mocker.patch('mhtml.make_filename') mock_method.return_value = 'name' assert res.get_short_filename(default='foo') == 'name' mock_method.assert_called_once_with(res._headers, default='foo') # content mock_prop = mocker.Mock(return_value=b'123') res.get_content = mock_prop assert res.content == b'123' mock_prop.assert_called_once_with() # content set mock_prop_set = mocker.Mock() res.set_content = mock_prop_set res.content = b'abc123' mock_prop_set.assert_called_once_with(b'abc123')
def test_Resource_content_set(mocker): # noqa: N802 bndry = '---boundary1---' content = b'-' mhtarc = mhtml.MHTMLArchive(content, None, 0, bndry) res = mhtml.Resource(mhtarc, None, 0, 0, 0) # check for correct references res._mhtml_file._content = 'abc' assert res.set_content(b'') is False res._mhtml_file = None assert res.set_content(b'') is False # test that calls are done mhtarc = mhtml.MHTMLArchive(content, None, 0, bndry) res = mhtml.Resource(mhtarc, None, 0, 0, 0) mock_method_replace = mocker.Mock(return_value=1) mhtarc.replace_content = mock_method_replace assert res.set_content(b'abc123') == 1 mock_method_replace.assert_called_once_with(res, b'abc123')
def test_content_hashing(mocker): content = b'content' content1 = b'abc' content2 = b'123_abc' mock_method_content = mocker.PropertyMock(return_value=content) mock_method_content1 = mocker.PropertyMock(return_value=content1) mock_method_content2 = mocker.PropertyMock(return_value=content2) # with the first two variants, the object can be created beforehand # var1: maybe no cleanup afterwards? # type(mhtarc).content = mock_method_content # var2: treats property as object? # mocker.patch('mhtml.MHTMLArchive.content', # new_callable=mock_method_content) # var3: should probably be used mocker.patch.object(mhtml.MHTMLArchive, 'content', mock_method_content) mocker.patch.object(mhtml.Resource, 'content', mock_method_content1) mocker.patch.object(mhtml.Resource, 'content_with_headers', mock_method_content2) mhtarc = mhtml.MHTMLArchive(b'-', None, 0, b'--') res = mhtml.Resource(mhtarc, None, 0, 0, 0) import hashlib m = hashlib.sha256() m.update(content) hash_content = m.digest() m = hashlib.sha256() m.update(content1) hash_content1 = m.digest() m = hashlib.sha256() m.update(content2) hash_content2 = m.digest() assert mhtarc.content_hash == hash_content mock_method_content.assert_called_once_with() assert res.content_hash == hash_content1 mock_method_content1.assert_called_once_with() assert res.content_with_headers_hash == hash_content2 mock_method_content2.assert_called_once_with()
def test_Resource_content_get(mocker): # noqa: N802 bndry = '---boundary1---' bndry_part = bytes('--' + bndry + '\r\n', 'ascii') bndry_end = bytes('--' + bndry + '--\r\n', 'ascii') content_header = b'H1: V1\r\n\r\n' content_content = b'Content\r\n' content = bndry_part + content_header + content_content + bndry_end # offsets in content offset = len(bndry_part) offset_content = offset + len(content_header) offset_end = offset_content + len(content_content) # objects mhtarc = mhtml.MHTMLArchive(content, None, 0, bndry) res = mhtml.Resource(mhtarc, None, offset, offset_content, offset_end) assert res.get_content() == content_content assert res.get_content(decode=False) == content_content # TODO: decoded content is currently None since no decoder implemented assert res.get_content(decode=True) is None # TODO: this currently needs work ... mock_headers = mocker.Mock() res._headers = mock_headers mock_headers.encoding = 'binary' assert res.get_content(decode=True) == content_content mock_headers.encoding = 'base64' assert res.get_content(decode=True) is None mock_headers.encoding = 'Quoted-Printable' assert res.get_content(decode=True) is None # default to binary # TODO: or should default to None? mock_headers.encoding = 'base64binary' assert res.get_content(decode=True) is None