From ae6128c7462298fe577656424fe24d35117736b9 Mon Sep 17 00:00:00 2001 From: apurvabhaleMS <86023331+apurvabhaleMS@users.noreply.github.com> Date: Fri, 29 Mar 2024 08:44:33 -0700 Subject: [PATCH] Handle 405 errors (#3777) * Handle 405 error * Updated the diagnosticInfo * Added unit test * Updated tests --- src/Microsoft.Health.Fhir.Api/Resources.Designer.cs | 9 +++++++++ src/Microsoft.Health.Fhir.Api/Resources.resx | 3 +++ .../Controllers/FhirController.cs | 5 +++++ .../Rest/ExceptionTests.cs | 13 +++++++++++++ 4 files changed, 30 insertions(+) diff --git a/src/Microsoft.Health.Fhir.Api/Resources.Designer.cs b/src/Microsoft.Health.Fhir.Api/Resources.Designer.cs index b287622c85..e681303db3 100644 --- a/src/Microsoft.Health.Fhir.Api/Resources.Designer.cs +++ b/src/Microsoft.Health.Fhir.Api/Resources.Designer.cs @@ -564,6 +564,15 @@ public static string OperationNotImplemented { } } + /// + /// Looks up a localized string similar to The requested operation is not supported.. + /// + public static string OperationNotSupported { + get { + return ResourceManager.GetString("OperationNotSupported", resourceCulture); + } + } + /// /// Looks up a localized string similar to FHIR Server. /// diff --git a/src/Microsoft.Health.Fhir.Api/Resources.resx b/src/Microsoft.Health.Fhir.Api/Resources.resx index c53c8f73d4..34a5701129 100644 --- a/src/Microsoft.Health.Fhir.Api/Resources.resx +++ b/src/Microsoft.Health.Fhir.Api/Resources.resx @@ -182,6 +182,9 @@ The requested "{0}" operation is not supported. {0} is the operation name + + The requested operation is not supported. + FHIR Server {NumberedPlaceHolder="FHIR"} diff --git a/src/Microsoft.Health.Fhir.Shared.Api/Controllers/FhirController.cs b/src/Microsoft.Health.Fhir.Shared.Api/Controllers/FhirController.cs index d2bb4d821f..c50af53fde 100644 --- a/src/Microsoft.Health.Fhir.Shared.Api/Controllers/FhirController.cs +++ b/src/Microsoft.Health.Fhir.Shared.Api/Controllers/FhirController.cs @@ -126,6 +126,11 @@ public IActionResult CustomError(int? statusCode = null) returnCode = HttpStatusCode.NotFound; diagnosticInfo = Resources.NotFoundException; break; + case (int)HttpStatusCode.MethodNotAllowed: + issueType = OperationOutcome.IssueType.NotSupported; + returnCode = HttpStatusCode.MethodNotAllowed; + diagnosticInfo = Resources.OperationNotSupported; + break; default: issueType = OperationOutcome.IssueType.Exception; returnCode = HttpStatusCode.InternalServerError; diff --git a/test/Microsoft.Health.Fhir.Shared.Tests.E2E/Rest/ExceptionTests.cs b/test/Microsoft.Health.Fhir.Shared.Tests.E2E/Rest/ExceptionTests.cs index 6966f4c0a3..3d41ee037f 100644 --- a/test/Microsoft.Health.Fhir.Shared.Tests.E2E/Rest/ExceptionTests.cs +++ b/test/Microsoft.Health.Fhir.Shared.Tests.E2E/Rest/ExceptionTests.cs @@ -5,6 +5,7 @@ using System; using System.Net; +using System.Net.Http; using Hl7.Fhir.Model; using Hl7.Fhir.Validation; using Microsoft.AspNetCore.Builder; @@ -102,6 +103,18 @@ public async Task GivenAnUnknownRoute_WhenPostingToHttp_TheServerShouldReturnAnO DotNetAttributeValidation.Validate(operationOutcome, true); } + [Fact] + [Trait(Traits.Priority, Priority.One)] + public async Task GivenANotAllowedMethod_WhenRequestIsSent_TheServerShouldReturnMethodNotSupported() + { + using var requestMessage = new HttpRequestMessage(); + string uriString = _client.HttpClient.BaseAddress + "admin.html"; + requestMessage.RequestUri = new Uri(uriString); + requestMessage.Method = HttpMethod.Head; + HttpResponseMessage response = await _client.HttpClient.SendAsync(requestMessage); + Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode); + } + public class StartupWithThrowingMiddleware : StartupBaseForCustomProviders { public StartupWithThrowingMiddleware(IConfiguration configuration)