MeshProxyInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { HttpUrl originalUri = chain.request().url(); StringBuilder host = new StringBuilder(originalUri.uri().getHost()); if (originalUri.uri().getPort() != -1) { host.append(':'); host.append(originalUri.uri().getPort()); } return chain.proceed(chain.request() .newBuilder() .url(originalUri .newBuilder() .host(meshProxy.getHost()) .port(meshProxy.getPort()) .build()) .header(HttpHeaders.HOST, host.toString()) .build()); } MeshProxyInterceptor(HostAndPort meshProxy); @Override Response intercept(Chain chain);  }
@Test public void intercept_portNotInAuthority() throws Exception { when(chain.request()) .thenReturn(new Request.Builder() .url("https: .method("GET", null) .build()); interceptor.intercept(chain); verify(chain).proceed(request.capture()); assertThat(request.getValue().url()).isEqualTo(HttpUrl.parse("https: assertThat(request.getValue().header(HttpHeaders.HOST)).isEqualTo("foo.com"); }
@Test public void intercept_stripsUserinfoFromAuthority() throws Exception { when(chain.request()) .thenReturn(new Request.Builder() .url("https: .method("GET", null) .build()); interceptor.intercept(chain); verify(chain).proceed(request.capture()); assertThat(request.getValue().url()) .isEqualTo(HttpUrl.parse("https: assertThat(request.getValue().header(HttpHeaders.HOST)).isEqualTo("foo.com"); }
@Test public void intercept_portInAuthority() throws Exception { when(chain.request()) .thenReturn(new Request.Builder() .url("https: .method("GET", null) .build()); interceptor.intercept(chain); verify(chain).proceed(request.capture()); assertThat(request.getValue().url()).isEqualTo(HttpUrl.parse("https: assertThat(request.getValue().header(HttpHeaders.HOST)).isEqualTo("foo.com:123"); }