Changeset 19651 in josm for trunk/test


Ignore:
Timestamp:
2026-10-22T16:46:16+02:00 (11 days ago)
Author:
stoecker
Message:

fix #24907 - patch by wangi - Trailing slash in API breaks OAuth and links - better normalization

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        2525target
        2626failed.png
         27dependency-reduced-pom.xml
  • trunk/test/unit/org/openstreetmap/josm/data/preferences/JosmUrlsTest.java

    r18870 r19651  
    2020        assertEquals("https://api06.dev.openstreetmap.org/user", Config.getUrls().getBaseUserUrl());
    2121    }
     22
     23    /**
     24     * An API URL entered with a trailing slash must give the same web site URLs as one without, so that
     25     * "History (web)" and the other links to the server are built from the web site and not from the API path.
     26     */
     27    @Test
     28    void testApiUrlWithTrailingSlash() {
     29        Config.getPref().put("osm-server.url", "https://api06.dev.openstreetmap.org/api/");
     30        assertEquals("https://api06.dev.openstreetmap.org", Config.getUrls().getOSMWebsiteDependingOnSelectedApi());
     31        assertEquals("https://api06.dev.openstreetmap.org", Config.getUrls().getBaseBrowseUrl());
     32        assertEquals("https://api06.dev.openstreetmap.org/user", Config.getUrls().getBaseUserUrl());
     33    }
     34
     35    /**
     36     * The default API URL is recognized even when it is entered with a trailing slash, so the links point to
     37     * the OSM web site rather than to the API host.
     38     */
     39    @Test
     40    void testDefaultApiUrlWithTrailingSlash() {
     41        Config.getPref().put("osm-server.url", Config.getUrls().getDefaultOsmApiUrl() + '/');
     42        assertEquals(Config.getUrls().getOSMWebsite(), Config.getUrls().getOSMWebsiteDependingOnSelectedApi());
     43    }
    2244}
  • trunk/test/unit/org/openstreetmap/josm/io/OsmApiTest.java

    r18853 r19651  
    33
    44import static org.junit.jupiter.api.Assertions.assertEquals;
     5import static org.junit.jupiter.api.Assertions.assertNull;
     6import static org.junit.jupiter.api.Assertions.assertSame;
    57
    68import java.io.ByteArrayInputStream;
    … …  
    1113import org.openstreetmap.josm.data.osm.User;
    1214import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
     15import org.openstreetmap.josm.spi.preferences.Config;
     16import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
    1317
    1418/**
    1519 * Unit tests of {@link OsmApi} class.
    1620 */
     21@BasicPreferences
     22@org.openstreetmap.josm.testutils.annotations.OsmApi
    1723class OsmApiTest {
    1824    /**
    … …  
    3743        assertEquals(User.getAnonymous(), cs2.getUser());
    3844    }
     45
     46    /**
     47     * Unit test of {@link OsmApi#normalizeApiUrl}: white space and trailing slashes are removed.
     48     */
     49    @Test
     50    void testNormalizeApiUrl() {
     51        assertEquals("https://example.com/api", OsmApi.normalizeApiUrl("https://example.com/api"));
     52        assertEquals("https://example.com/api", OsmApi.normalizeApiUrl("https://example.com/api/"));
     53        assertEquals("https://example.com/api", OsmApi.normalizeApiUrl("https://example.com/api///"));
     54        assertEquals("https://example.com/api", OsmApi.normalizeApiUrl("  https://example.com/api/\t"));
     55        assertEquals("https://example.com", OsmApi.normalizeApiUrl("https://example.com/"));
     56        assertEquals("", OsmApi.normalizeApiUrl("   "));
     57        assertNull(OsmApi.normalizeApiUrl(null));
     58        // the slashes of the scheme must not be eaten
     59        assertEquals("https://", OsmApi.normalizeApiUrl("https://"));
     60    }
     61
     62    /**
     63     * A URL which only differs by a trailing slash is the same API, and is stored without the slash.
     64     */
     65    @Test
     66    void testGetOsmApiIgnoresTrailingSlash() {
     67        OsmApi api = OsmApi.getOsmApi("https://example.com/api/");
     68        assertEquals("https://example.com/api", api.getServerUrl());
     69        assertSame(api, OsmApi.getOsmApi("https://example.com/api"));
     70        assertSame(api, OsmApi.getOsmApi(" https://example.com/api// "));
     71    }
     72
     73    /**
     74     * An API URL stored with a trailing slash is used without it, so that code comparing it to a known URL or
     75     * deriving another URL from it works. Non-regression test for the OAuth and "History (web)" breakage.
     76     */
     77    @Test
     78    void testTrailingSlashInPreference() {
     79        Config.getPref().put("osm-server.url", "https://example.com/api/");
     80        assertEquals("https://example.com/api", OsmApi.getOsmApi().getServerUrl());
     81        assertEquals("https://example.com/api/", OsmApi.getOsmApi().getBaseUrl());
     82    }
    3983}
Note: See TracChangeset for help on using the changeset viewer.