Skip to content

Commit

Permalink
Fix test_http.request_callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelange committed Jul 24, 2024
1 parent 5a82613 commit 5671d0e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 58 deletions.
89 changes: 45 additions & 44 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
- uses: actions/checkout@v2

- name: Setup up Python 3.11
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: "3.11"

Expand All @@ -26,21 +26,21 @@ jobs:
strategy:
matrix:
include:
- {python: '3.8', os: ubuntu-20.04}
- {python: '3.9', os: ubuntu-20.04}
- {python: '3.10', os: ubuntu-20.04}
- {python: '3.11', os: ubuntu-20.04}
- {python: '3.12', os: ubuntu-20.04}

- {python: '3.8', os: windows-2019}
- {python: '3.9', os: windows-2019}
- {python: '3.10', os: windows-2019}
- {python: '3.11', os: windows-2019}
- {python: '3.12', os: windows-2019}
- {python-version: '3.8', os: ubuntu-20.04}
- {python-version: '3.9', os: ubuntu-20.04}
- {python-version: '3.10', os: ubuntu-20.04}
- {python-version: '3.11', os: ubuntu-20.04}
- {python-version: '3.12', os: ubuntu-20.04}

- {python-version: '3.8', os: windows-2019}
- {python-version: '3.9', os: windows-2019}
- {python-version: '3.10', os: windows-2019}
- {python-version: '3.11', os: windows-2019}
- {python-version: '3.12', os: windows-2019}
steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand Down Expand Up @@ -71,25 +71,25 @@ jobs:
strategy:
matrix:
include:
- {python: '3.8', os: ubuntu-20.04}
- {python: '3.9', os: ubuntu-20.04}
- {python: '3.10', os: ubuntu-20.04}
- {python: '3.11', os: ubuntu-20.04}
- {python: '3.12', os: ubuntu-20.04}
- {python-version: '3.8', os: ubuntu-20.04}
- {python-version: '3.9', os: ubuntu-20.04}
- {python-version: '3.10', os: ubuntu-20.04}
- {python-version: '3.11', os: ubuntu-20.04}
- {python-version: '3.12', os: ubuntu-20.04}

#
# Some of the doctests don't pass on Windows because of Windows-specific
# character encoding issues.
#
# - {python: '3.7', os: windows-2019}
# - {python: '3.8', os: windows-2019}
# - {python: '3.9', os: windows-2019}
# - {python: '3.10', os: windows-2019}
# - {pythonversion: '3.7', os: windows-2019}
# - {pythonversion: '3.8', os: windows-2019}
# - {pythonversion: '3.9', os: windows-2019}
# - {pythonversion: '3.10', os: windows-2019}

steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -114,23 +114,23 @@ jobs:
strategy:
matrix:
include:
- {python: '3.8', os: ubuntu-20.04}
- {python: '3.9', os: ubuntu-20.04}
- {python: '3.10', os: ubuntu-20.04}
- {python: '3.11', os: ubuntu-20.04}
- {python: '3.12', os: ubuntu-20.04}
- {python-version: '3.8', os: ubuntu-20.04}
- {python-version: '3.9', os: ubuntu-20.04}
- {python-version: '3.10', os: ubuntu-20.04}
- {python-version: '3.11', os: ubuntu-20.04}
- {python-version: '3.12', os: ubuntu-20.04}

# Not sure why we exclude these, perhaps for historical reasons?
#
# - {python: '3.7', os: windows-2019}
# - {python: '3.8', os: windows-2019}
# - {python: '3.9', os: windows-2019}
# - {python: '3.10', os: windows-2019}
# - {python-version: '3.7', os: windows-2019}
# - {python-version: '3.8', os: windows-2019}
# - {python-version: '3.9', os: windows-2019}
# - {python-version: '3.10', os: windows-2019}

steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -148,7 +148,8 @@ jobs:
- run: bash ci_helpers/helpers.sh enable_moto_server
if: ${{ matrix.moto_server }}

