How to use the httpx.Request function in httpx

To help you get started, weā€™ve selected a few httpx examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github encode / httpx / tests / models / test_requests.py View on Github external
def test_override_host_header():
    headers = {"host": "1.2.3.4:80"}

    request = httpx.Request("GET", "http://example.org", headers=headers)
    assert request.headers["Host"] == "1.2.3.4:80"
github encode / httpx / tests / models / test_requests.py View on Github external
def test_invalid_urls():
    with pytest.raises(httpx.InvalidURL):
        httpx.Request("GET", "example.org")

    with pytest.raises(httpx.InvalidURL):
        httpx.Request("GET", "http:///foo")
github encode / httpx / tests / dispatch / utils.py View on Github external
if not self.requests[stream_id]:
            del self.requests[stream_id]

        headers_dict = dict(request["headers"])

        method = headers_dict[b":method"].decode("ascii")
        url = "%s://%s%s" % (
            headers_dict[b":scheme"].decode("ascii"),
            headers_dict[b":authority"].decode("ascii"),
            headers_dict[b":path"].decode("ascii"),
        )
        headers = [(k, v) for k, v in request["headers"] if not k.startswith(b":")]
        data = request["data"]

        # Call out to the app.
        request = Request(method, url, headers=headers, data=data)
        response = self.app(request)

        # Write the response to the buffer.
        status_code_bytes = str(response.status_code).encode("ascii")
        response_headers = [(b":status", status_code_bytes)] + response.headers.raw

        self.conn.send_headers(stream_id, response_headers)
        self.buffer += self.conn.data_to_send()
        self.return_data[stream_id] = response.content
        self.send_return_data(stream_id)
github encode / httpx / tests / models / test_requests.py View on Github external
async def test_url_encoded_data():
    request = httpx.Request("POST", "http://example.org", data={"test": "123"})
    assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"
    assert await request.content.aread() == b"test=123"
github encode / httpx / tests / models / test_requests.py View on Github external
def test_override_content_length_header():
    async def streaming_body(data):
        yield data  # pragma: nocover

    data = streaming_body(b"test 123")
    headers = {"Content-Length": "8"}

    request = httpx.Request("POST", "http://example.org", data=data, headers=headers)
    assert request.headers["Content-Length"] == "8"
github encode / httpx / tests / models / test_requests.py View on Github external
def test_no_content():
    request = httpx.Request("GET", "http://example.org")
    assert "Content-Length" not in request.headers
github encode / httpx / tests / test_decoders.py View on Github external
import brotli
import pytest

import httpx
from httpx.content_streams import AsyncIteratorStream
from httpx.decoders import (
    BrotliDecoder,
    DeflateDecoder,
    GZipDecoder,
    IdentityDecoder,
    LineDecoder,
    TextDecoder,
)

REQUEST = httpx.Request("GET", "https://example.org")


def test_deflate():
    body = b"test 123"
    compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)
    compressed_body = compressor.compress(body) + compressor.flush()

    headers = [(b"Content-Encoding", b"deflate")]
    response = httpx.Response(
        200, headers=headers, content=compressed_body, request=REQUEST
    )
    assert response.content == body


def test_gzip():
    body = b"test 123"
github encode / httpx / tests / models / test_responses.py View on Github external
import datetime
import json
from unittest import mock

import pytest

import httpx
from httpx.content_streams import AsyncIteratorStream

REQUEST = httpx.Request("GET", "https://example.org")


def streaming_body():
    yield b"Hello, "
    yield b"world!"


async def async_streaming_body():
    yield b"Hello, "
    yield b"world!"


def test_response():
    response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)
    assert response.status_code == 200
    assert response.reason_phrase == "OK"
github encode / httpx / tests / models / test_requests.py View on Github external
def test_url():
    url = "http://example.org"
    request = httpx.Request("GET", url)
    assert request.url.scheme == "http"
    assert request.url.port == 80
    assert request.url.full_path == "/"

    url = "https://example.org/abc?foo=bar"
    request = httpx.Request("GET", url)
    assert request.url.scheme == "https"
    assert request.url.port == 443
    assert request.url.full_path == "/abc?foo=bar"
github encode / httpx / tests / models / test_requests.py View on Github external
def test_invalid_urls():
    with pytest.raises(httpx.InvalidURL):
        httpx.Request("GET", "example.org")

    with pytest.raises(httpx.InvalidURL):
        httpx.Request("GET", "http:///foo")