Skip to content

Commit

Permalink
Exception handled for invalid HTTPVerb with unit testing. (#4714)
Browse files Browse the repository at this point in the history
Co-authored-by: Alankar Jagtap <[email protected]>
  • Loading branch information
v-aljagtap and Alankar Jagtap authored Dec 12, 2024
1 parent cb53a35 commit 1c2f46c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Microsoft.Health.Fhir.Api/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/Microsoft.Health.Fhir.Api/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,7 @@
<data name="MissingInputParams" xml:space="preserve">
<value>inputParams not found, request body must be a valid Parameters resource.</value>
</data>
</root>
<data name="InvalidBundleEntryRequest" xml:space="preserve">
<value>Requested operation '{0}' is invalid using the request method {1}.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,33 @@ public async Task GivenATransactionBundleRequestWithNullUrl_WhenProcessing_Retur
await Assert.ThrowsAsync<RequestNotValidException>(async () => await _bundleHandler.Handle(bundleRequest, default));
}

[Fact]
public async Task GivenATransactionBundleRequestWithNullRequestMethod_WhenProcessing_ReturnsABadRequest()
{
var bundle = new Hl7.Fhir.Model.Bundle
{
Type = BundleType.Transaction,
Entry = new List<EntryComponent>
{
new EntryComponent
{
Request = new RequestComponent
{
Method = null,
Url = "/Patient",
},
Resource = new Basic { Id = "test"},
},
},
};

_router.When(r => r.RouteAsync(Arg.Any<RouteContext>()))
.Do(RouteAsyncFunction);

var bundleRequest = new BundleRequest(bundle.ToResourceElement());
await Assert.ThrowsAsync<RequestNotValidException>(async () => await _bundleHandler.Handle(bundleRequest, default));
}

[Fact]
public async Task GivenABundle_WhenProcessed_CertainResponseHeadersArePropagatedToOuterResponse()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ private async Task<string> GetResourceId(EntryComponent entry, IDictionary<strin
private static bool ShouldValidateBundleEntry(EntryComponent entry)
{
string requestUrl = entry.Request?.Url;
var requestVerb = entry.Request?.MethodElement?.ObjectValue as string;
if (string.IsNullOrWhiteSpace(requestVerb) || !Enum.IsDefined(typeof(HTTPVerb), requestVerb))
{
throw new RequestNotValidException(string.Format(Api.Resources.InvalidBundleEntryRequest, entry.Request.Url, requestVerb));
}

HTTPVerb? requestMethod = entry.Request?.Method;

if (string.IsNullOrWhiteSpace(requestUrl))
Expand Down

0 comments on commit 1c2f46c

Please sign in to comment.