Skip to content

Commit 83b7f46

Browse files
jkiddojamesagnew
andauthored
Feat/support head metadata (#6406)
* Added support for HTTP HEAD * Renamed file to issue number and added issue number * Moved to 7.6 * Adjusted to new init method * Corrected error messages * Moved to 7.8.0 as it isn't urgent * Moved to 8.2.0 * Added HEAD test for stu3 * Added HEAD test * Adjusted according to review comments * Spotless formatting * Update MetadataCapabilityStatementDstu3Test.java * Update MetadataCapabilityStatementDstu3Test.java --------- Co-authored-by: James Agnew <[email protected]>
1 parent 0b0d0e3 commit 83b7f46

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
type: add
3+
issue: 6152
4+
title: "Added the option to do HTTP HEAD requests against /metadata. Thanks to Jens Villadsen (@jkiddo) for the contribution!"

hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/ConformanceMethodBinding.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ public MethodMatchEnum incomingServerRequestMatchesMethod(RequestDetails theRequ
220220
}
221221

222222
if ("metadata".equals(theRequest.getOperation())) {
223-
if (theRequest.getRequestType() == RequestTypeEnum.GET) {
223+
if (theRequest.getRequestType() == RequestTypeEnum.GET
224+
|| theRequest.getRequestType() == RequestTypeEnum.HEAD) {
224225
return MethodMatchEnum.EXACT;
225226
}
226-
throw new MethodNotAllowedException(
227-
Msg.code(388) + "/metadata request must use HTTP GET", RequestTypeEnum.GET);
227+
throw new MethodNotAllowedException(Msg.code(388) + "/metadata request must use HTTP GET or HTTP HEAD");
228228
}
229229

230230
return MethodMatchEnum.NONE;

hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/method/ConformanceMethodBindingTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ca.uhn.fhir.context.FhirContext;
44
import ca.uhn.fhir.rest.annotation.Metadata;
55
import ca.uhn.fhir.rest.api.Constants;
6+
import ca.uhn.fhir.rest.api.RequestTypeEnum;
67
import ca.uhn.fhir.rest.api.server.IRestfulServer;
78
import ca.uhn.fhir.rest.api.server.RequestDetails;
89
import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
@@ -12,12 +13,15 @@
1213
import org.junit.jupiter.api.BeforeEach;
1314
import org.junit.jupiter.api.Test;
1415
import org.junit.jupiter.api.extension.ExtendWith;
16+
import org.junit.jupiter.params.ParameterizedTest;
17+
import org.junit.jupiter.params.provider.EnumSource;
1518
import org.mockito.Answers;
1619
import org.mockito.Mock;
1720
import org.mockito.junit.jupiter.MockitoExtension;
1821

1922
import java.lang.reflect.Method;
2023

24+
import static org.junit.jupiter.api.Assertions.assertEquals;
2125
import static org.mockito.ArgumentMatchers.any;
2226
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
2327
import static org.mockito.Mockito.mock;
@@ -106,6 +110,21 @@ public void invokeServerNotCached_ClientControlled() throws NoSuchMethodExceptio
106110
verify(provider, times(2)).getServerConformance(any(), any());
107111
}
108112

113+
114+
@ParameterizedTest
115+
@EnumSource(
116+
value = RequestTypeEnum.class,
117+
names = {"GET", "HEAD"})
118+
public void invokeServer_metadata(RequestTypeEnum requestTypeEnum) throws NoSuchMethodException {
119+
init(new TestResourceProvider());
120+
121+
RequestDetails requestDetails = mySrd;
122+
when(requestDetails.getOperation()).thenReturn("metadata");
123+
when(requestDetails.getRequestType()).thenReturn(requestTypeEnum);
124+
assertEquals(conformanceMethodBinding.incomingServerRequestMatchesMethod(requestDetails), MethodMatchEnum.EXACT);
125+
126+
}
127+
109128
@SuppressWarnings("unused")
110129
static class TestResourceProvider {
111130

hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/MetadataCapabilityStatementDstu3Test.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.apache.commons.io.IOUtils;
1919
import org.apache.http.client.methods.CloseableHttpResponse;
2020
import org.apache.http.client.methods.HttpGet;
21+
import org.apache.http.client.methods.HttpHead;
2122
import org.apache.http.client.methods.HttpPost;
2223
import org.apache.http.client.methods.HttpRequestBase;
2324
import org.hl7.fhir.dstu3.hapi.rest.server.ServerCapabilityStatementProvider;
@@ -96,7 +97,7 @@ public void testHttpMethods() throws Exception {
9697
status = ourClient.execute(httpPost);
9798
output = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
9899
assertEquals(405, status.getStatusLine().getStatusCode());
99-
assertEquals("<OperationOutcome xmlns=\"http://hl7.org/fhir\"><issue><severity value=\"error\"/><code value=\"processing\"/><diagnostics value=\"" + Msg.code(388) + "/metadata request must use HTTP GET\"/></issue></OperationOutcome>", output);
100+
assertEquals("<OperationOutcome xmlns=\"http://hl7.org/fhir\"><issue><severity value=\"error\"/><code value=\"processing\"/><diagnostics value=\"" + Msg.code(388) + "/metadata request must use HTTP GET or HTTP HEAD\"/></issue></OperationOutcome>", output);
100101
} finally {
101102
IOUtils.closeQuietly(status.getEntity().getContent());
102103
}
@@ -133,6 +134,15 @@ public void testResponseContainsBaseUrl() throws Exception {
133134
}
134135
}
135136

137+
@Test
138+
public void testHeadElements() throws Exception {
139+
140+
HttpRequestBase httpHead = new HttpHead(ourServer.getBaseUrl() + "/metadata?_elements=fhirVersion&_pretty=true");
141+
CloseableHttpResponse status = ourClient.execute(httpHead);
142+
status.getAllHeaders();
143+
assertEquals(200, status.getStatusLine().getStatusCode());
144+
}
145+
136146
@Test
137147
public void testResponseContainsBaseUrlFixed() throws Exception {
138148
ourServer.setServerAddressStrategy(new HardcodedServerAddressStrategy("http://foo/bar"));

hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/MetadataConformanceDstu3Test.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.apache.commons.io.IOUtils;
1818
import org.apache.http.client.methods.CloseableHttpResponse;
1919
import org.apache.http.client.methods.HttpGet;
20+
import org.apache.http.client.methods.HttpHead;
2021
import org.apache.http.client.methods.HttpOptions;
2122
import org.apache.http.client.methods.HttpPost;
2223
import org.apache.http.client.methods.HttpRequestBase;
@@ -29,6 +30,7 @@
2930
import java.util.List;
3031

3132
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.junit.jupiter.api.Assertions.assertNull;
3234

3335
public class MetadataConformanceDstu3Test {
3436

@@ -121,7 +123,13 @@ public void testHttpMethods() throws Exception {
121123
try (CloseableHttpResponse status = ourClient.execute(httpOperation)) {
122124
output = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
123125
assertEquals(405, status.getStatusLine().getStatusCode());
124-
assertEquals("<OperationOutcome xmlns=\"http://hl7.org/fhir\"><issue><severity value=\"error\"/><code value=\"processing\"/><diagnostics value=\"" + Msg.code(388) + "/metadata request must use HTTP GET\"/></issue></OperationOutcome>", output);
126+
assertEquals("<OperationOutcome xmlns=\"http://hl7.org/fhir\"><issue><severity value=\"error\"/><code value=\"processing\"/><diagnostics value=\"" + Msg.code(388) + "/metadata request must use HTTP GET or HTTP HEAD\"/></issue></OperationOutcome>", output);
127+
}
128+
129+
httpOperation = new HttpHead(ourServer.getBaseUrl() + "/metadata");
130+
try (CloseableHttpResponse status = ourClient.execute(httpOperation)) {
131+
assertEquals(200, status.getStatusLine().getStatusCode());
132+
assertNull(status.getEntity());
125133
}
126134

127135
/*

0 commit comments

Comments
 (0)