diff --git a/README.md b/README.md index b720e09..ffb5e6b 100644 --- a/README.md +++ b/README.md @@ -240,7 +240,7 @@ For each curl test, you may define the following properties: - `logged_on_sso`: `true` or `false` wether the test is performed being logged in on the SSO or not (default: `false`) - `expect_title`: some text expected to be found in the HTML page's `` (none/ignored by default) - `expect_content`: some text expected to be found in the HTTP payload -- `expect_return_code`: integer, the expected HTTP return code (default `200`) +- `expect_return_code`: integer or list of integers, the expected HTTP return code (default `200`) - `auto_test_assets`: wether or not to test the first CSS and first JS asset found on the HTML page (default `false` except when the app provides no curl test and use the default mode) - `base_url`: defaults to the app's install URL (`$domain$path`). Can be changed to something like `https://__DOMAIN__` combined with `path` set to for example `/.well-known/foobar`, useful to test URLs which may be on a different domain or always at the domain root even when the app is on a subpath (such as well-known endpoints) diff --git a/lib/curl_tests.py b/lib/curl_tests.py index f7dbb6a..bd60422 100644 --- a/lib/curl_tests.py +++ b/lib/curl_tests.py @@ -182,6 +182,13 @@ def test( base_tag = html.find("base") base = base_tag.get("href", "") if base_tag else "" + def code_was_expected(code: int) -> bool: + if isinstance(expect_return_code, int): + return code == expect_return_code + if isinstance(expect_return_code, list): + return code in expect_return_code + raise ValueError("expect_return_code should be list or int") + errors = [] if expect_effective_url is None and "/yunohost/sso" in effective_url: errors.append( @@ -191,7 +198,7 @@ def test( errors.append( f"Ended up on URL '{effective_url}', but was expecting '{expect_effective_url}'" ) - if expect_return_code and code != expect_return_code: + if not code_was_expected(code): errors.append(f"Got return code {code}, but was expecting {expect_return_code}") if expect_title is None and "Welcome to nginx" in title: errors.append("The request ended up on the default nginx page?") @@ -206,7 +213,7 @@ def test( assets = [] # Auto-check assets - though skip this if we have an unexpected return code for the main page, because there's very likely no asset to find - if auto_test_assets and code == expect_return_code: + if auto_test_assets and code_was_expected(code): assets_to_check = [] stylesheets = html.find_all("link", rel="stylesheet", href=True) stylesheets = [