diff --git a/pywb/cdx/cdxobject.py b/pywb/cdx/cdxobject.py index b64b0861..7a190be4 100644 --- a/pywb/cdx/cdxobject.py +++ b/pywb/cdx/cdxobject.py @@ -46,10 +46,16 @@ class CDXObject(OrderedDict): "orig.length", "orig.offset", "orig.filename"] ] - def __init__(self, cdxline): + def __init__(self, cdxline=''): OrderedDict.__init__(self) cdxline = cdxline.rstrip() + + # Allows for filling the fields later or in a custom way + if not cdxline: + self.cdxline = cdxline + return + fields = cdxline.split(' ') cdxformat = None diff --git a/pywb/cdx/test/test_cdxobject.py b/pywb/cdx/test/test_cdxobject.py new file mode 100644 index 00000000..13e242a7 --- /dev/null +++ b/pywb/cdx/test/test_cdxobject.py @@ -0,0 +1,32 @@ +from pywb.cdx.cdxobject import CDXObject, IDXObject, CDXException +from pytest import raises + +def test_empty_cdxobject(): + x = CDXObject('') + assert len(x) == 0 + +def test_invalid_cdx_format(): + with raises(CDXException): + x = CDXObject('a b c') + + +def _make_line(fields): + line = ' '.join(['-'] * fields) + x = CDXObject(line) + assert len(x) == fields + assert str(x) == line + +def test_valid_cdx_formats(): + # Currently supported cdx formats, 9, 11, 12, 14 field + # See CDXObject for more details + _make_line(9) + _make_line(12) + + _make_line(11) + _make_line(14) + +def test_invalid_idx_format(): + with raises(CDXException): + x = IDXObject('a b c') + +