Skip to content

Commit

Permalink
Merge pull request #2805 from FirelyTeam/feature/2795-results-from-re…
Browse files Browse the repository at this point in the history
…sourcebase-and-resourceidentity

Fixed base url for FhirClient resources
  • Loading branch information
ewoutkramer authored Jun 27, 2024
2 parents 40904d3 + ebbf120 commit 0f8c753
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Hl7.Fhir.Base/Rest/HttpContentParsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ internal static async Task<ResponseData> ExtractResponseData(this HttpResponseMe
}

// Sets the Resource.ResourceBase to the location given in the RequestUri of the response message.
if (result.BodyResource is not null && message.GetRequestUri()?.OriginalString is string location)
if (result.BodyResource is not null && (message.Headers.Location?.OriginalString ?? message.GetRequestUri()?.OriginalString) is {} location)
{
var ri = new ResourceIdentity(location);
result.BodyResource.ResourceBase = ri.HasBaseUri && ri.Form == ResourceIdentityForm.AbsoluteRestUrl
? ResourceIdentity.Build(ri.BaseUri, ri.ResourceType, ri.Id, ri.VersionId)
? ri.BaseUri
: new Uri(location, UriKind.Absolute);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Base/Rest/ResourceIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ private void parseComponents(string url)

if (uri.IsAbsoluteUri)
{
var baseUri = url.Substring(0, url.IndexOf("/" + ResourceType + "/")).EnsureEndsWith("/");
var baseUri = url.Substring(0, url.IndexOf("/" + ResourceType + "/", StringComparison.Ordinal)).EnsureEndsWith("/");
BaseUri = new Uri(baseUri, UriKind.Absolute);
}

Expand Down
42 changes: 42 additions & 0 deletions src/Hl7.Fhir.Shared.Tests/Rest/FhirClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Tasks = System.Threading.Tasks;
Expand Down Expand Up @@ -911,6 +912,47 @@ public async Tasks.Task TestBinaryOperations()
var read = await client.ReadAsync<Binary>(created.ResourceIdentity());
read.Should().BeOfType(typeof(Binary));
}

[TestMethod]
[TestCategory("IntegrationTest"), TestCategory("FhirClient")]
public async Tasks.Task FhirClient_ResourceBase_ResourceIdentity_ShouldBeConsistent()
{
var client = new FhirClient(TestEndpoint);
var patient = new Patient()
{
BirthDate = "1972-11-30",
Name = new List<HumanName>()
{
new HumanName()
{
Given = new List<string>() {"test_given"},
Family = "Donald",
}
},
Identifier = new List<Identifier>()
{
new Identifier()
{
System = "urn:oid:1.2.36.146.595.217.0.1",
Value = "12345"
}
}
};

Console.WriteLine("Unsaved Patient");
patient.ResourceBase.Should().BeNull();
patient.ResourceIdentity().Should().BeNull();

Console.WriteLine("Saved Patient");
var newPatient = await client.CreateAsync(patient);
newPatient?.ResourceBase.Should().Be(TestEndpoint + "/");
newPatient?.ResourceIdentity().ToString().Should().Be(TestEndpoint + "/Patient/" + newPatient.Id + "/_history/" + newPatient.Meta.VersionId);

Console.WriteLine("Read Patient");
var patientGet = await client.ReadAsync<Patient>($"Patient/{newPatient.Id}");
patientGet?.ResourceBase.Should().Be(TestEndpoint + "/");
patientGet?.ResourceIdentity().ToString().Should().Be(TestEndpoint + "/Patient/" + patientGet.Id + "/_history/" + patientGet.Meta.VersionId);
}
}

internal class TestDeligatingHandler : DelegatingHandler
Expand Down
5 changes: 3 additions & 2 deletions src/Hl7.Fhir.Support.Poco.Tests/Rest/FhirClientMockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,15 @@ public async Task LocationHeaderTest()
RequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://example.com/Patient?name=henry"),
};

response.Headers.Add("Location", "/fhir/*/Bundle/example");
response.Headers.Add("Location", "https://example.com/fhir/Bundle/example");

using var client = new SubstituteBuilder()
.Send(response, h => h.RequestUri == new Uri("http://example.com/Patient?name=henry"))
.AsClient();
var patient = await client.SearchAsync<Patient>(new string[] { "name=henry" });

client.LastResult!.Location.Should().Be("/fhir/*/Bundle/example");
client.LastResult!.Location.Should().Be("https://example.com/fhir/Bundle/example");
patient!.ResourceBase.Should().Be(new Uri("https://example.com/fhir/"));
}

[DataTestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void GetVersionFromETagReturnsVersionOnly()
}

private const string DEFAULT_XML = "<Patient xmlns=\"http://hl7.org/fhir\"><active value=\"true\" /></Patient>";
private static readonly Uri REQUEST_URI = new("http://server.nl/fhir/SomeResource/1", UriKind.Absolute);
private static readonly Uri REQUEST_URI = new("http://server.nl/fhir/", UriKind.Absolute);
private static HttpContent makeXmlContent(string? xml = null) =>
new StringContent(xml ?? DEFAULT_XML, Encoding.UTF8, ContentType.XML_CONTENT_HEADER);
private static HttpResponseMessage makeXmlMessage(HttpStatusCode status = HttpStatusCode.OK, string? xml = null) =>
Expand Down

0 comments on commit 0f8c753

Please sign in to comment.