Skip to content

gh-131178: Add tests for http.server command-line interface #132540

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

Open
wants to merge 63 commits into
base: main
Choose a base branch
from

Conversation

ggqlq
Copy link

@ggqlq ggqlq commented Apr 15, 2025

@python-cla-bot
Copy link

python-cla-bot bot commented Apr 15, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Apr 15, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app
Copy link

bedevere-app bot commented Apr 15, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@ggqlq ggqlq changed the title Add tests for http.server command-line interface gh-131178: Add tests for http.server command-line interface Apr 15, 2025
@ggqlq ggqlq requested a review from donBarbos April 16, 2025 08:32
Copy link
Contributor

@donBarbos donBarbos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't read everything carefully yet, but this is what catches my eye first

ggqlq and others added 9 commits April 16, 2025 21:31
Co-authored-by: Semyon Moroz <[email protected]>
Co-authored-by: Semyon Moroz <[email protected]>
Co-authored-by: Semyon Moroz <[email protected]>
Co-authored-by: Semyon Moroz <[email protected]>
Co-authored-by: Semyon Moroz <[email protected]>
Co-authored-by: Semyon Moroz <[email protected]>
Co-authored-by: Semyon Moroz <[email protected]>
@ggqlq ggqlq requested a review from picnixz May 13, 2025 07:58
@ggqlq
Copy link
Author

ggqlq commented May 13, 2025

Should we remove the test_cgi_flag function? The --cgi flag will be removed in version 3.15 according to the documentation.

@donBarbos
Copy link
Contributor

Should we remove the test_cgi_flag function? The --cgi flag will be removed in version 3.15 according to the documentation.

I don't think we should remove it until PR #133811 is merged.

@picnixz
Copy link
Member

picnixz commented May 15, 2025

You can remove them. I don't think it's worth for a test to stay if I'm going to remove it just after. However, you shouldn't remove it from the runtime (namely, don't change the current behavior, you just don't need to bother with creating the tests). This will be part of my PR.

We didn't backport the additional tests, so in 3.15, there shouldn't be code using CGI.

ggqlq and others added 6 commits May 16, 2025 16:47
Comment on lines +1778 to +1787
proc = spawn_python('-u', '-m', 'http.server', str(port), '-b', bind,
'--tls-cert', self.tls_cert,
'--tls-key', self.tls_key,
'--tls-password-file', self.tls_password_file,
bufsize=1, text=True)
self.assertTrue(self.wait_for_server(proc, 'https', port, bind))
res = self.fetch_file(f'https://{bind}:{port}/{self.random_file_name}')
self.assertEqual(res, self.random_data)
proc.terminate()
kill_python(proc)
Copy link
Member

@picnixz picnixz May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
proc = spawn_python('-u', '-m', 'http.server', str(port), '-b', bind,
'--tls-cert', self.tls_cert,
'--tls-key', self.tls_key,
'--tls-password-file', self.tls_password_file,
bufsize=1, text=True)
self.assertTrue(self.wait_for_server(proc, 'https', port, bind))
res = self.fetch_file(f'https://{bind}:{port}/{self.random_file_name}')
self.assertEqual(res, self.random_data)
proc.terminate()
kill_python(proc)
proc = spawn_python('-u', '-m', 'http.server', str(port), '-b', bind,
'--tls-cert', self.tls_cert,
'--tls-key', self.tls_key,
'--tls-password-file', self.tls_password_file,
bufsize=1, text=True)
self.addCleanup(proc.terminate)
self.addCleanup(kill_python, proc)
self.assertTrue(self.wait_for_server(proc, 'https', port, bind))
res = self.fetch_file(f'https://{bind}:{port}/{self.random_file_name}')
self.assertEqual(res, self.random_data)

Hopefully, it will first call proc.terminate before calling kill_python(proc).

res = self.fetch_file(f'http://{bind}:{port}/{self.random_file_name}')
self.assertEqual(res, self.random_data)
proc.terminate()
kill_python(proc)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Comment on lines +1700 to +1708
with self.assertRaises(SystemExit):
_ = self.invoke_httpd(option)

@mock.patch('http.server.test')
def test_unknown_flag(self, _):
with self.assertRaises(SystemExit):
_ = self.invoke_httpd('--unknown-flag')

class CommandLineRunTimeTestCase(unittest.TestCase):
Copy link
Member

@picnixz picnixz May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's capture the output outside invoke_httpd as well:

def invoke_httpd(self, *args, stdout=None, stderr=None):
    stdout = StringIO() if stdout is None else stdout
    stderr = StringIO() if stderr is None else stderr
    with contextlib.redirect_stdout(stdout), \
        contextlib.redirect_stderr(stderr):
        server._main(args)
    return {'stdout': stdout.getvalue(), 'stderr': stderr.getvalue()}

Then do:

stdout, stderr = StringIO(), StringIO()
with self.assertRaises(SystemExit):
    _ = self.invoke_httpd('--unknown-flag', stdout=stdout, stderr=stderr)
self.assertEqual(stdout.getvalue(), "")
self.assertIn("error: ", stderr.getvalue())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants