def test_headers():
    h = Headers(raw=[(b"a", b"123"), (b"a", b"456"), (b"b", b"789")])
    assert "a" in h
    assert "A" in h
    assert "b" in h
    assert "B" in h
    assert "c" not in h
    assert h["a"] == "123"
    assert h.get("a") == "123"
    assert h.get("nope", default=None) is None
    assert h.getlist("a") == ["123", "456"]
    assert h.keys() == ["a", "a", "b"]
    assert h.values() == ["123", "456", "789"]
    assert h.items() == [("a", "123"), ("a", "456"), ("b", "789")]
    assert list(h) == ["a", "a", "b"]
    assert dict(h) == {"a": "123", "b": "789"}
    assert repr(
        h) == "Headers(raw=[(b'a', b'123'), (b'a', b'456'), (b'b', b'789')])"
    assert h == Headers(raw=[(b"a", b"123"), (b"b", b"789"), (b"a", b"456")])
    assert h != [(b"a", b"123"), (b"A", b"456"), (b"b", b"789")]

    h = Headers({"a": "123", "b": "789"})
    assert h["A"] == "123"
    assert h["B"] == "789"
    assert h.raw == [(b"a", b"123"), (b"b", b"789")]
    assert repr(h) == "Headers({'a': '123', 'b': '789'})"
Beispiel #2
0
 async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
     headers = Headers(scope=scope)
     self.should_decode_from_msgpack_to_json = (
         "application/x-msgpack" in headers.get("content-type", "")
     )
     # Take an initial guess, although we eventually may not
     # be able to do the conversion.
     self.should_encode_from_json_to_msgpack = (
         "application/x-msgpack" in headers.getlist("accept")
     )
     self.receive = receive
     self.send = send
     await self.app(scope, self.receive_with_msgpack, self.send_with_msgpack)
    async def gzippable_app(scope: Scope, receive: Receive,
                            send: Send) -> None:
        headers = Headers(scope=scope)

        if "gzip" in headers.getlist("accept-encoding"):
            body = gzip.compress(b"Hello, world!")
            response = PlainTextResponse(
                content=body,
                headers={
                    "Content-Encoding": "gzip",
                    "Content-Length": str(len(body))
                },
            )
        else:
            response = PlainTextResponse("Hello, world!")

        response.headers["Vary"] = "Accept-Encoding"
        await response(scope, receive, send)
Beispiel #4
0
async def get_data_from_response(message: dict, config: Config, event_type: str) -> dict:
    """Loads data from response for APM capturing.

    Args:
        message (dict)
        config (Config)
        event_type (str)

    Returns:
        dict
    """
    result = {}

    if "status_code" in message:
        result["status_code"] = message["status"]

    if config.capture_headers and "headers" in message:
        headers = Headers(raw=message["headers"])
        result["headers"] = {key: ";".join(headers.getlist(key)) for key in compat.iterkeys(headers)}

    return result