ServiceUnavailableInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Response response = chain.proceed(chain.request()); if (response.code() == HTTPStatus.SERVICE_UNAVAILABLE.code) { RealConnection connection = (RealConnection) chain.connection(); if (connection != null) { connection.setNoNewExchanges(true); } } return response; }  @Override Response intercept(Chain chain);  }
@Test void interceptWith503() throws IOException { var request = new Request.Builder().url("http: var response = new Response.Builder().request(request) .protocol(Protocol.HTTP_2) .code(HTTPStatus.SERVICE_UNAVAILABLE.code) .message("service unavailable").build(); when(chain.request()).thenReturn(request); when(chain.proceed(request)).thenReturn(response); when(chain.connection()).thenReturn(connection); interceptor.intercept(chain); verify(connection).setNoNewExchanges(true); }
@Test void interceptWithout503() throws IOException { var request = new Request.Builder().url("http: var response = new Response.Builder().request(request) .protocol(Protocol.HTTP_2) .code(HTTPStatus.OK.code) .message("ok").build(); when(chain.request()).thenReturn(request); when(chain.proceed(request)).thenReturn(response); interceptor.intercept(chain); }