Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed base url for FhirClient resources #2805

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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