Update tests for file.py.

This commit is contained in:
Boris Baldassari 2024-03-06 16:35:27 +01:00
parent b48ffa3781
commit d6922c17ec
5 changed files with 55 additions and 11 deletions

View file

@ -76,7 +76,7 @@ def resolve_path(res:str, root: str):
if res[-1] == "/":
res += "index.html"
if res[0] == "/":
while res[0] == "/":
res = res[1:]
res_path = path.join(root, res)

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

View file

@ -14,7 +14,7 @@ Script to test the myserver module.
import pytest
import sys
from os.path import dirname
from os.path import dirname, join
from myserver.file import resolve_location, resolve_path, get_resource
@ -29,22 +29,66 @@ def _find_root():
def test_resolve_path():
"""
Tests the resolve_path() function.
Tests the resolve_path() function with some normal use cases.
"""
tests_path = _find_root()
print('Tests path:', tests_path)
full_path = resolve_path(res='/index_simple.html', root=tests_path)
print(full_path)
assert full_path.endswith('/index_simple.html')
full_path = resolve_path(res='/index_simple.html', root=tests_path)
assert 1 == 2
res_path = join(tests_path, 'resources', 'www')
# Normal resource.
full_path = resolve_path(res='/index.html', root=res_path)
print(full_path)
assert full_path.endswith('resources/www/index.html')
# Let's try with a folder in resource, with an image.
full_path = resolve_path(res='/images/chaton.jpg', root=res_path)
print(full_path)
assert full_path.endswith('resources/www/images/chaton.jpg')
def test_resolve_path_weirdos():
"""
Tests the resolve_path() function with some abnormal use cases.
"""
tests_path = _find_root()
res_path = join(tests_path, 'resources', 'www')
# //index.html should be the same as /index.html.
full_path = resolve_path(res='//index.html', root=res_path)
print(full_path)
assert full_path.endswith('resources/www/index.html')
# / should return /index.html path
full_path = resolve_path(res='/', root=res_path)
print(full_path)
assert full_path.endswith('resources/www/index.html')
# Non-existing resources should return the empty string ''.
full_path = resolve_path(res='/index.dontexist', root=res_path)
print(full_path)
assert full_path == ''
def test_get_resource():
"""
Tests the get_resource() function by capturing stdout.
Tests the get_resource() function by checking the content of the returned file.
"""
out = get_resource(res_path='tests/resources/index_simple.html')
tests_path = _find_root()
res_path = join(tests_path, 'resources', 'www', 'index.html')
out = get_resource(res_path=res_path)
print(out)
assert out[0] == b' <!DOCTYPE html>\n<html>\n<body>\n\n<h1>My First Heading</h1>\n<p>My first paragraph. See https://www.w3schools.com/html/html_basic.asp</p>\n\n</body>\n</html> '
def test_get_resource_image():
"""
Tests the get_resource() function by checking the content of an image.
For an image, check we get the magic numbers for a JPEG.
See https://gist.github.com/leommoore/f9e57ba2aa4bf197ebc5 for a complete list.
"""
tests_path = _find_root()
res_path = join(tests_path, 'resources', 'www', 'images', 'chaton.jpg')
out = get_resource(res_path=res_path)
print(out[0][:4])
assert out[0][:4] == b'\xff\xd8\xff\xe0'