@Test
  public void testApacheProxyAddressStrategy() throws Exception {

    ourServlet.setServerAddressStrategy(ApacheProxyAddressStrategy.forHttp());
    HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient");
    HttpResponse status = ourClient.execute(httpGet);
    String responseContent = IOUtils.toString(status.getEntity().getContent());
    IOUtils.closeQuietly(status.getEntity().getContent());
    ourLog.info(responseContent);
    assertEquals(200, status.getStatusLine().getStatusCode());
    assertThat(responseContent, containsString("<family value=\"FAMILY\""));
    assertThat(
        responseContent,
        containsString("<fullUrl value=\"http://localhost:" + ourPort + "/Patient/1\"/>"));

    ourServlet.setServerAddressStrategy(new ApacheProxyAddressStrategy(false));
    httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient");
    httpGet.addHeader("x-forwarded-host", "foo.com");
    status = ourClient.execute(httpGet);
    responseContent = IOUtils.toString(status.getEntity().getContent());
    IOUtils.closeQuietly(status.getEntity().getContent());
    ourLog.info(responseContent);
    assertEquals(200, status.getStatusLine().getStatusCode());
    assertThat(responseContent, containsString("<family value=\"FAMILY\""));
    assertThat(responseContent, containsString("<fullUrl value=\"http://foo.com/Patient/1\"/>"));

    ourServlet.setServerAddressStrategy(ApacheProxyAddressStrategy.forHttps());
    httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient");
    httpGet.addHeader("x-forwarded-host", "foo.com");
    status = ourClient.execute(httpGet);
    responseContent = IOUtils.toString(status.getEntity().getContent());
    IOUtils.closeQuietly(status.getEntity().getContent());
    ourLog.info(responseContent);
    assertEquals(200, status.getStatusLine().getStatusCode());
    assertThat(responseContent, containsString("<family value=\"FAMILY\""));
    assertThat(responseContent, containsString("<fullUrl value=\"https://foo.com/Patient/1\"/>"));

    ourServlet.setServerAddressStrategy(new ApacheProxyAddressStrategy(false));
    httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient");
    httpGet.addHeader("x-forwarded-host", "foo.com");
    httpGet.addHeader("x-forwarded-proto", "https");
    status = ourClient.execute(httpGet);
    responseContent = IOUtils.toString(status.getEntity().getContent());
    IOUtils.closeQuietly(status.getEntity().getContent());
    ourLog.info(responseContent);
    assertEquals(200, status.getStatusLine().getStatusCode());
    assertThat(responseContent, containsString("<family value=\"FAMILY\""));
    assertThat(responseContent, containsString("<fullUrl value=\"https://foo.com/Patient/1\"/>"));
  }
  @Test
  public void testIncomingRequestAddressStrategy() throws Exception {
    ourServlet.setServerAddressStrategy(new IncomingRequestAddressStrategy());

    HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient");
    HttpResponse status = ourClient.execute(httpGet);
    String responseContent = IOUtils.toString(status.getEntity().getContent());
    IOUtils.closeQuietly(status.getEntity().getContent());
    ourLog.info(responseContent);
    assertEquals(200, status.getStatusLine().getStatusCode());
    assertThat(responseContent, containsString("<family value=\"FAMILY\""));
    assertThat(
        responseContent,
        containsString("<fullUrl value=\"http://localhost:" + ourPort + "/Patient/1\"/>"));
  }
 @Before
 public void before() {
   ourServlet.setServerAddressStrategy(ourDefaultAddressStrategy);
   ourLastIncludes = null;
   ourLastAndList = null;
 }
  /** #149 */
  @Test
  public void testReturnLinksWithAddressStrategy() throws Exception {
    ourServlet.setServerAddressStrategy(
        new HardcodedServerAddressStrategy("https://blah.com/base"));

    HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_query=findWithLinks");

    CloseableHttpResponse status = ourClient.execute(httpGet);
    String responseContent = IOUtils.toString(status.getEntity().getContent());
    IOUtils.closeQuietly(status.getEntity().getContent());
    assertEquals(200, status.getStatusLine().getStatusCode());
    Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);

    ourLog.info(responseContent);

    assertEquals(10, bundle.getEntries().size());
    assertEquals("https://blah.com/base", bundle.getLinkBase().getValue());
    assertEquals(
        "https://blah.com/base/Patient?_query=findWithLinks", bundle.getLinkSelf().getValue());

    Patient p = bundle.getResources(Patient.class).get(0);
    assertEquals("AAANamed", p.getIdentifierFirstRep().getValue().getValue());
    assertEquals("http://foo/Patient?_id=1", bundle.getEntries().get(0).getLinkSearch().getValue());
    assertEquals(
        "https://blah.com/base/Patient/99881",
        bundle.getEntries().get(0).getLinkAlternate().getValue());
    assertEquals("http://foo/Patient?_id=1", ResourceMetadataKeyEnum.LINK_SEARCH.get(p));
    assertEquals(
        "https://blah.com/base/Patient/99881", ResourceMetadataKeyEnum.LINK_ALTERNATE.get(p));

    String linkNext = bundle.getLinkNext().getValue();
    ourLog.info(linkNext);
    assertThat(linkNext, startsWith("https://blah.com/base?_getpages="));

    /*
     * Load the second page
     */
    String urlPart = linkNext.substring(linkNext.indexOf('?'));
    String link = "http://localhost:" + ourPort + urlPart;
    httpGet = new HttpGet(link);

    status = ourClient.execute(httpGet);
    responseContent = IOUtils.toString(status.getEntity().getContent());
    IOUtils.closeQuietly(status.getEntity().getContent());
    assertEquals(200, status.getStatusLine().getStatusCode());
    bundle = ourCtx.newXmlParser().parseBundle(responseContent);

    ourLog.info(responseContent);

    assertEquals(10, bundle.getEntries().size());
    assertEquals("https://blah.com/base", bundle.getLinkBase().getValue());
    assertEquals(linkNext, bundle.getLinkSelf().getValue());

    p = bundle.getResources(Patient.class).get(0);
    assertEquals("AAANamed", p.getIdentifierFirstRep().getValue().getValue());
    assertEquals(
        "http://foo/Patient?_id=11", bundle.getEntries().get(0).getLinkSearch().getValue());
    assertEquals(
        "https://blah.com/base/Patient/998811",
        bundle.getEntries().get(0).getLinkAlternate().getValue());
    assertEquals("http://foo/Patient?_id=11", ResourceMetadataKeyEnum.LINK_SEARCH.get(p));
    assertEquals(
        "https://blah.com/base/Patient/998811", ResourceMetadataKeyEnum.LINK_ALTERNATE.get(p));
  }