diff --git a/matemat/webserver/test/abstract_httpd_test.py b/matemat/webserver/test/abstract_httpd_test.py index 0817d41..5680836 100644 --- a/matemat/webserver/test/abstract_httpd_test.py +++ b/matemat/webserver/test/abstract_httpd_test.py @@ -118,7 +118,7 @@ class MockServer: ) # Set up logger self.logger: logging.Logger = logging.getLogger('matemat unit test') - self.logger.setLevel(0) + self.logger.setLevel(logging.DEBUG) # Initalize a log handler to stderr and set the log format sh: logging.StreamHandler = logging.StreamHandler() sh.setFormatter(logging.Formatter('%(asctime)s %(name)s [%(levelname)s]: %(message)s')) diff --git a/matemat/webserver/test/test_serve.py b/matemat/webserver/test/test_serve.py index 5db53b3..c67e06e 100644 --- a/matemat/webserver/test/test_serve.py +++ b/matemat/webserver/test/test_serve.py @@ -3,6 +3,8 @@ from typing import Any, Dict, Union import os import os.path +from datetime import datetime, timedelta + from matemat.exceptions import HttpException from matemat.webserver import HttpHandler, RequestArguments, PageletResponse, RedirectResponse, TemplateResponse from matemat.webserver.test.abstract_httpd_test import AbstractHttpdTest, test_pagelet @@ -163,6 +165,34 @@ class TestServe(AbstractHttpdTest): self.assertEqual(403, packet.statuscode) self.assertNotEqual(b'This should not be readable', packet.body) + def test_serve_static_cache(self): + # Request a static resource + timeout: datetime = datetime.utcnow() + timedelta(hours=1) + timeoutstr = timeout.strftime('%a, %d %b %Y %H:%M:%S GMT') + self.client_sock.set_request( + f'GET /static_resource.txt HTTP/1.1\r\nIf-Modified-Since: {timeoutstr}\r\n\r\n'.encode('utf-8')) + HttpHandler(self.client_sock, ('::1', 45678), self.server) + packet = self.client_sock.get_response() + + # Make sure that no pagelet was called + self.assertIsNone(packet.pagelet) + # Make sure a 304 Not Modified is sent and the body is empty + self.assertEqual(304, packet.statuscode) + self.assertEqual(0, len(packet.body)) + + def test_serve_static_cache_renew(self): + # Request a static resource + self.client_sock.set_request( + b'GET /static_resource.txt HTTP/1.1\r\nIf-Modified-Since: Mon, 01 Jan 2018 13:37:42 GMT\r\n\r\n') + HttpHandler(self.client_sock, ('::1', 45678), self.server) + packet = self.client_sock.get_response() + + # Make sure that no pagelet was called + self.assertIsNone(packet.pagelet) + # Make sure the expected content is served + self.assertEqual(200, packet.statuscode) + self.assertEqual(b'static resource test', packet.body) + def test_serve_not_found(self): # Request a nonexistent resource self.client_sock.set_request(b'GET /nonexistent HTTP/1.1\r\n\r\n')