From f50c0836806386677b6bfa01de840cc650654c7f Mon Sep 17 00:00:00 2001 From: Jens Kristian Villadsen Date: Thu, 25 Jul 2024 14:16:33 +0200 Subject: [PATCH] Added support for HTTP HEAD --- .../7_4_0/xxxx-add-support-for-head-request.yaml | 4 ++++ .../server/method/ConformanceMethodBinding.java | 5 +++-- .../method/ConformanceMethodBindingTest.java | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_4_0/xxxx-add-support-for-head-request.yaml diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_4_0/xxxx-add-support-for-head-request.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_4_0/xxxx-add-support-for-head-request.yaml new file mode 100644 index 00000000000..79ebac4b7b5 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_4_0/xxxx-add-support-for-head-request.yaml @@ -0,0 +1,4 @@ +--- +type: add +issue: xxxx +title: "Added the option to do HTTP HEAD requests against /metadata. Thanks to Jens Villadsen (@jkiddo) for the contribution!" diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/ConformanceMethodBinding.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/ConformanceMethodBinding.java index c45988fe4c8..f9bc3bacec2 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/ConformanceMethodBinding.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/ConformanceMethodBinding.java @@ -220,11 +220,12 @@ public class ConformanceMethodBinding extends BaseResourceReturningMethodBinding } if ("metadata".equals(theRequest.getOperation())) { - if (theRequest.getRequestType() == RequestTypeEnum.GET) { + if (theRequest.getRequestType() == RequestTypeEnum.GET + || theRequest.getRequestType() == RequestTypeEnum.HEAD) { return MethodMatchEnum.EXACT; } throw new MethodNotAllowedException( - Msg.code(388) + "/metadata request must use HTTP GET", RequestTypeEnum.GET); + Msg.code(388) + "/metadata request must use HTTP GET or HTTP HEAD", RequestTypeEnum.GET); } return MethodMatchEnum.NONE; diff --git a/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/method/ConformanceMethodBindingTest.java b/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/method/ConformanceMethodBindingTest.java index 08d465a99cf..09239f5ad12 100644 --- a/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/method/ConformanceMethodBindingTest.java +++ b/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/method/ConformanceMethodBindingTest.java @@ -3,6 +3,7 @@ package ca.uhn.fhir.rest.server.method; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.Metadata; import ca.uhn.fhir.rest.api.Constants; +import ca.uhn.fhir.rest.api.RequestTypeEnum; import ca.uhn.fhir.rest.api.server.IRestfulServer; import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; @@ -18,6 +19,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.lang.reflect.Method; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; @@ -106,6 +108,18 @@ public class ConformanceMethodBindingTest { verify(provider, times(2)).getServerConformance(any(), any()); } + @Test + public void invokeServer_metadata() { + + RequestDetails requestDetails = mySrd; + when(requestDetails.getOperation()).thenReturn("metadata"); + when(requestDetails.getRequestType()).thenReturn(RequestTypeEnum.GET); + assertEquals(conformanceMethodBinding.incomingServerRequestMatchesMethod(requestDetails), MethodMatchEnum.EXACT); + when(requestDetails.getRequestType()).thenReturn(RequestTypeEnum.HEAD); + assertEquals(conformanceMethodBinding.incomingServerRequestMatchesMethod(requestDetails), MethodMatchEnum.EXACT); + + } + @SuppressWarnings("unused") static class TestResourceProvider {