Add a test for POST with data

This commit is contained in:
Kovid Goyal 2024-08-17 09:51:34 +05:30
parent 23cf355130
commit b2745092c6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 24 additions and 4 deletions

View File

@ -167,6 +167,11 @@ class Browser:
else: else:
f.write(data) f.write(data)
cmd['data_path'] = f.name cmd['data_path'] = f.name
for k, v in cmd['headers']:
if k.lower() == 'content-type':
break
else:
cmd['headers'].append(('Content-Type', 'application/x-www-form-urlencoded'))
res = FakeResponse() res = FakeResponse()
self.dispatch_map[self.id_counter] = res.queue self.dispatch_map[self.id_counter] = res.queue
self._send_command(cmd) self._send_command(cmd)

View File

@ -23,6 +23,11 @@ class Handler(http.server.BaseHTTPRequestHandler):
self.test_obj = test_obj self.test_obj = test_obj
super().__init__(*a) super().__init__(*a)
def do_POST(self):
if self.test_obj.dont_send_response:
return
self.do_response()
def do_GET(self): def do_GET(self):
if self.test_obj.dont_send_response: if self.test_obj.dont_send_response:
return return
@ -35,6 +40,9 @@ class Handler(http.server.BaseHTTPRequestHandler):
self.end_headers() self.end_headers()
self.flush_headers() self.flush_headers()
return return
self.do_response()
def do_response(self):
h = {} h = {}
for k, v in self.headers.items(): for k, v in self.headers.items():
h.setdefault(k, []).append(v) h.setdefault(k, []).append(v)
@ -43,7 +51,10 @@ class Handler(http.server.BaseHTTPRequestHandler):
'path': self.path, 'path': self.path,
'headers': h, 'headers': h,
'request_count': self.test_obj.request_count, 'request_count': self.test_obj.request_count,
'method': self.command,
} }
if 'Content-Length' in self.headers:
ans['data'] = self.rfile.read(int(self.headers['Content-Length'])).decode()
data = json.dumps(ans).encode() data = json.dumps(ans).encode()
self.send_response(http.HTTPStatus.OK) self.send_response(http.HTTPStatus.OK)
self.send_header('Content-type', 'application/json') self.send_header('Content-type', 'application/json')
@ -91,13 +102,13 @@ class TestFetchBackend(unittest.TestCase):
def u(path=''): def u(path=''):
return f'http://localhost:{self.port}{path}' return f'http://localhost:{self.port}{path}'
def get(path='', headers=None, timeout=None): def get(path='', headers=None, timeout=None, data=None):
url = u(path) url = u(path)
if headers: if headers:
req = Request(url, headers=headers) req = Request(url, headers=headers)
else: else:
req = url req = url
res = br.open(req, timeout=timeout) res = br.open(req, data=data, timeout=timeout)
raw = res.read() raw = res.read()
ans = json.loads(raw) ans = json.loads(raw)
ans['final_url'] = res.geturl() ans['final_url'] = res.geturl()
@ -118,6 +129,7 @@ class TestFetchBackend(unittest.TestCase):
try: try:
r = get() r = get()
self.ae(r['method'], 'GET')
self.ae(r['request_count'], 1) self.ae(r['request_count'], 1)
self.ae(r['headers']['th'], ['1']) self.ae(r['headers']['th'], ['1'])
self.ae(r['headers']['User-Agent'], ['test-ua']) self.ae(r['headers']['User-Agent'], ['test-ua'])
@ -144,17 +156,20 @@ class TestFetchBackend(unittest.TestCase):
r = get() r = get()
self.ae(r['headers']['User-Agent'], ['man in black']) self.ae(r['headers']['User-Agent'], ['man in black'])
self.ae(r['headers']['Cookie'], ['sc=1; cook=ie']) self.ae(r['headers']['Cookie'], ['sc=1; cook=ie'])
r = get(data=b'1234')
self.ae(r['method'], 'POST')
self.ae(r['data'], '1234')
finally: finally:
br.shutdown() br.shutdown()
def run_server(self): def run_server(self):
import socketserver from http.server import ThreadingHTTPServer
def create_handler(*a): def create_handler(*a):
ans = Handler(self, *a) ans = Handler(self, *a)
return ans return ans
with socketserver.TCPServer(("", 0), create_handler) as httpd: with ThreadingHTTPServer(("", 0), create_handler) as httpd:
self.server = httpd self.server = httpd
self.port = httpd.server_address[1] self.port = httpd.server_address[1]
self.server_started.set() self.server_started.set()