3
3
Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>,
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
@@ -588,6 +588,15 @@ def test_html_escape_filename(self):
588
588
print(os.environ["%s"])
589
589
"""
590
590
591
+ cgi_file6 = """\
592
+ #!%s
593
+ import os
594
+
595
+ print("Content-type: text/plain")
596
+ print()
597
+ print(repr(os.environ))
598
+ """
599
+
591
600
592
601
@unittest .skipIf (hasattr (os , 'geteuid' ) and os .geteuid () == 0 ,
593
602
"This test can't be run reliably as root (issue #13308)." )
@@ -666,6 +675,11 @@ def setUp(self):
666
675
file5 .write (cgi_file1 % self .pythonexe )
667
676
os .chmod (self .file5_path , 0o777 )
668
677
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
+
669
683
os .chdir (self .parent_dir )
670
684
671
685
def tearDown (self ):
@@ -685,6 +699,8 @@ def tearDown(self):
685
699
os .remove (self .file4_path )
686
700
if self .file5_path :
687
701
os .remove (self .file5_path )
702
+ if self .file6_path :
703
+ os .remove (self .file6_path )
688
704
os .rmdir (self .cgi_child_dir )
689
705
os .rmdir (self .cgi_dir )
690
706
os .rmdir (self .cgi_dir_in_sub_dir )
@@ -818,6 +834,23 @@ def test_cgi_path_in_sub_directories(self):
818
834
finally :
819
835
CGIHTTPRequestHandler .cgi_directories .remove ('/sub/dir/cgi-bin' )
820
836
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 ())
821
854
822
855
823
856
class SocketlessRequestHandler (SimpleHTTPRequestHandler ):
0 commit comments