Skip to content

Commit

Permalink
Merge pull request #256 from xcp-ng/ipv6-fileserver
Browse files Browse the repository at this point in the history
Adapt fileserver tests to handle IPv6 as well
  • Loading branch information
stormi authored Sep 10, 2024
2 parents 4d7106a + 8eb111c commit fb52863
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions tests/misc/test_file_server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import re
import subprocess

import lib.commands as commands

from lib.netutil import wrap_ip

Expand All @@ -11,19 +12,14 @@
# 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}"
regex = fr"{name}:\s?{re.escape(value)}"
return re.match(regex, header) is not None

def test_fileserver_redirect_https(host):
path = "/path/to/dir/file.txt"
ip = wrap_ip(host.hostname_or_ip)
process = subprocess.Popen(
["curl", "-i", "http://" + ip + path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout, _ = process.communicate()
lines = stdout.decode().splitlines()
res = commands.local_cmd(["curl", "-s", "-i", "http://" + ip + path])
lines = res.stdout.splitlines()
assert lines[0].strip() == "HTTP/1.1 301 Moved Permanently"
assert _header_equal(lines[2], "location", "https://" + ip + path)

Expand All @@ -33,13 +29,10 @@ class TestHSTS:
HSTS_HEADER_VALUE = "max-age=63072000"

def __get_header(host):
process = subprocess.Popen(
["curl", "-XGET", "-k", "-I", "https://" + wrap_ip(host.hostname_or_ip)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
res = commands.local_cmd(
["curl", "-s", "-XGET", "-k", "-I", "https://" + wrap_ip(host.hostname_or_ip)]
)
stdout, _ = process.communicate()
return stdout.decode().splitlines()
return res.stdout.splitlines()

def test_fileserver_hsts_default(self, host):
# By default HSTS header should not be set
Expand Down

0 comments on commit fb52863

Please sign in to comment.