Skip to content

Commit

Permalink
Included create_ssl_context function to create the same context with …
Browse files Browse the repository at this point in the history
…SSLConfig and serve as API.
  • Loading branch information
Can Sarigol authored and can-sarigol committed May 24, 2020
1 parent be1ea5a commit 4bab0af
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
21 changes: 17 additions & 4 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,13 +577,11 @@ import httpx
r = httpx.get("https://example.org", verify="path/to/client.pem")
```

Alternatively, you can pass a standard library `ssl.SSLContext`.
Alternatively, you can use `httpx.create_ssl_context`.

```python
>>> import ssl
>>> import httpx
>>> context = ssl.create_default_context()
>>> context.load_verify_locations(cafile="/tmp/client.pem")
>>> context = httpx.create_ssl_context(verify="/tmp/client.pem")
>>> httpx.get('https://example.org', verify=context)
<Response [200 OK]>
```
Expand Down Expand Up @@ -623,6 +621,21 @@ If you do need to make HTTPS connections to a local server, for example to test
Response <200 OK>
```

### Generate custom SSL Context

Accepts the standard 'verify', 'cert', trust_env' arguments.

```python
>>> import httpx
>>> ssl_context = httpx.create_ssl_context()
```

```python
ssl_context = httpx.create_ssl_context()
transport = httpcore.AsyncConnectionPool(ssl_context=ssl_context)
client = httpx.AsyncClient(transport=transport)
```

## Custom Transports

HTTPX's `Client` also accepts a `transport` argument. This argument allows you
Expand Down
3 changes: 2 additions & 1 deletion httpx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ._api import delete, get, head, options, patch, post, put, request, stream
from ._auth import Auth, BasicAuth, DigestAuth
from ._client import AsyncClient, Client
from ._config import PoolLimits, Proxy, Timeout
from ._config import PoolLimits, Proxy, Timeout, create_ssl_context
from ._exceptions import (
ConnectTimeout,
CookieConflict,
Expand Down Expand Up @@ -54,6 +54,7 @@
"PoolLimits",
"Proxy",
"Timeout",
"create_ssl_context",
"ConnectTimeout",
"CookieConflict",
"DecodingError",
Expand Down
11 changes: 11 additions & 0 deletions httpx/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ class UnsetType:
UNSET = UnsetType()


def create_ssl_context(
cert: CertTypes = None,
verify: VerifyTypes = True,
trust_env: bool = None,
http2: bool = False,
) -> ssl.SSLContext:
return SSLConfig(
cert=cert, verify=verify, trust_env=trust_env, http2=http2
).ssl_context


class SSLConfig:
"""
SSL Configuration.
Expand Down
13 changes: 13 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ def test_ssl_context_cache_for_cert_param():
assert id(ssl1.ssl_context) != id(ssl2.ssl_context) != id(ssl3.ssl_context)


def test_create_ssl_context_with_get_request(server, cert_pem_file):
context = httpx.create_ssl_context(verify=cert_pem_file)
response = httpx.get(server.url, verify=context)
assert response.status_code == 200


def test_create_ssl_context():
ssl_config = SSLConfig()
context = httpx.create_ssl_context()

assert ssl_config.ssl_context == context


def test_limits_repr():
limits = httpx.PoolLimits(max_connections=100)
assert repr(limits) == "PoolLimits(max_keepalive=None, max_connections=100)"
Expand Down

0 comments on commit 4bab0af

Please sign in to comment.