From b615b5b98b7363a0987ec35e669187e4de1a8c12 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 20 Mar 2024 02:32:07 +0100
Subject: [PATCH] Add rump HTTP server/handler testing.

---
 .pylintrc          |  2 +-
 tests/test_days.py | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/.pylintrc b/.pylintrc
index 8033983..82e4d96 100644
--- a/.pylintrc
+++ b/.pylintrc
@@ -1,3 +1,3 @@
 [BASIC]
 init-hook='import sys; sys.path.append(".")'
-good-names-rgxs=do_(GET|POST)(_[a-z]+)?,test_[A-Z]+
+good-names-rgxs=(test_)?do_(GET|POST)(_[a-z]+)?,test_[A-Z]+
diff --git a/tests/test_days.py b/tests/test_days.py
index 3a6c633..35380c6 100644
--- a/tests/test_days.py
+++ b/tests/test_days.py
@@ -1,7 +1,10 @@
 """Test days module."""
 from unittest import TestCase
+from threading import Thread
+from http.client import HTTPConnection
 from datetime import datetime
 from os import remove as remove_file
+from plomtask.http import TaskHandler, TaskServer
 from plomtask.days import Day
 from plomtask.misc import HandledException
 from plomtask.db import DatabaseFile, DatabaseConnection
@@ -97,3 +100,27 @@ class TestsWithDB(TestCase):
         d2.save(self.db_conn)
         day_retrieved = Day.by_date(self.db_conn, d2.date)
         self.assertEqual(day_retrieved.comment, 'foo')
+
+
+class TestsWithServer(TestsWithDB):
+    """Tests against our HTTP server/handler (and database)."""
+
+    def setUp(self):
+        super().setUp()
+        self.httpd = TaskServer(self.db_file, ('localhost', 0), TaskHandler)
+        self.server_thread = Thread(target=self.httpd.serve_forever)
+        self.server_thread.daemon = True
+        self.server_thread.start()
+
+    def test_do_GET_day(self):
+        """Test /day response."""
+        conn = HTTPConnection(*self.httpd.server_address)
+        conn.request('GET', '/day')
+        response = conn.getresponse()
+        self.assertEqual(response.status, 200)
+
+    def tearDown(self):
+        self.httpd.shutdown()
+        self.httpd.server_close()
+        self.server_thread.join()
+        super().tearDown()
-- 
2.30.2