QosExceptionThrowingCallAdapterFactory extends CallAdapter.Factory { @Nullable @Override public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { CallAdapter<?, ?> adapter = delegate.get(returnType, annotations, retrofit); return new QosExceptionThrowingCallAdapter<>(adapter); } QosExceptionThrowingCallAdapterFactory(CallAdapter.Factory delegate); @Nullable @Override CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit);  }
@Test public void http_429_throw_qos_throttle() throws IOException { when(call.execute()).thenReturn(Response.error(429, NO_CONTENT_RESPONSE_BODY)); CallAdapter<Void, Void> adapter = (CallAdapter<Void, Void>) factory.get(type, NO_ANNOTATIONS, retrofit); adapter.adapt(call); assertThatThrownBy(() -> argument.getValue().execute()) .isInstanceOfSatisfying(QosException.Throttle.class, e -> assertThat(e.getRetryAfter()) .isEmpty()); }
@Test public void http_429_throw_qos_throttle_with_retry_after() throws IOException { okhttp3.Request request = new okhttp3.Request.Builder().url(url).get().build(); okhttp3.Response response = new okhttp3.Response.Builder() .code(429) .header(HttpHeaders.RETRY_AFTER, "5") .request(request) .protocol(Protocol.HTTP_1_1) .message("test") .build(); when(call.execute()).thenReturn(Response.error(NO_CONTENT_RESPONSE_BODY, response)); CallAdapter<Void, Void> adapter = (CallAdapter<Void, Void>) factory.get(type, NO_ANNOTATIONS, retrofit); adapter.adapt(call); assertThatThrownBy(() -> argument.getValue().execute()) .isInstanceOfSatisfying(QosException.Throttle.class, e -> assertThat(e.getRetryAfter()) .contains(Duration.ofSeconds(5))); }
@Test public void http_503_throw_qos_unavailable() throws IOException { when(call.execute()).thenReturn(Response.error(503, NO_CONTENT_RESPONSE_BODY)); CallAdapter<Void, Void> adapter = (CallAdapter<Void, Void>) factory.get(type, NO_ANNOTATIONS, retrofit); adapter.adapt(call); assertThatThrownBy(() -> argument.getValue().execute()).isInstanceOf(QosException.Unavailable.class); }