Skip to content

gh-135056: Add a --cors CLI argument to http.server #135057

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 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
gh-135056: Add test for http.server cli --header argument
  • Loading branch information
aisipos committed Jul 10, 2025
commit 6f88c13a4338dd33827e7cd929e7744ef63d3aca
3 changes: 2 additions & 1 deletion Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,7 @@ def test(HandlerClass=BaseHTTPRequestHandler,
except KeyboardInterrupt:
print("\nKeyboard interrupt received, exiting.")
sys.exit(0)
return server


def _main(args=None):
Expand Down Expand Up @@ -1081,7 +1082,7 @@ class HTTPSDualStackServer(DualStackServerMixin, ThreadingHTTPSServer):

ServerClass = HTTPSDualStackServer if args.tls_cert else HTTPDualStackServer

test(
return test(
HandlerClass=SimpleHTTPRequestHandler,
ServerClass=ServerClass,
port=args.port,
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,34 @@ def test_unknown_flag(self, _):
self.assertEqual(stdout.getvalue(), '')
self.assertIn('error', stderr.getvalue())

def test_response_headers_arg(self):
# with mock.patch.object(
# SimpleHTTPRequestHandler, '__init__'
# ) as mock_handler, \
# mock.patch.object(
# HTTPServer, 'serve_forever'
# ) as mock_serve_forever:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# with mock.patch.object(
# SimpleHTTPRequestHandler, '__init__'
# ) as mock_handler, \
# mock.patch.object(
# HTTPServer, 'serve_forever'
# ) as mock_serve_forever:

Copy link
Author

@aisipos aisipos Jul 15, 2025

Choose a reason for hiding this comment

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

Fixed in 9450b86. Sorry to mistakenly leave these in my earlier checkins.

with mock.patch.object(
HTTPServer, 'serve_forever'
) as mock_serve_forever:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
with mock.patch.object(
HTTPServer, 'serve_forever'
) as mock_serve_forever:
with mock.patch.object(HTTPServer, 'serve_forever'):

Copy link
Author

Choose a reason for hiding this comment

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

Fixed in 9450b86

httpd = server._main(
['-H', 'X-Test1', 'Test1', '-H', 'X-Test2', 'Test2', '8080']
)
request_handler_class = httpd.RequestHandlerClass
with mock.patch.object(
request_handler_class, '__init__'
) as mock_handler_init:
mock_handler_init.return_value = None
# finish_request instantiates a request handler class,
# ensure response_headers are passed to it
httpd.finish_request(mock.Mock(), '127.0.0.1')
mock_handler_init.assert_called_once_with(
mock.ANY, mock.ANY, mock.ANY, directory=mock.ANY,
response_headers={
'X-Test1': 'Test1', 'X-Test2': 'Test2'
}
)


class CommandLineRunTimeTestCase(unittest.TestCase):
served_data = os.urandom(32)
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy