Beispiel #1
0
 def test_encode_complex(self):
     od = collections.OrderedDict()
     od['KeyA'] = ['listitemA', {'k': 'v'}, 3]
     od['KeyB'] = {'k': 'v'}
     od['KeyC'] = 3
     od['KeyD'] = 'AString'
     expected_result = b'd4:KeyAl9:listitemAd1:k1:vei3ee4:KeyBd1:k1:ve4:KeyCi3e4:KeyD7:AStringe'
     coded = bhencode.encode(od)
     self.assertEqual(coded, expected_result, msg='Failed to encode complex object.')
     pass
Beispiel #2
0
 def test_exception_when_strict(self):
     invalid_obj = None
     with self.assertRaises(bhencode.EncodingError):
         bhencode.encode(invalid_obj, strict=True)
Beispiel #3
0
 def test_encode_tuple(self):
     t = ('a', 'b', 3)
     coded = bhencode.encode(t)
     self.assertEqual(coded, b'l1:a1:bi3ee', msg='Failed to encode list from tuple.')
Beispiel #4
0
 def test_encode_dict(self):
     od = collections.OrderedDict()
     od['ka'] = 'va'
     od['kb'] = 2
     coded = bhencode.encode(od)
     self.assertEqual(coded, b'd2:ka2:va2:kbi2ee', msg='Failed to encode dictionary from dict.')
Beispiel #5
0
 def test_encode_list(self):
     l = ['a', 'b', 3]
     coded = bhencode.encode(l)
     self.assertEqual(coded, b'l1:a1:bi3ee', msg='Failed to encode list from list.')
Beispiel #6
0
 def test_encode_bytes(self):
     pass
     b = b"TheseAreSomeBytes"
     coded = bhencode.encode(b)
     l = bytes(str(len(b)), 'utf-8')
     self.assertEqual(coded, l + b':' + b, msg='Failed to encode string from bytes.')
Beispiel #7
0
 def test_encode_int(self):
     coded = bhencode.encode(42)
     self.assertEqual(coded, b'i42e', msg='Failed to encode integer from int.')
Beispiel #8
0
 def test_encode_str(self):
     coded = bhencode.encode('ThisIsAString')
     self.assertEqual(coded, b'13:ThisIsAString', msg='Failed to encode string from str.')
Beispiel #9
0
 def test_no_exception_when_not_strict(self):
     invalid_obj = None
     bhencode.encode(invalid_obj, strict=False)
     self.assert_(True)