def test__str__simplestNonEmptyDict(self): "__str__ - Dict with one header key, 1 associated string value should result in a single leading Key: Value line" x = MimeDict(Hello="World") lines = str(x).splitlines(True) self.assertEqual("Hello: World\r\n", lines[0]) x = MimeDict(Sheep="Dolly") lines = str(x).splitlines(True) self.assertEqual("Sheep: Dolly\r\n", lines[0])
def test__str__FourListsDifferingLengths(self): "__str__ - Dict with 4 keys, each with lists of multiple values, all get inserted, possibly mixed up" x = MimeDict( Greeting=["Hello", "Bonjour", "Hi", "Greetings"], Parting=["Farewell", "Goodbye", "Ciao"], Numbers=["One", "Two", "Three", "Four", "Five", "Six", "Seven"], Empty=[]) lines = str(x).splitlines(True) header = lines[:15] header.sort() self.assertEqual("Empty: \r\n", header[0]) self.assertEqual("Greeting: Bonjour\r\n", header[1]) self.assertEqual("Greeting: Greetings\r\n", header[2]) self.assertEqual("Greeting: Hello\r\n", header[3]) self.assertEqual("Greeting: Hi\r\n", header[4]) self.assertEqual("Numbers: Five\r\n", header[5]) self.assertEqual("Numbers: Four\r\n", header[6]) self.assertEqual("Numbers: One\r\n", header[7]) self.assertEqual("Numbers: Seven\r\n", header[8]) self.assertEqual("Numbers: Six\r\n", header[9]) self.assertEqual("Numbers: Three\r\n", header[10]) self.assertEqual("Numbers: Two\r\n", header[11]) self.assertEqual("Parting: Ciao\r\n", header[12]) self.assertEqual("Parting: Farewell\r\n", header[13]) self.assertEqual("Parting: Goodbye\r\n", header[14])
def test__str__emptyDictNonEmptyBody(self): "__str__ - String representation of an empty MimeDict with a non-empty body should be that non-empty body with an empty line prepended" someString = """This is some random text so there""" x = MimeDict(__BODY__=someString) self.assertEqual(str(x), '\r\n' + someString)
def test_secondaryInsertion(self): "Insertion of multiple values sequentially into a dictionary results in it remembering the last thing added" x = MimeDict() x["hello"] = "1hello1" x["hello"] = "2hello2" x["hello"] = "3hello3" x["hello"] = "4hello4" self.assertEqual("4hello4", x["hello"])
def test___str__fromString_ManyDifferentHeaders_NoBody(self): "performing __str__/fromString halftrip on a dict with just multiple different simple headers, no body should result in identity" x = MimeDict(Header="Hello World", Heeder="Goodbye Smalltown", Hooder="Bingle") y = MimeDict.fromString(str(x)) self.assertEqual(x, y) self.assert_(x is not y)
def test_basicInsertion_Roundtrip(self): "Insertion into a dictionary, then roundtripped -- fromString(str(x)) results in original value" x = MimeDict() x["hello"] = ["2hello1", "2hello2"] x["__BODY__"] = "Hello\nWorld\n" stringified = str(x) y = MimeDict.fromString(stringified) self.assertEqual(x, y)
def test_EmbeddedNewlineInHeaderRoundtrip_fromInsertion(self): "A header which contains a single carriage return keeps the carriage return embedded since it *isn't* a carriage return/line feed" x = MimeDict() x["header"] = "Hello\nWorld" y = MimeDict.fromString(str(x)) self.assertEqual(y["__BODY__"], "") self.assertEqual(y["header"], x["header"]) self.assertEqual(x["header"], "Hello\nWorld")
def test__str__NonEmptyDict(self): "__str__ - String representation a non empty dict, non-empty body should finish with that body and leading blank line" someString = """This is some random text so there""" x = MimeDict(Hello="World", Fate="Success", __BODY__=someString) self.assertEqual( str(x)[-(4 + len(someString)):], "\r\n\r\n" + someString)
def test_InformationLossRoundtrip(self): "If you put a list with a single string into a MimeDict, and try to send that across the network by itself, it will not be reconstituted as a list. This is because we have no real way of determining that the single value should or should not be a list" x = MimeDict() x["hello"] = ["hello"] x["__BODY__"] = "Hello\nWorld\n" stringified = str(x) y = MimeDict.fromString(stringified) self.assertNotEqual(x, y)
def test_BasicDeletion(self): "Deleting a key value succeeds correctly" x = MimeDict() x["hello"] = "hello" x["__BODY__"] = "Hello\nWorld\n" x["world"] = "world" x["dolly"] = "dolly" del x["world"] y = MimeDict() y["hello"] = "hello" y["__BODY__"] = "Hello\nWorld\n" y["dolly"] = "dolly" str_x = str(x) str_y = str(y) self.assertEqual(x, y) self.assertEqual(str_x, str_y)
def test___str__fromString_MultipleDifferentHeaders_NoBody(self): "performing __str__/fromString halftrip on a dict with multiple header types multiple times, no body should result in identity" x = MimeDict(HeaderA=["value 1", "value 2", "value 3"], HeaderB=["value 4", "value 5", "value 6"]) y = MimeDict.fromString(str(x)) self.assertEqual(y, x) self.assertEqual(y["HeaderA"], ["value 1", "value 2", "value 3"]) self.assertEqual(y["HeaderB"], ["value 4", "value 5", "value 6"]) self.assert_(x is not y)
def test_fromString_ManyHeaderLineNonEmptyBody(self): "fromString - Many header lines followed by an empty line and a body is valid" header = "Header: line\r\nHeeder: line\r\nHooder: line\r\n" body = "This is a random body\r\nWibbleWibble\r\n" x = MimeDict.fromString(header + "\r\n" + body) self.assertEqual( x, MimeDict(Header="line", Heeder="line", Hooder="line", __BODY__=body))
def test__str__TwoListsLengthTwoValues(self): "__str__ - Dict with 2 keys, each with lists of multiple values, both get inserted, possibly mixed up" x = MimeDict(Greeting=["Hello", "Bonjour"], Parting=["Farewell", "Goodbye"]) lines = str(x).splitlines(True) header = lines[:4] header.sort() self.assertEqual("Greeting: Bonjour\r\n", header[0]) self.assertEqual("Greeting: Hello\r\n", header[1]) self.assertEqual("Parting: Farewell\r\n", header[2]) self.assertEqual("Parting: Goodbye\r\n", header[3])
def test__str__SampleNonEmptyDict(self): "__str__ - Dict with multiple headers each with 1 associated simple string value should result in leading Key: Value lines" x = MimeDict(Hello="World", Sheep="Dolly", Marvin="Android", Cat="Garfield") lines = str(x).splitlines(True) self.assertEqual(lines[4], '\r\n') header = lines[:4] header.sort() keys = x.keys() keys.sort() h = 0 for k in keys: if k == "__BODY__": continue self.assertEqual(header[h], "%s: %s\r\n" % (k, x[k])) h += 1
def test__init__New__BODY__NotClobbered(self): "__init__ - Passing over a __BODY__ argument should be stored and not clobbered" x = MimeDict(__BODY__="Hello World") self.assert_("__BODY__" in x) self.assertEqual(x["__BODY__"], "Hello World")
def test_fromString_HeaderLineEmptyBody(self): "fromString - A header line followed by an empty line is valid, has the form 'Header: Key'" header = """Header: line\r\n""" x = MimeDict.fromString(header + "\r\n") self.assertEqual(x, MimeDict(Header="line"))
def test_fromString_NoEmptyLineAndBody(self): "fromString - Random text not preceded by a new line forms a body attribute" randomText = "If the implementation is hard to explain, it's a bad idea." x = MimeDict.fromString(randomText) self.assertEqual(x, MimeDict(__BODY__=randomText))
def test_SingleSpaceFieldRoundTrip(self): "Header with a single space remains a header with a single space" x = MimeDict() x["header"] = " " y = MimeDict.fromString(str(x)) self.assertEqual(x["header"], y["header"])
def test__str__NonEmptyDictEmptyBody(self): "__str__ - String representation a non empty dict with no body should finish with an empty line - last 4 chars should be network EOL" x = MimeDict(Hello="World", Fate="Success") self.assertEqual(str(x)[-4:], "\r\n\r\n")
def test_EmptyFieldRoundTrip(self): "Empty header remains empty" x = MimeDict() x["header"] = "" y = MimeDict.fromString(str(x)) self.assertEqual(x["header"], y["header"])
def test_fromString_ManyHeaderLineEmptyBody(self): "fromString - Many header lines followed by an empty line is valid" header = "Header: line\r\nHeeder: line\r\nHooder: line\r\n" x = MimeDict.fromString(header + "\r\n") self.assertEqual(x, MimeDict(Header="line", Heeder="line", Hooder="line"))
def test___str__fromString_emptyDict(self): "performing __str__ on an empty dict and then fromString should result in empty dict" x = MimeDict() y = MimeDict.fromString(str(x)) self.assertEqual(x, y) self.assert_(x is not y)
def test_SmokeTest_NoArguments(self): "__init__ - Creating an empty mime dict does not raise any exception" x = MimeDict()
def test__str__emptyDict(self): "__str__ - The string representation of an empty MimeDict should be precisely 1 empty line" x = MimeDict() self.assertEqual(str(x), '\r\n')
def test___str__fromString_OnlyBody(self): "performing __str__ on a dict with just a __BODY__ and then fromString should result in the same dict" x = MimeDict(__BODY__="Hello World") y = MimeDict.fromString(str(x)) self.assertEqual(x, y) self.assert_(x is not y)
def test___str__fromString_OneHeader_NoBody(self): "performing __str__/fromString halftrip on a dict with just one header, no body should result in identity" x = MimeDict(Header="Hello World") y = MimeDict.fromString(str(x)) self.assertEqual(x, y) self.assert_(x is not y)
def test___str__fromString_ManySameHeaders_NoBody(self): "performing __str__/fromString halftrip on a dict with single header type multiple times, no body should result in identity" x = MimeDict(Header=["Hello World", "Goodbye Smalltown", "Bingle"]) y = MimeDict.fromString(str(x)) self.assertEqual(x, y) self.assert_(x is not y)
def test_basicInsertion(self): "Insertion into a dictionary succeeds" x = MimeDict() x["hello"] = "hello" self.assertEqual("hello", x["hello"])
def test_SmokeTest_SubClassOfDict(self): "__init__ - MimeDict items are also dictionaries" x = MimeDict() self.assert_(isinstance(x, MimeDict)) self.assert_(isinstance(x, dict))
def test__init__emptyDict_hasBody(self): "__init__ - An empty MimeDict always has a __BODY__ attribute" x = MimeDict() self.assert_("__BODY__" in x)