Skip to content

Commit

Permalink
Support new format for HTTP header
Browse files Browse the repository at this point in the history
XAPI changed HTTP header format:
xapi-project/xen-api#5238

Signed-off-by: Benjamin Reis <[email protected]>
  • Loading branch information
benjamreis committed Jan 31, 2024
1 parent 65f75c3 commit 4070ae3
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions tests/misc/test_file_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import re
import subprocess

# These tests are meant to test an host fileserver behavior.
Expand All @@ -7,6 +8,10 @@
# - an XCP-ng host >= 8.2 with latest updates (and >= 8.3 for the HSTS test).
# The host must be configured with `website-https-only` set to true (which is the default config).

def _header_equal(header, name, value):
regex = fr"{name}:\s?{value}"
return re.match(regex, header) is not None

def test_fileserver_redirect_https(host):
path = "/path/to/dir/file.txt"
process = subprocess.Popen(
Expand All @@ -17,11 +22,12 @@ def test_fileserver_redirect_https(host):
stdout, _ = process.communicate()
lines = stdout.decode().splitlines()
assert lines[0].strip() == "HTTP/1.1 301 Moved Permanently"
assert lines[2].strip() == "location:https://" + host.hostname_or_ip + path
assert _header_equal(lines[2], "location", "https://" + host.hostname_or_ip + path)

@pytest.mark.usefixtures("host_at_least_8_3")
class TestHSTS:
HSTS_HEADER = "strict-transport-security:max-age=63072000"
HSTS_HEADER_NAME = "strict-transport-security"
HSTS_HEADER_VALUE = "max-age=63072000"

def __get_header(host):
process = subprocess.Popen(
Expand All @@ -35,13 +41,13 @@ def __get_header(host):
def test_fileserver_hsts_default(self, host):
# By default HSTS header should not be set
for line in TestHSTS.__get_header(host):
assert line != TestHSTS.HSTS_HEADER
assert not _header_equal(line, TestHSTS.HSTS_HEADER_NAME, TestHSTS.HSTS_HEADER_VALUE)

def test_fileserver_hsts(self, host_with_hsts):
hsts_header_found = False

for line in TestHSTS.__get_header(host_with_hsts):
if line == TestHSTS.HSTS_HEADER:
if _header_equal(line, TestHSTS.HSTS_HEADER_NAME, TestHSTS.HSTS_HEADER_VALUE):
hsts_header_found = True
break

Expand Down

0 comments on commit 4070ae3

Please sign in to comment.