Add client integration test

This commit is contained in:
James Agnew 2016-06-27 13:43:12 -04:00
parent 8c8b7b8234
commit b587e3695b
3 changed files with 161 additions and 5 deletions

View File

@ -0,0 +1,44 @@
package ca.uhn.fhir.rest.server.interceptor;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ca.uhn.fhir.rest.method.RequestDetails;
import ca.uhn.fhir.rest.server.exceptions.AuthenticationException;
/**
* This interceptor creates verbose server log entries containing the complete request and response payloads.
* <p>
* This interceptor is mainly intended for debugging since it will generate very large log entries and
* could potentially be a security risk since it logs every header and complete payload. Use with caution!
* </p>
*/
public class VerboseLoggingInterceptor extends InterceptorAdapter {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(VerboseLoggingInterceptor.class);
@Override
public boolean incomingRequestPostProcessed(RequestDetails theRequestDetails, HttpServletRequest theRequest, HttpServletResponse theResponse) throws AuthenticationException {
StringBuilder b = new StringBuilder("Incoming request: ");
b.append(theRequest.getMethod());
b.append(" ");
b.append(theRequest.getRequestURL());
b.append("\n");
for (Enumeration<String> headerEnumeration = theRequest.getHeaderNames(); headerEnumeration.hasMoreElements(); ) {
String nextName = headerEnumeration.nextElement();
for (Enumeration<String> valueEnumeration = theRequest.getHeaders(nextName); valueEnumeration.hasMoreElements(); ) {
b.append(" * ").append(nextName).append(": ").append(valueEnumeration.nextElement()).append("\n");
}
}
ourLog.info(b.toString());
return true;
}
}

View File

@ -39,11 +39,6 @@ public class TestDstu1Config extends BaseJavaConfigDstu1 {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public IServerInterceptor securityInterceptor() {
return new PublicSecurityInterceptor();
}
@Bean()
public DaoConfig daoConfig() {
DaoConfig retVal = new DaoConfig();

View File

@ -0,0 +1,117 @@
package ca.uhn.fhir.rest.client.apache;
import static org.junit.Assert.assertEquals;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.dstu2.resource.Bundle;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.rest.annotation.OptionalParam;
import ca.uhn.fhir.rest.annotation.RequiredParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.client.IGenericClient;
import ca.uhn.fhir.rest.param.StringParam;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.RestfulServer;
import ca.uhn.fhir.rest.server.interceptor.LoggingInterceptor;
import ca.uhn.fhir.rest.server.interceptor.VerboseLoggingInterceptor;
import ca.uhn.fhir.util.PortUtil;
import ca.uhn.fhir.util.TestUtil;
public class ApacheClientIntegrationTest {
private static FhirContext ourCtx = FhirContext.forDstu2();
private static String ourLastMethod;
private static StringParam ourLastName;
private static int ourPort;
private static Server ourServer;
private static String ourBase;
@Before
public void before() {
ourLastMethod = null;
ourLastName = null;
}
@Test
public void testSearchWithParam() throws Exception {
IGenericClient client = ourCtx.newRestfulGenericClient(ourBase);
Bundle response = client.search().forResource(Patient.class).where(Patient.NAME.matches().value("FOO")).returnBundle(Bundle.class).execute();
assertEquals("search", ourLastMethod);
assertEquals("FOO", ourLastName.getValue());
assertEquals(1, response.getEntry().size());
assertEquals("123", response.getEntry().get(0).getResource().getIdElement().getIdPart());
}
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer(ourCtx);
servlet.registerInterceptor(new VerboseLoggingInterceptor());
servlet.setResourceProviders(new DummyPatientResourceProvider());
ServletHolder servletHolder = new ServletHolder(servlet);
proxyHandler.addServletWithMapping(servletHolder, "/*");
ourServer.setHandler(proxyHandler);
ourServer.start();
ourBase = "http://localhost:" + ourPort;
}
public static class DummyPatientResourceProvider implements IResourceProvider {
//@formatter:off
@Search()
public List<IBaseResource> search(@OptionalParam(name=Patient.SP_NAME) StringParam theName) {
ourLastMethod = "search";
ourLastName = theName;
List<IBaseResource> retVal = new ArrayList<IBaseResource>();
Patient patient = new Patient();
patient.setId("123");
patient.addName().addGiven("GIVEN");
retVal.add(patient);
return retVal;
}
//@formatter:on
@Override
public Class<? extends IBaseResource> getResourceType() {
return Patient.class;
}
}
}