home · contact · privacy
Extend POST tests, and handling of missing form data.
[plomtask] / tests / utils.py
index 9964201bbc7d6312ed1e8cb29e1a5a359608e86f..3ca0cb90c8c4ef52519d40a45f5f1e92bbfbbdee 100644 (file)
@@ -2,6 +2,7 @@
 from unittest import TestCase
 from threading import Thread
 from http.client import HTTPConnection
+from urllib.parse import urlencode
 from datetime import datetime
 from os import remove as remove_file
 from plomtask.db import DatabaseFile, DatabaseConnection
@@ -39,3 +40,17 @@ class TestCaseWithServer(TestCaseWithDB):
         self.httpd.server_close()
         self.server_thread.join()
         super().tearDown()
+
+    def post_to(self, data: dict[str, object], target: str) -> None:
+        """Post form data to target URL."""
+        encoded_form_data = urlencode(data).encode('utf-8')
+        headers = {'Content-Type': 'application/x-www-form-urlencoded',
+                   'Content-Length': str(len(encoded_form_data))}
+        self.conn.request('POST', target,
+                          body=encoded_form_data, headers=headers)
+
+    def check_redirect(self, target: str) -> None:
+        """Check that self.conn answers with a 302 redirect to target."""
+        response = self.conn.getresponse()
+        self.assertEqual(response.status, 302)
+        self.assertEqual(response.getheader('Location'), target)