From 89c1a15aa42160792910dc0538cceaba69f91c3f Mon Sep 17 00:00:00 2001 From: Petro Mykhaylyshyn Date: Sat, 23 Nov 2019 18:56:22 +0200 Subject: [PATCH] [(fixclienthandle204)] Fixed handling 204 response which has no body in hapiclient. --- .../uhn/fhir/rest/client/impl/BaseClient.java | 4 +++ .../rest/client/GenericClientDstu2Test.java | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/hapi-fhir-client/src/main/java/ca/uhn/fhir/rest/client/impl/BaseClient.java b/hapi-fhir-client/src/main/java/ca/uhn/fhir/rest/client/impl/BaseClient.java index 82b5d196a5c..ee000bd9baa 100644 --- a/hapi-fhir-client/src/main/java/ca/uhn/fhir/rest/client/impl/BaseClient.java +++ b/hapi-fhir-client/src/main/java/ca/uhn/fhir/rest/client/impl/BaseClient.java @@ -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); } diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu2Test.java index 56b6d378d2f..0622c981494 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu2Test.java @@ -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 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 capt = ArgumentCaptor.forClass(HttpUriRequest.class);