TimeoutInterceptor implements Interceptor { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Interceptor.Chain chainWithTimeout = chain; Request request = chain.request(); HTTPRequest httpRequest = request.tag(HTTPRequest.class); if (httpRequest != null) { if (httpRequest.connectTimeout != null) { chainWithTimeout = chainWithTimeout.withConnectTimeout((int) httpRequest.connectTimeout.toMillis(), TimeUnit.MILLISECONDS); } if (httpRequest.timeout != null) { chainWithTimeout = chainWithTimeout.withReadTimeout((int) httpRequest.timeout.toMillis(), TimeUnit.MILLISECONDS) .withWriteTimeout((int) httpRequest.timeout.toMillis(), TimeUnit.MILLISECONDS); } } return chainWithTimeout.proceed(request); }  @Override Response intercept(Interceptor.Chain chain);  }
@Test void interceptWithConnectTimeout() throws IOException { var httpRequest = new HTTPRequest(HTTPMethod.POST, "https: httpRequest.connectTimeout = Duration.ofSeconds(30); var request = new Request.Builder().url(httpRequest.uri).tag(HTTPRequest.class, httpRequest).build(); when(chain.request()).thenReturn(request); when(chain.withConnectTimeout(30000, TimeUnit.MILLISECONDS)).thenReturn(chain); interceptor.intercept(chain); }
@Test void interceptWithTimeout() throws IOException { var httpRequest = new HTTPRequest(HTTPMethod.POST, "https: httpRequest.timeout = Duration.ofSeconds(30); var request = new Request.Builder().url(httpRequest.uri).tag(HTTPRequest.class, httpRequest).build(); when(chain.request()).thenReturn(request); when(chain.withReadTimeout(30000, TimeUnit.MILLISECONDS)).thenReturn(chain); when(chain.withWriteTimeout(30000, TimeUnit.MILLISECONDS)).thenReturn(chain); interceptor.intercept(chain); }
@Test void interceptWithoutTimeout() throws IOException { var request = new Request.Builder().url("http: when(chain.request()).thenReturn(request); interceptor.intercept(chain); }