test_config.py (5285B)
1 # -*- coding: utf-8 -*- 2 """ 3 test_config 4 ~~~~~~~~~~~ 5 6 Test the configuration object. 7 """ 8 import logging 9 import pytest 10 11 import h2.config 12 13 14 class TestH2Config(object): 15 """ 16 Tests of the H2 config object. 17 """ 18 19 def test_defaults(self): 20 """ 21 The default values of the HTTP/2 config object are sensible. 22 """ 23 config = h2.config.H2Configuration() 24 assert config.client_side 25 assert config.header_encoding is None 26 assert isinstance(config.logger, h2.config.DummyLogger) 27 28 boolean_config_options = [ 29 'client_side', 30 'validate_outbound_headers', 31 'normalize_outbound_headers', 32 'validate_inbound_headers', 33 'normalize_inbound_headers', 34 ] 35 36 @pytest.mark.parametrize('option_name', boolean_config_options) 37 @pytest.mark.parametrize('value', [None, 'False', 1]) 38 def test_boolean_config_options_reject_non_bools_init( 39 self, option_name, value 40 ): 41 """ 42 The boolean config options raise an error if you try to set a value 43 that isn't a boolean via the initializer. 44 """ 45 with pytest.raises(ValueError): 46 h2.config.H2Configuration(**{option_name: value}) 47 48 @pytest.mark.parametrize('option_name', boolean_config_options) 49 @pytest.mark.parametrize('value', [None, 'False', 1]) 50 def test_boolean_config_options_reject_non_bools_attr( 51 self, option_name, value 52 ): 53 """ 54 The boolean config options raise an error if you try to set a value 55 that isn't a boolean via attribute setter. 56 """ 57 config = h2.config.H2Configuration() 58 with pytest.raises(ValueError): 59 setattr(config, option_name, value) 60 61 @pytest.mark.parametrize('option_name', boolean_config_options) 62 @pytest.mark.parametrize('value', [True, False]) 63 def test_boolean_config_option_is_reflected_init(self, option_name, value): 64 """ 65 The value of the boolean config options, when set, is reflected 66 in the value via the initializer. 67 """ 68 config = h2.config.H2Configuration(**{option_name: value}) 69 assert getattr(config, option_name) == value 70 71 @pytest.mark.parametrize('option_name', boolean_config_options) 72 @pytest.mark.parametrize('value', [True, False]) 73 def test_boolean_config_option_is_reflected_attr(self, option_name, value): 74 """ 75 The value of the boolean config options, when set, is reflected 76 in the value via attribute setter. 77 """ 78 config = h2.config.H2Configuration() 79 setattr(config, option_name, value) 80 assert getattr(config, option_name) == value 81 82 @pytest.mark.parametrize('header_encoding', [True, 1, object()]) 83 def test_header_encoding_must_be_false_str_none_init( 84 self, header_encoding 85 ): 86 """ 87 The value of the ``header_encoding`` setting must be False, a string, 88 or None via the initializer. 89 """ 90 with pytest.raises(ValueError): 91 h2.config.H2Configuration(header_encoding=header_encoding) 92 93 @pytest.mark.parametrize('header_encoding', [True, 1, object()]) 94 def test_header_encoding_must_be_false_str_none_attr( 95 self, header_encoding 96 ): 97 """ 98 The value of the ``header_encoding`` setting must be False, a string, 99 or None via attribute setter. 100 """ 101 config = h2.config.H2Configuration() 102 with pytest.raises(ValueError): 103 config.header_encoding = header_encoding 104 105 @pytest.mark.parametrize('header_encoding', [False, 'ascii', None]) 106 def test_header_encoding_is_reflected_init(self, header_encoding): 107 """ 108 The value of ``header_encoding``, when set, is reflected in the value 109 via the initializer. 110 """ 111 config = h2.config.H2Configuration(header_encoding=header_encoding) 112 assert config.header_encoding == header_encoding 113 114 @pytest.mark.parametrize('header_encoding', [False, 'ascii', None]) 115 def test_header_encoding_is_reflected_attr(self, header_encoding): 116 """ 117 The value of ``header_encoding``, when set, is reflected in the value 118 via the attribute setter. 119 """ 120 config = h2.config.H2Configuration() 121 config.header_encoding = header_encoding 122 assert config.header_encoding == header_encoding 123 124 def test_logger_instance_is_reflected(self): 125 """ 126 The value of ``logger``, when set, is reflected in the value. 127 """ 128 logger = logging.Logger('hyper-h2.test') 129 config = h2.config.H2Configuration() 130 config.logger = logger 131 assert config.logger is logger 132 133 @pytest.mark.parametrize("trace_level", [False, True]) 134 def test_output_logger(self, capsys, trace_level): 135 logger = h2.config.OutputLogger(trace_level=trace_level) 136 137 logger.debug("This is a debug message %d.", 123) 138 logger.trace("This is a trace message %d.", 123) 139 captured = capsys.readouterr() 140 assert "h2 (debug): This is a debug message 123.\n" in captured.err 141 if trace_level: 142 assert "h2 (trace): This is a trace message 123.\n" in captured.err 143 else: 144 assert "h2 (trace): This is a trace message 123.\n" not in captured.err