Ticket #23448: 23448.patch

File 23448.patch, 6.1 KB (added by taylor.smock, 2 years ago)

Sample special casing of rate limiting (do not apply)

  • src/org/openstreetmap/josm/io/OsmApi.java

    Subject: [PATCH] Handle rate limiting with a more specific message
    ---
    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/org/openstreetmap/josm/io/OsmApi.java b/src/org/openstreetmap/josm/io/OsmApi.java
    a b  
    830830                    CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER);
    831831                    throw new OsmApiException(retCode, errorHeader, errorBody, activeConnection.getURL().toString(),
    832832                            doAuthenticate ? retrieveBasicAuthorizationLogin(client) : null, response.getContentType());
     833                case 429: // This is the code that OSM uses for rate limiting
     834                    // FIXME: Use a specific exception, we may also want to inform the user that they may want to wait an hour.
     835                    throw new OsmApiException(retCode, errorHeader, errorBody, activeConnection.getURL().toString(),
     836                            doAuthenticate ? retrieveBasicAuthorizationLogin(client) : null, response.getContentType());
    833837                default:
    834838                    throw new OsmApiException(retCode, errorHeader, errorBody);
    835839                }
  • test/unit/org/openstreetmap/josm/io/OsmApiTest.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/test/unit/org/openstreetmap/josm/io/OsmApiTest.java b/test/unit/org/openstreetmap/josm/io/OsmApiTest.java
    a b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.io;
    33
     4import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
    45import static org.junit.jupiter.api.Assertions.assertEquals;
     6import static org.junit.jupiter.api.Assertions.assertThrows;
    57
    68import java.io.ByteArrayInputStream;
    79import java.nio.charset.StandardCharsets;
     10import java.util.Collections;
    811
     12import org.junit.jupiter.api.AfterEach;
     13import org.junit.jupiter.api.BeforeEach;
    914import org.junit.jupiter.api.Test;
     15import org.junit.jupiter.api.extension.RegisterExtension;
     16import org.openstreetmap.josm.TestUtils;
     17import org.openstreetmap.josm.data.coor.LatLon;
     18import org.openstreetmap.josm.data.oauth.IOAuthToken;
     19import org.openstreetmap.josm.data.oauth.OAuth20Exception;
     20import org.openstreetmap.josm.data.oauth.OAuth20Parameters;
     21import org.openstreetmap.josm.data.oauth.OAuth20Token;
     22import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder;
    1023import org.openstreetmap.josm.data.osm.Changeset;
     24import org.openstreetmap.josm.data.osm.Node;
    1125import org.openstreetmap.josm.data.osm.User;
    1226import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    1327
     28import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
     29import com.github.tomakehurst.wiremock.client.WireMock;
     30import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
     31import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
     32import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
     33
    1434/**
    1535 * Unit tests of {@link OsmApi} class.
    1636 */
     37@org.openstreetmap.josm.testutils.annotations.OsmApi(org.openstreetmap.josm.testutils.annotations.OsmApi.APIType.NONE)
    1738class OsmApiTest {
     39    @RegisterExtension
     40    static WireMockExtension wireMockExtension = WireMockExtension.newInstance().options(
     41            WireMockConfiguration.options().usingFilesUnderDirectory(TestUtils.getTestDataRoot())
     42    ).failOnUnmatchedRequests(true).build();
     43
     44    @BeforeEach
     45    void setup(WireMockRuntimeInfo info) throws OAuth20Exception {
     46        OAuthAccessTokenHolder.getInstance().setAccessToken(info.getHttpBaseUrl(), new OAuth20Token(new OAuth20Parameters("clientId", "clientSecret",
     47                "tokenUrl", "authorizeUrl", info.getHttpBaseUrl(), "redirectUrl"),
     48                "{\"access_token\": \"test_token\", \"token_type\": \"bearer\"}"));
     49    }
     50
     51    @AfterEach
     52    void tearDown(WireMockRuntimeInfo info) {
     53        OAuthAccessTokenHolder.getInstance().setAccessToken(info.getHttpBaseUrl(), (IOAuthToken) null);
     54    }
     55
    1856    /**
    1957     * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/12675">Bug #12675</a>.
    2058     * @throws IllegalDataException if an error occurs
    2159     */
    2260    @Test
    2361    void testTicket12675() throws IllegalDataException {
     62
    2463        OsmApi api = OsmApi.getOsmApi();
    2564        Changeset cs = new Changeset();
    2665        cs.setUser(User.getAnonymous());
     
    3675                NullProgressMonitor.INSTANCE).iterator().next();
    3776        assertEquals(User.getAnonymous(), cs2.getUser());
    3877    }
     78
     79    @Test
     80    void testTooManyRequests429(WireMockRuntimeInfo info) {
     81        final OsmApi osmApi = OsmApi.getOsmApi(info.getHttpBaseUrl());
     82        final ResponseDefinitionBuilder responseDefinitionBuilder = WireMock.status(429).withBody("Upload has been blocked due to rate limiting. Please try again later.")
     83                        .withHeader("Error", "Upload has been blocked due to rate limiting. Please try again later.");
     84        info.getWireMock().register(WireMock.get("/capabilities").willReturn(WireMock.aResponse().withBodyFile("api/0.6/capabilities")));
     85        info.getWireMock().register(WireMock.put("/0.6/changeset/create").willReturn(WireMock.aResponse().withBody(Integer.toString(Integer.MAX_VALUE))));
     86        info.getWireMock().register(WireMock.post("/0.6/changeset/2147483647/upload").willReturn(responseDefinitionBuilder));
     87        Changeset changeset = new Changeset();
     88        assertDoesNotThrow(() -> osmApi.openChangeset(changeset, NullProgressMonitor.INSTANCE));
     89        assertEquals(Integer.MAX_VALUE, changeset.getId());
     90        osmApi.setChangeset(changeset);
     91        assertThrows(OsmApiException.class, () -> osmApi.uploadDiff(Collections.singletonList(new Node(LatLon.ZERO)), NullProgressMonitor.INSTANCE));
     92    }
    3993}