3
3
Written by Cody A.W. Somerville <[email protected] >,
4
4
Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest.
5
5
"""
6
-
6
+ from collections import OrderedDict
7
7
from http .server import BaseHTTPRequestHandler , HTTPServer , \
8
8
SimpleHTTPRequestHandler , CGIHTTPRequestHandler
9
9
from http import server , HTTPStatus
19
19
import email .message
20
20
import email .utils
21
21
import html
22
- import http .client
22
+ import http , http .client
23
23
import urllib .parse
24
24
import tempfile
25
25
import time
@@ -586,6 +586,15 @@ def test_html_escape_filename(self):
586
586
print(os.environ["%s"])
587
587
"""
588
588
589
+ cgi_file6 = """\
590
+ #!%s
591
+ import os
592
+
593
+ print("Content-type: text/plain")
594
+ print()
595
+ print(repr(os.environ))
596
+ """
597
+
589
598
590
599
@unittest .skipIf (hasattr (os , 'geteuid' ) and os .geteuid () == 0 ,
591
600
"This test can't be run reliably as root (issue #13308)." )
@@ -664,6 +673,11 @@ def setUp(self):
664
673
file5 .write (cgi_file1 % self .pythonexe )
665
674
os .chmod (self .file5_path , 0o777 )
666
675
676
+ self .file6_path = os .path .join (self .cgi_dir , 'file6.py' )
677
+ with open (self .file6_path , 'w' , encoding = 'utf-8' ) as file6 :
678
+ file6 .write (cgi_file6 % self .pythonexe )
679
+ os .chmod (self .file6_path , 0o777 )
680
+
667
681
os .chdir (self .parent_dir )
668
682
669
683
def tearDown (self ):
@@ -683,6 +697,8 @@ def tearDown(self):
683
697
os .remove (self .file4_path )
684
698
if self .file5_path :
685
699
os .remove (self .file5_path )
700
+ if self .file6_path :
701
+ os .remove (self .file6_path )
686
702
os .rmdir (self .cgi_child_dir )
687
703
os .rmdir (self .cgi_dir )
688
704
os .rmdir (self .cgi_dir_in_sub_dir )
@@ -816,6 +832,23 @@ def test_cgi_path_in_sub_directories(self):
816
832
finally :
817
833
CGIHTTPRequestHandler .cgi_directories .remove ('/sub/dir/cgi-bin' )
818
834
835
+ def test_accept (self ):
836
+ browser_accept = \
837
+ 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
838
+ tests = (
839
+ ((('Accept' , browser_accept ),), browser_accept ),
840
+ ((), '' ),
841
+ # Hack case to get two values for the one header
842
+ ((('Accept' , 'text/html' ), ('ACCEPT' , 'text/plain' )),
843
+ 'text/html,text/plain' ),
844
+ )
845
+ for headers , expected in tests :
846
+ headers = OrderedDict (headers )
847
+ with self .subTest (headers ):
848
+ res = self .request ('/cgi-bin/file6.py' , 'GET' , headers = headers )
849
+ self .assertEqual (http .HTTPStatus .OK , res .status )
850
+ expected = f"'HTTP_ACCEPT': { expected !r} "
851
+ self .assertIn (expected .encode ('ascii' ), res .read ())
819
852
820
853
821
854
class SocketlessRequestHandler (SimpleHTTPRequestHandler ):
0 commit comments