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