1 #!/usr/bin/env python 2 # 3 # Copyright 2012, Google Inc. 4 # All rights reserved. 5 # 6 # Redistribution and use in source and binary forms, with or without 7 # modification, are permitted provided that the following conditions are 8 # met: 9 # 10 # * Redistributions of source code must retain the above copyright 11 # notice, this list of conditions and the following disclaimer. 12 # * Redistributions in binary form must reproduce the above 13 # copyright notice, this list of conditions and the following disclaimer 14 # in the documentation and/or other materials provided with the 15 # distribution. 16 # * Neither the name of Google Inc. nor the names of its 17 # contributors may be used to endorse or promote products derived from 18 # this software without specific prior written permission. 19 # 20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32 33 """Tests for extensions module.""" 34 35 36 import unittest 37 38 import set_sys_path # Update sys.path to locate mod_pywebsocket module. 39 40 from mod_pywebsocket import common 41 from mod_pywebsocket import extensions 42 43 44 class PerFrameCompressionExtensionTest(unittest.TestCase): 45 """A unittest for the perframe-compression extension.""" 46 47 def test_parse_method_simple(self): 48 method_list = extensions._parse_compression_method('foo') 49 self.assertEqual(1, len(method_list)) 50 method = method_list[0] 51 self.assertEqual('foo', method.name()) 52 self.assertEqual(0, len(method.get_parameters())) 53 54 def test_parse_method_with_parameter(self): 55 method_list = extensions._parse_compression_method('foo; x; y=10') 56 self.assertEqual(1, len(method_list)) 57 method = method_list[0] 58 self.assertEqual('foo', method.name()) 59 self.assertEqual(2, len(method.get_parameters())) 60 self.assertTrue(method.has_parameter('x')) 61 self.assertEqual(None, method.get_parameter_value('x')) 62 self.assertTrue(method.has_parameter('y')) 63 self.assertEqual('10', method.get_parameter_value('y')) 64 65 def test_parse_method_with_quoted_parameter(self): 66 method_list = extensions._parse_compression_method( 67 'foo; x="Hello World"; y=10') 68 self.assertEqual(1, len(method_list)) 69 method = method_list[0] 70 self.assertEqual('foo', method.name()) 71 self.assertEqual(2, len(method.get_parameters())) 72 self.assertTrue(method.has_parameter('x')) 73 self.assertEqual('Hello World', method.get_parameter_value('x')) 74 self.assertTrue(method.has_parameter('y')) 75 self.assertEqual('10', method.get_parameter_value('y')) 76 77 def test_parse_method_multiple(self): 78 method_list = extensions._parse_compression_method('foo, bar') 79 self.assertEqual(2, len(method_list)) 80 self.assertEqual('foo', method_list[0].name()) 81 self.assertEqual(0, len(method_list[0].get_parameters())) 82 self.assertEqual('bar', method_list[1].name()) 83 self.assertEqual(0, len(method_list[1].get_parameters())) 84 85 def test_parse_method_multiple_methods_with_quoted_parameter(self): 86 method_list = extensions._parse_compression_method( 87 'foo; x="Hello World", bar; y=10') 88 self.assertEqual(2, len(method_list)) 89 self.assertEqual('foo', method_list[0].name()) 90 self.assertEqual(1, len(method_list[0].get_parameters())) 91 self.assertTrue(method_list[0].has_parameter('x')) 92 self.assertEqual('Hello World', 93 method_list[0].get_parameter_value('x')) 94 self.assertEqual('bar', method_list[1].name()) 95 self.assertEqual(1, len(method_list[1].get_parameters())) 96 self.assertTrue(method_list[1].has_parameter('y')) 97 self.assertEqual('10', method_list[1].get_parameter_value('y')) 98 99 def test_create_method_desc_simple(self): 100 params = common.ExtensionParameter('foo') 101 desc = extensions._create_accepted_method_desc('foo', 102 params.get_parameters()) 103 self.assertEqual('foo', desc) 104 105 def test_create_method_desc_with_parameters(self): 106 params = common.ExtensionParameter('foo') 107 params.add_parameter('x', 'Hello, World') 108 params.add_parameter('y', '10') 109 desc = extensions._create_accepted_method_desc('foo', 110 params.get_parameters()) 111 self.assertEqual('foo; x="Hello, World"; y=10', desc) 112 113 114 if __name__ == '__main__': 115 unittest.main() 116 117 118 # vi:sts=4 sw=4 et 119