1
0
Fork 0
forked from s3lph/matemat

Unit tests for 304 Not Modified testing.

This commit is contained in:
s3lph 2018-07-14 23:05:50 +02:00
parent a9fc6f451b
commit 96eaa2c4c0
2 changed files with 31 additions and 1 deletions

View file

@ -118,7 +118,7 @@ class MockServer:
) )
# Set up logger # Set up logger
self.logger: logging.Logger = logging.getLogger('matemat unit test') 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 # Initalize a log handler to stderr and set the log format
sh: logging.StreamHandler = logging.StreamHandler() sh: logging.StreamHandler = logging.StreamHandler()
sh.setFormatter(logging.Formatter('%(asctime)s %(name)s [%(levelname)s]: %(message)s')) sh.setFormatter(logging.Formatter('%(asctime)s %(name)s [%(levelname)s]: %(message)s'))

View file

@ -3,6 +3,8 @@ from typing import Any, Dict, Union
import os import os
import os.path import os.path
from datetime import datetime, timedelta
from matemat.exceptions import HttpException from matemat.exceptions import HttpException
from matemat.webserver import HttpHandler, RequestArguments, PageletResponse, RedirectResponse, TemplateResponse from matemat.webserver import HttpHandler, RequestArguments, PageletResponse, RedirectResponse, TemplateResponse
from matemat.webserver.test.abstract_httpd_test import AbstractHttpdTest, test_pagelet from matemat.webserver.test.abstract_httpd_test import AbstractHttpdTest, test_pagelet
@ -163,6 +165,34 @@ class TestServe(AbstractHttpdTest):
self.assertEqual(403, packet.statuscode) self.assertEqual(403, packet.statuscode)
self.assertNotEqual(b'This should not be readable', packet.body) 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): def test_serve_not_found(self):
# Request a nonexistent resource # Request a nonexistent resource
self.client_sock.set_request(b'GET /nonexistent HTTP/1.1\r\n\r\n') self.client_sock.set_request(b'GET /nonexistent HTTP/1.1\r\n\r\n')