How to use httpcore - 10 common examples

To help you get started, we’ve selected a few httpcore 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 / test_sync.py View on Github external
def test_post(server):
    with httpcore.SyncClient() as http:
        response = http.post("http://127.0.0.1:8000/", data=b"Hello, world!")
    assert response.status_code == 200
    assert response.reason_phrase == "OK"
github encode / httpx / tests / test_sync.py View on Github external
def test_get(server):
    with httpcore.SyncClient() as http:
        response = http.get("http://127.0.0.1:8000/")
    assert response.status_code == 200
    assert response.content == b"Hello, world!"
    assert response.text == "Hello, world!"
github encode / httpx / tests / test_sync.py View on Github external
def test_stream_response(server):
    with httpcore.SyncClient() as http:
        response = http.get("http://127.0.0.1:8000/", stream=True)
    assert response.status_code == 200
    content = response.read()
    assert content == b"Hello, world!"
github encode / httpx / tests / test_sync.py View on Github external
def test_stream_iterator(server):
    with httpcore.SyncClient() as http:
        response = http.get("http://127.0.0.1:8000/", stream=True)
    assert response.status_code == 200
    body = b""
    for chunk in response.stream():
        body += chunk
    assert body == b"Hello, world!"
github encode / httpx / tests / test_sync.py View on Github external
def test_raw_iterator(server):
    with httpcore.SyncClient() as http:
        response = http.get("http://127.0.0.1:8000/", stream=True)
    assert response.status_code == 200
    body = b""
    for chunk in response.raw():
        body += chunk
    assert body == b"Hello, world!"
github siebenmann / dwiki / testbench.py View on Github external
# We're one of the people who actually needs this.
		cfg, ms, webserv, staticstore, cachestore = r

	except derrors.WikiErr as e:
		die("dwiki error: "+str(e))

	env = setup_env(url)
	if options.ip6origin:
		# This format matches what the Fedora 17 lighttpd
		# puts into $REMOTE_ADDR. Your mileage may vary for
		# other web servers, although it's probably right
		# for Apache too.
		env['REMOTE_ADDR'] = "::1"
		
	if rend:
		httpcore.environSetup(env, cfg, ms, webserv, staticstore,
				      cachestore)
		env['dwiki.logger'] = None
		rdata = httpcore.gather_reqdata(env)
		if 'query' not in rdata:
			die("URL %s is not accepted by the core" % url)
		ctx = context.HTMLContext(cfg, ms, webserv, rdata)
		if cachestore:
			ctx.setvar(':_cachestore', cachestore)
		if options.prerun:
			runLimited(*(options.prerun, runrend, rend, ctx))
		op(cnt, runrend, rend, ctx)
	else:
		if options.prerun:
			runLimited(*(options.prerun, runwsgi, env, app))
		op(cnt, runwsgi, env, app)
github siebenmann / dwiki / testbench.py View on Github external
except derrors.WikiErr as e:
		die("dwiki error: "+str(e))

	env = setup_env(url)
	if options.ip6origin:
		# This format matches what the Fedora 17 lighttpd
		# puts into $REMOTE_ADDR. Your mileage may vary for
		# other web servers, although it's probably right
		# for Apache too.
		env['REMOTE_ADDR'] = "::1"
		
	if rend:
		httpcore.environSetup(env, cfg, ms, webserv, staticstore,
				      cachestore)
		env['dwiki.logger'] = None
		rdata = httpcore.gather_reqdata(env)
		if 'query' not in rdata:
			die("URL %s is not accepted by the core" % url)
		ctx = context.HTMLContext(cfg, ms, webserv, rdata)
		if cachestore:
			ctx.setvar(':_cachestore', cachestore)
		if options.prerun:
			runLimited(*(options.prerun, runrend, rend, ctx))
		op(cnt, runrend, rend, ctx)
	else:
		if options.prerun:
			runLimited(*(options.prerun, runwsgi, env, app))
		op(cnt, runwsgi, env, app)
github lundberg / respx / tests / test_api.py View on Github external
side_effect=ConnectionRefusedError("test request blocked"),
        ) as open_connection:
            with pytest.raises(NetworkError):
                await client.get("https://example.org/")

        assert open_connection.called is True
        assert request.called is True
        assert request.pass_through is expected

    with MockTransport() as respx_mock:
        request = respx_mock.add(**parameters)

        with asynctest.mock.patch(
            "socket.socket.connect", side_effect=socket.error("test request blocked"),
        ) as connect:
            with pytest.raises(NetworkError):
                httpx.get("https://example.org/")

        assert connect.called is True
        assert request.called is True
        assert request.pass_through is expected
github lundberg / respx / tests / test_api.py View on Github external
async def test_pass_through(client, parameters, expected):
    async with MockTransport() as respx_mock:
        request = respx_mock.add(**parameters)

        with asynctest.mock.patch(
            "asyncio.open_connection",
            side_effect=ConnectionRefusedError("test request blocked"),
        ) as open_connection:
            with pytest.raises(NetworkError):
                await client.get("https://example.org/")

        assert open_connection.called is True
        assert request.called is True
        assert request.pass_through is expected

    with MockTransport() as respx_mock:
        request = respx_mock.add(**parameters)

        with asynctest.mock.patch(
            "socket.socket.connect", side_effect=socket.error("test request blocked"),
        ) as connect:
            with pytest.raises(NetworkError):
                httpx.get("https://example.org/")

        assert connect.called is True
github lundberg / respx / tests / test_transports.py View on Github external
async def test_httpcore_request():
    async with MockTransport() as transport:
        transport.add("GET", "https://foo.bar/", content="foobar")
        with httpcore.SyncConnectionPool() as http:
            (http_version, status_code, reason_phrase, headers, stream,) = http.request(
                method=b"GET", url=(b"https", b"foo.bar", 443, b"/"),
            )

            body = b"".join([chunk for chunk in stream])
            stream.close()
            assert body == b"foobar"

        async with httpcore.AsyncConnectionPool() as http:
            (
                http_version,
                status_code,
                reason_phrase,
                headers,
                stream,
            ) = await http.request(