PageRenderTime 142ms CodeModel.GetById 60ms app.highlight 6ms RepoModel.GetById 75ms app.codeStats 0ms

/test_apiary/codec.py

https://bitbucket.org/lindenlab/apiary/
Python | 80 lines | 43 code | 13 blank | 24 comment | 8 complexity | e2559b0234a62ddf31b9cf46557ed7a4 MD5 | raw file
 1#
 2# $LicenseInfo:firstyear=2010&license=mit$
 3# 
 4# Copyright (c) 2010, Linden Research, Inc.
 5# 
 6# Permission is hereby granted, free of charge, to any person obtaining a copy
 7# of this software and associated documentation files (the "Software"), to deal
 8# in the Software without restriction, including without limitation the rights
 9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10# copies of the Software, and to permit persons to whom the Software is
11# furnished to do so, subject to the following conditions:
12# 
13# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
15# 
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22# THE SOFTWARE.
23# $/LicenseInfo$
24#
25
26import unittest
27from cStringIO import StringIO
28from apiary.tools.codec import Message, FormatError
29    
30
31class CodecTests (unittest.TestCase):
32    def testCodecSymmetry(self):
33        for body, hdrs in PositiveTestVectors:
34            msgin = Message(body, **hdrs)
35            msgout = Message.decode_from_string(msgin.encode_to_string())
36            self.failUnlessMessagesEqual(msgin, msgout)
37            
38    def testStreamSymmetry(self):
39        msgs = []
40        
41        f = StringIO()
42        for body, hdrs in PositiveTestVectors:
43            msgin = Message(body, **hdrs)
44            msgin.encode_to_file(f)
45            msgs.append(msgin)
46
47        f.seek(0)
48
49        for msgin in msgs:
50            msgout = Message.decode_from_file(f)
51            self.failUnlessMessagesEqual(msgin, msgout)
52            
53    def testNegativeEncoding(self):
54        for body, hdrs in NegativeTestVectors:
55            try:
56                Message(body, **hdrs).encode_to_string()
57            except FormatError:
58                continue # This negative test passes.
59            self.fail('Constructed an invalid message: body %r; headers %r' % (body, hdrs))
60                
61    def failUnlessMessagesEqual(self, a, b):
62        self.failUnlessEqual(a.headers, b.headers)
63        self.failUnlessEqual(a.body, b.body)
64            
65        
66
67
68PositiveTestVectors = [
69    ('', {}),
70    ('foo', {'fruit': 'banana'}),
71    ('x' * (2**20), {'description': 'A mebibyte of xs.',
72                     'reason': 'Pure OSSMness.'}),
73    ]
74
75
76NegativeTestVectors = [
77    ('', {'body_length': 7}),
78    ('', {'body_length': -1}),
79    ('bad header type', {'bad_header': object}),
80    ]