From 653ab9528a42d0b83d8277148373937b9e5a0380 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 15 Apr 2024 12:30:10 +0200 Subject: [PATCH] autotest: make test_vsicurl_bearer() independent of currently flaky server --- autotest/conftest.py | 21 ++++++++++++++++ autotest/gcore/vsicurl.py | 52 ++++++++++++++++++++++++++++++--------- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/autotest/conftest.py b/autotest/conftest.py index 1856fd9ebc98..acdfc206bbfb 100755 --- a/autotest/conftest.py +++ b/autotest/conftest.py @@ -217,6 +217,27 @@ def pytest_collection_modifyitems(config, items): if not gdaltest.built_against_curl(): item.add_marker(pytest.mark.skip("curl support not available")) + required_version = [ + mark.args[0] if len(mark.args) > 0 else 0, + mark.args[1] if len(mark.args) > 1 else 0, + mark.args[2] if len(mark.args) > 2 else 0, + ] + + actual_version = [0, 0, 0] + for build_info_item in gdal.VersionInfo("BUILD_INFO").strip().split("\n"): + if build_info_item.startswith("CURL_VERSION="): + actual_version = [ + int(x) + for x in build_info_item[len("CURL_VERSION=") :].split(".") + ] + + if actual_version < required_version: + item.add_marker( + pytest.mark.skip( + f"Requires curl >= {'.'.join(str(x) for x in required_version)}" + ) + ) + def pytest_addoption(parser): parser.addini("gdal_version", "GDAL version for which pytest.ini was generated") diff --git a/autotest/gcore/vsicurl.py b/autotest/gcore/vsicurl.py index 69493680ff2f..1b425be6935d 100755 --- a/autotest/gcore/vsicurl.py +++ b/autotest/gcore/vsicurl.py @@ -1109,19 +1109,47 @@ def test_vsicurl_NETRC_FILE(): # Check auth with bearer token -def test_vsicurl_bearer(): - if gdaltest.is_travis_branch("ubuntu_1804") or gdaltest.is_travis_branch( - "ubuntu_1804_32bit" - ): - pytest.skip("Too old libcurl version, requires at least 7.61.0") +@gdaltest.enable_exceptions() +@pytest.mark.require_curl(7, 61, 0) +def test_vsicurl_bearer(server): + + gdal.VSICurlClearCache() + + handler = webserver.SequentialHandler() + handler.add("GET", "/", 404) + handler.add( + "HEAD", + "/test_vsicurl_bearer.bin", + 200, + {"Content-Length": "3"}, + expected_headers={ + "Authorization": "Bearer myuniqtok", + }, + ) + handler.add( + "GET", + "/test_vsicurl_bearer.bin", + 200, + {"Content-Length": "3"}, + b"foo", + expected_headers={ + "Authorization": "Bearer myuniqtok", + }, + ) + token = "myuniqtok" - with gdal.config_options({"GDAL_HTTP_AUTH": "BEARER", "GDAL_HTTP_BEARER": token}): - f = gdal.VSIFOpenL("/vsicurl/http://httpbin.org/bearer", "rb") - gdal.VSIFSeekL(f, 0, 2) - vsilen = gdal.VSIFTellL(f) - gdal.VSIFSeekL(f, 0, 0) - data = gdal.VSIFReadL(1, vsilen, f).decode("ascii") - assert token in data + with webserver.install_http_handler(handler): + with gdal.config_options( + {"GDAL_HTTP_AUTH": "BEARER", "GDAL_HTTP_BEARER": token} + ): + f = gdal.VSIFOpenL( + "/vsicurl/http://localhost:%d/test_vsicurl_bearer.bin" % server.port, + "rb", + ) + assert f + data = gdal.VSIFReadL(1, 3, f) + gdal.VSIFCloseL(f) + assert data == b"foo" ###############################################################################