- run: |
- name: Start vsftpd
run: |
sudo apt-get install vsftpd
sudo bash ci_helpers/helpers.sh create_ftp_ftps_servers
Expand All @@ -169,21 +170,21 @@ jobs:
strategy:
matrix:
include:
- {python: '3.8', os: ubuntu-20.04}
- {python: '3.9', os: ubuntu-20.04}
- {python: '3.10', os: ubuntu-20.04}
- {python: '3.11', os: ubuntu-20.04}
- {python: '3.12', os: ubuntu-20.04}
- {python-version: '3.8', os: ubuntu-20.04}
- {python-version: '3.9', os: ubuntu-20.04}
- {python-version: '3.10', os: ubuntu-20.04}
- {python-version: '3.11', os: ubuntu-20.04}
- {python-version: '3.12', os: ubuntu-20.04}

# - {python: '3.7', os: windows-2019}
# - {python: '3.8', os: windows-2019}
# - {python: '3.9', os: windows-2019}
# - {python: '3.10', os: windows-2019}
# - {python-version: '3.7', os: windows-2019}
# - {python-version: '3.8', os: windows-2019}
# - {python-version: '3.9', os: windows-2019}
# - {python-version: '3.10', os: windows-2019}

steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand Down
4 changes: 4 additions & 0 deletions ci_helpers/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ create_ftp_ftps_servers(){
mkdir $home_dir
useradd -p $(echo $pass | openssl passwd -1 -stdin) -d $home_dir $user
chown $user:$user $home_dir
openssl req -x509 -nodes -new -sha256 -days 10240 -newkey rsa:2048 -keyout /etc/vsftpd.key -out /etc/vsftpd.pem -subj "/C=ZA/CN=localhost"
chmod 755 /etc/vsftpd.key
chmod 755 /etc/vsftpd.pem

server_setup='''
listen=YES
Expand All @@ -32,6 +35,7 @@ chroot_local_user=YES
allow_writeable_chroot=YES'''

additional_ssl_setup='''
rsa_cert_file=/etc/vsftpd.pem
ssl_enable=YES
allow_anon_ssl=NO
force_local_data_ssl=NO
Expand Down
7 changes: 6 additions & 1 deletion integration-tests/test_ftp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from __future__ import unicode_literals
import pytest
from smart_open import open
import ssl
from functools import partial

# localhost has self-signed cert, see ci_helpers/helpers.sh:create_ftp_ftps_servers
ssl.create_default_context = partial(ssl.create_default_context, cafile="/etc/vsftpd.pem")


@pytest.fixture(params=[("ftp", 21), ("ftps", 90)])
Expand Down Expand Up @@ -81,4 +86,4 @@ def test_line_endings_binary(server_info):

with open(f"{server_type}://user:123@localhost:{port_num}/file4", "rb") as f:
for line in f:
assert B_CLRF in line
assert B_CLRF in line
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def read(fname):
tests_require = all_deps + [
'moto[server]',
'responses',
'boto3',
'pytest',
'pytest-rerunfailures',
]
Expand Down
2 changes: 1 addition & 1 deletion smart_open/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def convert_transport_params_to_args(transport_params):
def _connect(hostname, username, port, password, secure_connection, transport_params):
kwargs = convert_transport_params_to_args(transport_params)
if secure_connection:
ssl_context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
ssl_context = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH)
ftp = FTP_TLS(context=ssl_context, **kwargs)
else:
ftp = FTP(**kwargs)
Expand Down
20 changes: 9 additions & 11 deletions smart_open/tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,22 @@
URL = 'http://localhost'
HTTPS_URL = 'https://localhost'
HEADERS = {
'Content-Length': str(len(BYTES)),
'Accept-Ranges': 'bytes',
}


def request_callback(request, headers=HEADERS, data=BYTES):
try:
range_string = request.headers['range']
except KeyError:
return (200, headers, data)
headers = headers.copy()
range_string = request.headers.get('range', 'bytes=0-')

start, end = range_string.replace('bytes=', '').split('-', 1)
start, end = range_string.replace('bytes=', '', 1).split('-', 1)
start = int(start)
if end:
end = int(end)
else:
end = len(data)
return (200, headers, data[start:end])
end = int(end) if end else len(data)

data = data[start:end]
headers['Content-Length'] = str(len(data))

return (200, headers, data)


@unittest.skipIf(os.environ.get('TRAVIS'), 'This test does not work on TravisCI for some reason')
Expand Down

0 comments on commit 5671d0e

Please sign in to comment.