BaseApacheClient { protected static HttpHost determineTarget(final HttpUriRequest request, Headers headers) { HttpHost target = null; final URI requestURI = request.getURI(); if (requestURI.isAbsolute()) { target = URIUtils.extractHost(requestURI); if (target == null) { throw new UnirestException("URI does not specify a valid host name: " + requestURI); } if(headers.containsKey("Host") && InetAddressUtils.isIPv4Address(target.getHostName())){ try { InetAddress address = InetAddress.getByName(target.getHostName()); target = new HttpHost(address, headers.getFirst("Host"), target.getPort(), target.getSchemeName()); }catch (UnknownHostException e){ throw new UnirestException(e); } } } return target; }  void setConfigFactory(RequestConfigFactory configFactory);  }
@Test void basicHost() { request = new HttpGet(URI.create("http: HttpHost host = client.determineTarget(request, headers); assertEquals("zombo.com", host.getHostName()); assertNull(host.getAddress()); assertEquals(-1, host.getPort()); assertEquals("http", host.getSchemeName()); }
@Test void basicHost_withPort() { request = new HttpGet(URI.create("http: HttpHost host = client.determineTarget(request, headers); assertEquals("zombo.com", host.getHostName()); assertNull(host.getAddress()); assertEquals(8080, host.getPort()); assertEquals("http", host.getSchemeName()); }
@Test void willIgnoreHostHeaderprovided_IfNotIPv4() { headers.add("Host","homestarrunner.com"); request = new HttpGet(URI.create("http: HttpHost host = client.determineTarget(request, headers); assertEquals("zombo.com", host.getHostName()); }
@Test void willUseHostHeaderprovided_IfNotIPv4() throws UnknownHostException { headers.add("Host","homestarrunner.com"); request = new HttpGet(URI.create("http: HttpHost host = client.determineTarget(request, headers); assertEquals("homestarrunner.com", host.getHostName()); assertEquals(InetAddress.getByName("127.0.0.1"), host.getAddress()); }
@Test void ifUrlIsRelativeThenReturnNull() { request = new HttpGet(URI.create("/somewhere")); HttpHost host = client.determineTarget(request, headers); assertNull(host); }