[(fixclienthandle204)] Fixed handling 204 response which has no body in hapiclient.

This commit is contained in:
Petro Mykhaylyshyn 2019-11-23 18:56:22 +02:00 committed by James Agnew
parent 708b683928
commit 89c1a15aa4
2 changed files with 34 additions and 0 deletions

View File

@ -375,6 +375,10 @@ public abstract class BaseClient implements IRestfulClient {
}
}
if (inputStreamToReturn == null) {
inputStreamToReturn = new ByteArrayInputStream(new byte[]{});
}
return binding.invokeClient(mimeType, inputStreamToReturn, response.getStatus(), headers);
}

View File

@ -14,6 +14,8 @@ import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Link;
import ca.uhn.fhir.model.dstu2.resource.Conformance.Rest;
import ca.uhn.fhir.model.dstu2.resource.Conformance.RestSecurity;
import ca.uhn.fhir.model.dstu2.valueset.BundleTypeEnum;
import ca.uhn.fhir.model.dstu2.valueset.HTTPVerbEnum;
import ca.uhn.fhir.model.primitive.*;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.parser.IParser;
@ -22,6 +24,7 @@ import ca.uhn.fhir.rest.api.*;
import ca.uhn.fhir.rest.client.apache.ApacheRestfulClientFactory;
import ca.uhn.fhir.rest.client.api.*;
import ca.uhn.fhir.rest.client.exceptions.InvalidResponseException;
import ca.uhn.fhir.rest.client.exceptions.NonFhirResponseException;
import ca.uhn.fhir.rest.client.impl.BaseClient;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.rest.param.DateParam;
@ -2360,6 +2363,33 @@ public class GenericClientDstu2Test {
assertEquals("Patient/2/_history/2", response.getEntry().get(1).getResponse().getLocation());
}
@Test
public void testTransactionHandle204NoBody() throws Exception {
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
ca.uhn.fhir.model.dstu2.resource.Bundle bundle = new Bundle();
bundle.setType(BundleTypeEnum.TRANSACTION);
ca.uhn.fhir.model.dstu2.resource.Bundle.Entry entry = bundle.addEntry();
entry.setResource(new Patient());
entry.getRequest().setMethod(HTTPVerbEnum.PUT);
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), Constants.STATUS_HTTP_204_NO_CONTENT, ""));
when(myHttpResponse.getEntity() ).thenReturn(null);
try {
client.transaction().withBundle(bundle).execute();
fail("Should throw an exception");
} catch (NonFhirResponseException e) {
assertEquals("status", Constants.STATUS_HTTP_204_NO_CONTENT, e.getStatusCode());
}
}
@Test
public void testUpdateConditional() throws Exception {
ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class);