|
| 1 | +package test_locally.api; |
| 2 | + |
| 3 | +import com.slack.api.Slack; |
| 4 | +import com.slack.api.SlackConfig; |
| 5 | +import com.slack.api.audit.AuditApiException; |
| 6 | +import com.slack.api.methods.MethodsCompletionException; |
| 7 | +import com.slack.api.methods.SlackApiException; |
| 8 | +import com.slack.api.rate_limits.metrics.MetricsDatastore; |
| 9 | +import com.slack.api.scim.SCIMApiException; |
| 10 | +import com.slack.api.scim.SCIMEndpointName; |
| 11 | +import com.slack.api.scim2.SCIM2ApiException; |
| 12 | +import com.slack.api.scim2.SCIM2EndpointName; |
| 13 | +import com.slack.api.util.thread.DaemonThreadExecutorServiceProvider; |
| 14 | +import org.eclipse.jetty.server.Server; |
| 15 | +import org.eclipse.jetty.server.ServerConnector; |
| 16 | +import org.eclipse.jetty.servlet.ServletHandler; |
| 17 | +import org.eclipse.jetty.servlet.ServletHolder; |
| 18 | +import org.junit.After; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import javax.servlet.http.HttpServlet; |
| 23 | +import javax.servlet.http.HttpServletRequest; |
| 24 | +import javax.servlet.http.HttpServletResponse; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.UUID; |
| 27 | +import java.util.concurrent.ExecutionException; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | + |
| 30 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 31 | +import static org.hamcrest.CoreMatchers.is; |
| 32 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 33 | +import static org.hamcrest.CoreMatchers.nullValue; |
| 34 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | +import static org.junit.Assert.assertThrows; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests behavior when a 429 response contains a non-numeric Retry-After header. |
| 39 | + */ |
| 40 | +public class InvalidRetryAfterHeaderTest { |
| 41 | + |
| 42 | + private static final String TEAM_ID = "T1234567"; |
| 43 | + |
| 44 | + private final String executorName = getClass().getName() + "_" + UUID.randomUUID(); |
| 45 | + private final String token = "xoxb-test-" + UUID.randomUUID(); |
| 46 | + private volatile String retryAfter = "notanumber"; |
| 47 | + private Server server; |
| 48 | + private SlackConfig config; |
| 49 | + private Slack slack; |
| 50 | + |
| 51 | + @Before |
| 52 | + public void setup() throws Exception { |
| 53 | + server = new Server(0); |
| 54 | + ServletHandler handler = new ServletHandler(); |
| 55 | + handler.addServletWithMapping(new ServletHolder(new HttpServlet() { |
| 56 | + @Override |
| 57 | + protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 58 | + response.setContentType("application/json"); |
| 59 | + if (request.getRequestURI().equals("/api/auth.test")) { |
| 60 | + response.getWriter().write("{\"ok\":true,\"team_id\":\"" + TEAM_ID + "\"}"); |
| 61 | + return; |
| 62 | + } |
| 63 | + response.setStatus(429); |
| 64 | + if (retryAfter != null) { |
| 65 | + response.setHeader("Retry-After", retryAfter); |
| 66 | + } |
| 67 | + response.getWriter().write("{\"ok\":false,\"error\":\"ratelimited\"," |
| 68 | + + "\"Errors\":{\"description\":\"ratelimited\",\"code\":429}}"); |
| 69 | + } |
| 70 | + }), "/*"); |
| 71 | + server.setHandler(handler); |
| 72 | + server.start(); |
| 73 | + |
| 74 | + String url = "http://127.0.0.1:" + ((ServerConnector) server.getConnectors()[0]).getLocalPort() + "/api/"; |
| 75 | + config = new SlackConfig(); |
| 76 | + // A private provider prevents close() from shutting down other tests' thread pools. |
| 77 | + config.setExecutorServiceProvider(new DaemonThreadExecutorServiceProvider()); |
| 78 | + config.setMethodsEndpointUrlPrefix(url); |
| 79 | + config.setAuditEndpointUrlPrefix(url); |
| 80 | + config.setScimEndpointUrlPrefix(url); |
| 81 | + config.setScim2EndpointUrlPrefix(url); |
| 82 | + config.getMethodsConfig().setExecutorName(executorName); |
| 83 | + config.getAuditConfig().setExecutorName(executorName); |
| 84 | + config.getSCIMConfig().setExecutorName(executorName); |
| 85 | + config.getSCIM2Config().setExecutorName(executorName); |
| 86 | + config.synchronizeMetricsDatabases(); |
| 87 | + slack = Slack.getInstance(config); |
| 88 | + } |
| 89 | + |
| 90 | + @After |
| 91 | + public void tearDown() throws Exception { |
| 92 | + try { |
| 93 | + slack.close(); |
| 94 | + } finally { |
| 95 | + server.stop(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void nonNumericRetryAfterHeaderShouldThrowSlackApiException() { |
| 101 | + SlackApiException e = assertThrows(SlackApiException.class, |
| 102 | + () -> slack.methods(token).usersList(r -> r)); |
| 103 | + assertThat(e.getResponse().code(), is(429)); |
| 104 | + assertThat(e.getResponse().header("Retry-After"), is("notanumber")); |
| 105 | + assertRetryTimeNotRecorded(config.getMethodsConfig().getMetricsDatastore(), "users.list"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void multipartRequest() { |
| 110 | + SlackApiException e = assertThrows(SlackApiException.class, |
| 111 | + () -> slack.methods(token).usersSetPhoto(r -> r.imageData(new byte[]{1}))); |
| 112 | + assertThat(e.getResponse().code(), is(429)); |
| 113 | + assertThat(e.getResponse().header("Retry-After"), is("notanumber")); |
| 114 | + assertRetryTimeNotRecorded(config.getMethodsConfig().getMetricsDatastore(), "users.setPhoto"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void asyncMethodsClientRejectingRateLimitedCalls() { |
| 119 | + config.getMethodsConfig().setRejectRateLimitedCalls(true); |
| 120 | + ExecutionException e = assertThrows(ExecutionException.class, |
| 121 | + () -> slack.methodsAsync(token).usersList(r -> r).get(10, TimeUnit.SECONDS)); |
| 122 | + assertThat(e.getCause(), is(instanceOf(MethodsCompletionException.class))); |
| 123 | + SlackApiException cause = ((MethodsCompletionException) e.getCause()).getSlackApiException(); |
| 124 | + assertThat(cause, is(notNullValue())); |
| 125 | + assertThat(cause.getResponse().code(), is(429)); |
| 126 | + assertThat(cause.getResponse().header("Retry-After"), is("notanumber")); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void auditClient() { |
| 131 | + AuditApiException e = assertThrows(AuditApiException.class, |
| 132 | + () -> slack.audit(token).getLogs(r -> r)); |
| 133 | + assertThat(e.getResponse().code(), is(429)); |
| 134 | + assertThat(e.getResponse().header("Retry-After"), is("notanumber")); |
| 135 | + assertRetryTimeNotRecorded(config.getAuditConfig().getMetricsDatastore(), "logs"); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void scimClient() { |
| 140 | + SCIMApiException e = assertThrows(SCIMApiException.class, |
| 141 | + () -> slack.scim(token).readUser(r -> r.id("W1234567"))); |
| 142 | + assertThat(e.getResponse().code(), is(429)); |
| 143 | + assertThat(e.getResponse().header("Retry-After"), is("notanumber")); |
| 144 | + assertRetryTimeNotRecorded(config.getSCIMConfig().getMetricsDatastore(), SCIMEndpointName.readUser.name()); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void scim2Client() { |
| 149 | + SCIM2ApiException e = assertThrows(SCIM2ApiException.class, |
| 150 | + () -> slack.scim2(token).readUser(r -> r.id("W1234567"))); |
| 151 | + assertThat(e.getResponse().code(), is(429)); |
| 152 | + assertThat(e.getResponse().header("Retry-After"), is("notanumber")); |
| 153 | + assertRetryTimeNotRecorded(config.getSCIM2Config().getMetricsDatastore(), SCIM2EndpointName.readUser.name()); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void numericRetryAfterHeaderIsStillRecorded() { |
| 158 | + retryAfter = "30"; |
| 159 | + assertThrows(SlackApiException.class, () -> slack.methods(token).usersList(r -> r)); |
| 160 | + assertThrows(SlackApiException.class, () -> slack.methods(token).usersSetPhoto(r -> r.imageData(new byte[]{1}))); |
| 161 | + assertThrows(AuditApiException.class, () -> slack.audit(token).getLogs(r -> r)); |
| 162 | + assertThrows(SCIMApiException.class, () -> slack.scim(token).readUser(r -> r.id("W1234567"))); |
| 163 | + assertThrows(SCIM2ApiException.class, () -> slack.scim2(token).readUser(r -> r.id("W1234567"))); |
| 164 | + |
| 165 | + assertRetryTimeRecorded(config.getMethodsConfig().getMetricsDatastore(), "users.list"); |
| 166 | + assertRetryTimeRecorded(config.getMethodsConfig().getMetricsDatastore(), "users.setPhoto"); |
| 167 | + assertRetryTimeRecorded(config.getAuditConfig().getMetricsDatastore(), "logs"); |
| 168 | + assertRetryTimeRecorded(config.getSCIMConfig().getMetricsDatastore(), SCIMEndpointName.readUser.name()); |
| 169 | + assertRetryTimeRecorded(config.getSCIM2Config().getMetricsDatastore(), SCIM2EndpointName.readUser.name()); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void missingRetryAfterHeader() { |
| 174 | + retryAfter = null; |
| 175 | + SlackApiException e = assertThrows(SlackApiException.class, |
| 176 | + () -> slack.methods(token).usersList(r -> r)); |
| 177 | + assertThat(e.getResponse().code(), is(429)); |
| 178 | + assertRetryTimeNotRecorded(config.getMethodsConfig().getMetricsDatastore(), "users.list"); |
| 179 | + } |
| 180 | + |
| 181 | + private void assertRetryTimeNotRecorded(MetricsDatastore datastore, String methodName) { |
| 182 | + assertThat(datastore.getRateLimitedMethodRetryEpochMillis(executorName, TEAM_ID, methodName), is(nullValue())); |
| 183 | + } |
| 184 | + |
| 185 | + private void assertRetryTimeRecorded(MetricsDatastore datastore, String methodName) { |
| 186 | + assertThat(datastore.getRateLimitedMethodRetryEpochMillis(executorName, TEAM_ID, methodName), is(notNullValue())); |
| 187 | + } |
| 188 | +} |
0 commit comments