Exemplo n.º 1
0
    def test_str(self):
        headers = semantics.Headers(Host="example.com")
        assert str(headers) == "Host: example.com\r\n"

        headers = semantics.Headers([["Host", "example.com"],
                                     ["Accept", "text/plain"]])
        assert str(headers) == "Host: example.com\r\nAccept: text/plain\r\n"
Exemplo n.º 2
0
    def test_eq_ne(self):
        headers1 = semantics.Headers(Host="example.com")
        headers2 = semantics.Headers(host="example.com")
        assert not (headers1 == headers2)
        assert headers1 != headers2

        headers1 = semantics.Headers(Host="example.com")
        headers2 = semantics.Headers(Host="example.com")
        assert headers1 == headers2
        assert not (headers1 != headers2)

        assert headers1 != 42
Exemplo n.º 3
0
    def test_keys(self):
        headers = semantics.Headers(Host="example.com")
        assert len(headers.keys()) == 1
        assert headers.keys()[0] == "Host"

        headers = self._2host()
        assert len(headers.keys()) == 1
        assert headers.keys()[0] == "Host"
Exemplo n.º 4
0
    def test_getitem(self):
        headers = semantics.Headers(Host="example.com")
        assert headers["Host"] == "example.com"
        assert headers["host"] == "example.com"
        tutils.raises(KeyError, headers.__getitem__, "Accept")

        headers = self._2host()
        assert headers["Host"] == "example.com, example.org"
Exemplo n.º 5
0
    def test_state(self):
        headers = self._2host()
        assert len(headers.get_state()) == 2
        assert headers == semantics.Headers.from_state(headers.get_state())

        headers2 = semantics.Headers()
        assert headers != headers2
        headers2.load_state(headers.get_state())
        assert headers == headers2
Exemplo n.º 6
0
    def test_set_all(self):
        headers = semantics.Headers(Host="example.com")
        headers.set_all("Accept", ["text/plain"])
        assert len(headers) == 2
        assert "accept" in headers

        headers = self._2host()
        headers.set_all("Host", ["example.org"])
        assert headers["host"] == "example.org"

        headers.set_all("Host", ["example.org", "example.net"])
        assert headers["host"] == "example.org, example.net"
Exemplo n.º 7
0
    def test_init(self):
        headers = semantics.Headers()
        assert len(headers) == 0

        headers = semantics.Headers([["Host", "example.com"]])
        assert len(headers) == 1
        assert headers["Host"] == "example.com"

        headers = semantics.Headers(Host="example.com")
        assert len(headers) == 1
        assert headers["Host"] == "example.com"

        headers = semantics.Headers([["Host", "invalid"]], Host="example.com")
        assert len(headers) == 1
        assert headers["Host"] == "example.com"

        headers = semantics.Headers(
            [["Host", "invalid"], ["Accept", "text/plain"]],
            Host="example.com")
        assert len(headers) == 2
        assert headers["Host"] == "example.com"
        assert headers["Accept"] == "text/plain"
Exemplo n.º 8
0
    def test_delitem(self):
        headers = semantics.Headers(Host="example.com")
        assert len(headers) == 1
        del headers["host"]
        assert len(headers) == 0
        try:
            del headers["host"]
        except KeyError:
            assert True
        else:
            assert False

        headers = self._2host()
        del headers["Host"]
        assert len(headers) == 0
Exemplo n.º 9
0
    def test_setitem(self):
        headers = semantics.Headers()
        headers["Host"] = "example.com"
        assert "Host" in headers
        assert "host" in headers
        assert headers["Host"] == "example.com"

        headers["host"] = "example.org"
        assert "Host" in headers
        assert "host" in headers
        assert headers["Host"] == "example.org"

        headers["accept"] = "text/plain"
        assert len(headers) == 2
        assert "Accept" in headers
        assert "Host" in headers

        headers = self._2host()
        assert len(headers.fields) == 2
        headers["Host"] = "example.com"
        assert len(headers.fields) == 1
        assert "Host" in headers
Exemplo n.º 10
0
 def _2host(self):
     return semantics.Headers([["Host", "example.com"],
                               ["host", "example.org"]])