Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test_adapters.py #457

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 24 additions & 30 deletions tests/test_adapters.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
# -*- coding: utf-8 -*-

import ssl
import sure # noqa
import urllib3
import pytest

from cfscrape import CloudflareAdapter


class TestCloudflareAdapter:
@pytest.fixture
def adapter():
adapter = CloudflareAdapter()
yield adapter
adapter.close()

def test_create_adapter(self):
adapter = CloudflareAdapter()
adapter.should.be.a("requests.adapters.HTTPAdapter")
adapter.close()

def test_get_connection(self):
adapter = CloudflareAdapter()
def test_create_adapter(adapter):
assert isinstance(adapter, urllib3.HTTPAdapter)

conn = adapter.get_connection("https://127.0.0.1", None)

conn.conn_kw.should.be.a("dict")
conn.conn_kw.should.have.key("ssl_context")
ssl_context = conn.conn_kw["ssl_context"]
def test_get_connection(adapter):
conn = adapter.get_connection("https://127.0.0.1", None)

# This should be ssl.SSLContext unless pyOpenSSL is installed.
# If pyOpenSSL is injected into urllib3, this should still work.
try:
assert isinstance(ssl_context, urllib3.contrib.pyopenssl.PyOpenSSLContext)
except BaseException:
assert isinstance(ssl_context, ssl.SSLContext)
assert isinstance(conn.conn_kw, dict)
assert "ssl_context" in conn.conn_kw
ssl_context = conn.conn_kw["ssl_context"]

adapter.close()
# This should be ssl.SSLContext unless pyOpenSSL is installed.
# If pyOpenSSL is injected into urllib3, this should still work.
assert isinstance(ssl_context, (ssl.SSLContext, urllib3.contrib.pyopenssl.PyOpenSSLContext))

def test_set_ciphers(self):
adapter = CloudflareAdapter()

# Reinitialize the pool manager with a different context
ctx = ssl.create_default_context()
adapter.init_poolmanager(1, 1, ssl_context=ctx)
# Check to see if the context remains the same without error
conn = adapter.get_connection('https://127.0.0.1', None)
conn.conn_kw.should.be.a("dict")
assert conn.conn_kw["ssl_context"] is ctx
def test_set_ciphers(adapter):
# Reinitialize the pool manager with a different context
ctx = ssl.create_default_context()
adapter.init_poolmanager(1, 1, ssl_context=ctx)

adapter.close()
# Check to see if the context remains the same without error
conn = adapter.get_connection('https://127.0.0.1', None)
assert isinstance(conn.conn_kw, dict)
assert conn.conn_kw["ssl_context"] is ctx