Add test for #192

This commit is contained in:
James Agnew 2015-07-03 13:59:28 -04:00
parent 2d05b48bdc
commit ea2bb36950
1 changed files with 52 additions and 0 deletions

View File

@ -25,6 +25,7 @@ import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicStatusLine; import org.apache.http.message.BasicStatusLine;
import org.hamcrest.Matchers;
import org.hamcrest.core.StringContains; import org.hamcrest.core.StringContains;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -55,6 +56,7 @@ import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.rest.server.EncodingEnum; import ca.uhn.fhir.rest.server.EncodingEnum;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.util.UrlUtil;
public class GenericClientTest { public class GenericClientTest {
@ -979,6 +981,56 @@ public class GenericClientTest {
} }
/**
* Test for #192
*/
@SuppressWarnings("unused")
@Test
public void testSearchByTokenWithEscaping() throws Exception {
final String msg = getPatientFeedWithOneResult();
ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class);
when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"));
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer<InputStream>() {
@Override
public InputStream answer(InvocationOnMock theInvocation) throws Throwable {
return new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"));
}});
IGenericClient client = ourCtx.newRestfulGenericClient("http://foo");
int index = 0;
String wantPrefix = "http://foo/Patient?identifier=";
//@formatter:off
Bundle response = client.search()
.forResource("Patient")
.where(Patient.IDENTIFIER.exactly().systemAndCode("1", "2"))
.execute();
String wantValue = "1|2";
String url = capt.getAllValues().get(index).getURI().toString();
assertThat(url, Matchers.startsWith(wantPrefix));
assertEquals(wantValue, UrlUtil.unescape(url.substring(wantPrefix.length())));
assertEquals(UrlUtil.escape(wantValue), url.substring(wantPrefix.length()));
index++;
//@formatter:on
//@formatter:off
response = client.search()
.forResource("Patient")
.where(Patient.IDENTIFIER.exactly().systemAndCode("1,2", "3,4"))
.execute();
wantValue = "1\\,2|3\\,4";
url = capt.getAllValues().get(index).getURI().toString();
assertThat(url, Matchers.startsWith(wantPrefix));
assertEquals(wantValue, UrlUtil.unescape(url.substring(wantPrefix.length())));
assertEquals(UrlUtil.escape(wantValue), url.substring(wantPrefix.length()));
index++;
//@formatter:on
}
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Test @Test
public void testSearchUsingGetSearch() throws Exception { public void testSearchUsingGetSearch() throws Exception {