33Written by Cody A.W. Somerville <[email protected] >, 44Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest.
55"""
6-
6+ from collections import OrderedDict
77from http .server import BaseHTTPRequestHandler , HTTPServer , \
88 SimpleHTTPRequestHandler , CGIHTTPRequestHandler
99from http import server , HTTPStatus
1919import email .message
2020import email .utils
2121import html
22- import http .client
22+ import http , http .client
2323import urllib .parse
2424import tempfile
2525import time
@@ -588,6 +588,15 @@ def test_html_escape_filename(self):
588588print(os.environ["%s"])
589589"""
590590
591+ cgi_file6 = """\
592+ #!%s
593+ import os
594+
595+ print("Content-type: text/plain")
596+ print()
597+ print(repr(os.environ))
598+ """
599+
591600
592601@unittest .skipIf (hasattr (os , 'geteuid' ) and os .geteuid () == 0 ,
593602 "This test can't be run reliably as root (issue #13308)." )
@@ -666,6 +675,11 @@ def setUp(self):
666675 file5 .write (cgi_file1 % self .pythonexe )
667676 os .chmod (self .file5_path , 0o777 )
668677
678+ self .file6_path = os .path .join (self .cgi_dir , 'file6.py' )
679+ with open (self .file6_path , 'w' , encoding = 'utf-8' ) as file6 :
680+ file6 .write (cgi_file6 % self .pythonexe )
681+ os .chmod (self .file6_path , 0o777 )
682+
669683 os .chdir (self .parent_dir )
670684
671685 def tearDown (self ):
@@ -685,6 +699,8 @@ def tearDown(self):
685699 os .remove (self .file4_path )
686700 if self .file5_path :
687701 os .remove (self .file5_path )
702+ if self .file6_path :
703+ os .remove (self .file6_path )
688704 os .rmdir (self .cgi_child_dir )
689705 os .rmdir (self .cgi_dir )
690706 os .rmdir (self .cgi_dir_in_sub_dir )
@@ -818,6 +834,23 @@ def test_cgi_path_in_sub_directories(self):
818834 finally :
819835 CGIHTTPRequestHandler .cgi_directories .remove ('/sub/dir/cgi-bin' )
820836
837+ def test_accept (self ):
838+ browser_accept = \
839+ 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
840+ tests = (
841+ ((('Accept' , browser_accept ),), browser_accept ),
842+ ((), '' ),
843+ # Hack case to get two values for the one header
844+ ((('Accept' , 'text/html' ), ('ACCEPT' , 'text/plain' )),
845+ 'text/html,text/plain' ),
846+ )
847+ for headers , expected in tests :
848+ headers = OrderedDict (headers )
849+ with self .subTest (headers ):
850+ res = self .request ('/cgi-bin/file6.py' , 'GET' , headers = headers )
851+ self .assertEqual (http .HTTPStatus .OK , res .status )
852+ expected = f"'HTTP_ACCEPT': { expected !r} "
853+ self .assertIn (expected .encode ('ascii' ), res .read ())
821854
822855
823856class SocketlessRequestHandler (SimpleHTTPRequestHandler ):
0 commit comments