99using System . Net ;
1010using System . Threading . Tasks ;
1111using Hl7 . Fhir . Model ;
12- using Hl7 . Fhir . Serialization ;
1312using Microsoft . Health . Core . Extensions ;
1413using Microsoft . Health . Extensions . Xunit ;
1514using Microsoft . Health . Fhir . Client ;
@@ -34,7 +33,9 @@ public class CustomSearchParamTests : SearchTestsBase<HttpIntegrationTestFixture
3433 {
3534 private readonly HttpIntegrationTestFixture _fixture ;
3635 private readonly ITestOutputHelper _output ;
36+ private const int MaxAllowedUrlLength = 128 ;
3737 private const int MaxRetryCount = 10 ;
38+ private const string UrlLengthValidationMessage = "exceeds the maximum length limit of 128" ;
3839
3940 public CustomSearchParamTests ( HttpIntegrationTestFixture fixture , ITestOutputHelper output )
4041 : base ( fixture )
@@ -69,5 +70,61 @@ public async Task GivenAnInvalidSearchParam_WhenCreatingParam_ThenMeaningfulErro
6970 Assert . Contains ( ex . OperationOutcome . Issue , i => i . Diagnostics . Contains ( errorMessage ) ) ;
7071 }
7172 }
73+
74+ [ Fact ]
75+ public async Task GivenASearchParameterWithUrlLongerThan128_WhenCreating_ThenValidationErrorReturned ( )
76+ {
77+ SearchParameter searchParam = CreateCustomSearchParameter ( MaxAllowedUrlLength + 1 ) ;
78+
79+ using FhirClientException exception = await Assert . ThrowsAsync < FhirClientException > ( ( ) => Client . CreateAsync ( searchParam ) ) ;
80+
81+ Assert . Contains ( exception . OperationOutcome . Issue , issue => issue . Diagnostics . Contains ( UrlLengthValidationMessage ) ) ;
82+ }
83+
84+ [ Fact ]
85+ public async Task GivenAnExistingSearchParameter_WhenUpdatingWithUrlLongerThan128_ThenValidationErrorReturned ( )
86+ {
87+ SearchParameter searchParam = CreateCustomSearchParameter ( ) ;
88+
89+ using FhirResponse < SearchParameter > createResponse = await Client . UpdateAsync ( searchParam ) ;
90+ Assert . Equal ( HttpStatusCode . Created , createResponse . StatusCode ) ;
91+
92+ searchParam . Url = CreateCustomSearchParameter ( MaxAllowedUrlLength + 1 ) . Url ;
93+
94+ using FhirClientException exception = await Assert . ThrowsAsync < FhirClientException > ( ( ) => Client . UpdateAsync ( searchParam ) ) ;
95+
96+ Assert . Contains ( exception . OperationOutcome . Issue , issue => issue . Diagnostics . Contains ( UrlLengthValidationMessage ) ) ;
97+ }
98+
99+ private static SearchParameter CreateCustomSearchParameter ( int urlLength = MaxAllowedUrlLength )
100+ {
101+ string suffix = Guid . NewGuid ( ) . ToString ( "N" ) ;
102+ const string prefix = "http://example.org/fhir/SearchParameter/" ;
103+
104+ #if R5
105+ var baseResourceTypes = new List < VersionIndependentResourceTypesAll ? >
106+ {
107+ VersionIndependentResourceTypesAll . Patient ,
108+ } ;
109+ #else
110+ var baseResourceTypes = new List < ResourceType ? >
111+ {
112+ ResourceType . Patient ,
113+ } ;
114+ #endif
115+
116+ return new SearchParameter
117+ {
118+ Id = $ "custom-search-param-{ suffix [ ..8 ] } ",
119+ Url = prefix + new string ( 'a' , urlLength - prefix . Length ) ,
120+ Name = $ "CustomSearchParam{ suffix [ ..8 ] } ",
121+ Status = PublicationStatus . Draft ,
122+ Description = new Markdown ( "Custom search parameter used for E2E URL validation tests." ) ,
123+ Code = $ "customparam{ suffix [ ..8 ] } ",
124+ Base = baseResourceTypes ,
125+ Type = SearchParamType . String ,
126+ Expression = "Patient.name" ,
127+ } ;
128+ }
72129 }
73130}
0 commit comments