Allow HTTP 204 response to transaction (#2051)
* Allow HTTP 204 response to transaction * Add changelog * Test fix
This commit is contained in:
parent
a0710d4f47
commit
dbbff1fd16
|
@ -567,6 +567,10 @@ public abstract class BaseClient implements IRestfulClient {
|
|||
|
||||
@Override
|
||||
public T invokeClient(String theResponseMimeType, InputStream theResponseInputStream, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws BaseServerResponseException {
|
||||
if (theResponseStatusCode == Constants.STATUS_HTTP_204_NO_CONTENT) {
|
||||
return null;
|
||||
}
|
||||
|
||||
EncodingEnum respType = EncodingEnum.forContentType(theResponseMimeType);
|
||||
if (respType == null) {
|
||||
if (myAllowHtmlResponse && theResponseMimeType.toLowerCase().contains(Constants.CT_HTML) && myReturnType != null) {
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
type: add
|
||||
issue: 2051
|
||||
title: The FHIR Client will now accept an HTTP 204 NO CONTENT as a response to a write operation such
|
||||
as a create/update/transaction/etc.
|
|
@ -2397,33 +2397,6 @@ 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(Constants.STATUS_HTTP_204_NO_CONTENT, e.getStatusCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateConditional() throws Exception {
|
||||
ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class);
|
||||
|
|
|
@ -25,6 +25,7 @@ import ca.uhn.fhir.rest.param.DateRangeParam;
|
|||
import ca.uhn.fhir.rest.param.ParamPrefixEnum;
|
||||
import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException;
|
||||
import ca.uhn.fhir.rest.server.exceptions.UnclassifiedServerFailureException;
|
||||
import ca.uhn.fhir.util.TransactionBuilder;
|
||||
import ca.uhn.fhir.util.UrlUtil;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.helger.commons.io.stream.StringInputStream;
|
||||
|
@ -36,6 +37,7 @@ import org.apache.http.client.methods.HttpPut;
|
|||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.message.BasicStatusLine;
|
||||
import org.hl7.fhir.instance.model.api.IBaseBundle;
|
||||
import org.hl7.fhir.instance.model.api.IPrimitiveType;
|
||||
import org.hl7.fhir.r4.model.Binary;
|
||||
import org.hl7.fhir.r4.model.BooleanType;
|
||||
|
@ -2106,6 +2108,31 @@ public class GenericClientR4Test extends BaseGenericClientR4Test {
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransactionWithResponseHttp204NoContent() throws IOException {
|
||||
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
|
||||
|
||||
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), 204, "No Content"));
|
||||
when(myHttpResponse.getEntity()).thenReturn(null);
|
||||
when(myHttpResponse.getAllHeaders()).thenAnswer(new Answer<Header[]>() {
|
||||
@Override
|
||||
public Header[] answer(InvocationOnMock theInvocation) {
|
||||
return new Header[0];
|
||||
}
|
||||
});
|
||||
|
||||
TransactionBuilder builder = new TransactionBuilder(ourCtx);
|
||||
builder.addCreateEntry(new Patient().setActive(true));
|
||||
|
||||
IBaseBundle outcome = client.transaction().withBundle(builder.getBundle()).execute();
|
||||
assertNull(outcome);
|
||||
|
||||
assertEquals("http://example.com/fhir", capt.getAllValues().get(0).getURI().toASCIIString());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateById() throws Exception {
|
||||
IParser p = ourCtx.newXmlParser();
|
||||
|
|
Loading…
Reference in New Issue