From b3d545de34c367f72f519deb2e7b62774142f5a5 Mon Sep 17 00:00:00 2001 From: Steffen Rehberg Date: Fri, 26 Mar 2021 11:25:27 +0100 Subject: [PATCH] Make cleanUrl non-static so that APIs can overwrite it (#614) cleanUrl replaces missing values in key-value-pairs with an empty string so that ?param&... becomes ?param=&... . This may lead to problems in some cases (e.g. at SLUBApi when resolving libero references in getDetailById). Even it we'd replace the "" by null in the else branch of the current implementation, a trailing = will still be be added by URLEncodedUtils.format, as Android uses httpclient-android version 4.3.5.1 which has a bug to add a trailing = sign to key-only parameters by replacing missing parameters with empty strings the same way clearUrl does (versions 4.5 and newer work correctly but can't be used by Android, see also https://hc.apache.org/httpcomponents-client-4.5.x/android.html. To avoid this we make cleanUrl non-static so that APIs can overwrite it with their own version as needed. --- .../main/java/de/geeksfactory/opacclient/apis/BaseApi.java | 2 +- .../de/geeksfactory/opacclient/apis/ApacheBaseApiTest.java | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/opacclient/libopac/src/main/java/de/geeksfactory/opacclient/apis/BaseApi.java b/opacclient/libopac/src/main/java/de/geeksfactory/opacclient/apis/BaseApi.java index 8609931ca..31f830815 100644 --- a/opacclient/libopac/src/main/java/de/geeksfactory/opacclient/apis/BaseApi.java +++ b/opacclient/libopac/src/main/java/de/geeksfactory/opacclient/apis/BaseApi.java @@ -464,7 +464,7 @@ protected JSONObject loadJsonResource(String filename) { * @param myURL the URL to clean * @return cleaned URL */ - public static String cleanUrl(String myURL) { + public String cleanUrl(String myURL) { String[] parts = myURL.split("\\?"); String url = parts[0]; try { diff --git a/opacclient/libopac/src/test/java/de/geeksfactory/opacclient/apis/ApacheBaseApiTest.java b/opacclient/libopac/src/test/java/de/geeksfactory/opacclient/apis/ApacheBaseApiTest.java index e6ed1426b..5340a3fe9 100644 --- a/opacclient/libopac/src/test/java/de/geeksfactory/opacclient/apis/ApacheBaseApiTest.java +++ b/opacclient/libopac/src/test/java/de/geeksfactory/opacclient/apis/ApacheBaseApiTest.java @@ -3,6 +3,7 @@ import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.junit.Test; +import org.mockito.Mockito; import java.util.Arrays; @@ -12,10 +13,11 @@ public class ApacheBaseApiTest { @Test - public void cleanUrlShouldHandleMultipleEqualsSigns() throws Exception { + public void cleanUrlShouldHandleMultipleEqualsSigns() { + BaseApi baseApi = Mockito.mock(BaseApi.class, Mockito.CALLS_REAL_METHODS); String url = "http://www.example.com/file?param1=value=1¶m=value2"; assertEquals("http://www.example.com/file?param1=value%3D1¶m=value2", - ApacheBaseApi.cleanUrl(url)); + baseApi.cleanUrl(url)); } @Test