Class SmileTestHttpClient
- All Implemented Interfaces:
ISmileTestHttpClient,AutoCloseable
The configuration is deliberately identical everywhere: generous timeouts (a debugging test should not fail on a socket read), a pool large enough that a test making many concurrent calls does not queue on itself, and redirects disabled so that a test can assert on a 302 and its Location header rather than silently following it.
The cookie store is shared across every request made through request(String), which is
what makes multi-request login flows (SMART/OAuth) work. Tests that need to start a fresh session
call clearCookies().
cookielessRequest(String) is the deliberate exception: it neither sends nor stores
cookies, so a test asserting that a request is rejected without credentials cannot be handed a
pass by a session some earlier request established. Both kinds of request share the same
connection pool.
Code that only needs to issue requests should take ISmileTestHttpClient instead of this
class. That interface omits close() and getClient() and is not
AutoCloseable, so a caller cannot accidentally shut down a pool it does not own.
-
Method Summary
Modifier and TypeMethodDescriptionvoidDiscards every cookie held by this client, ending any session established by prior requests.voidclose()Closes both underlying clients and the connection pool they share.ca.uhn.fhir.test.utilities.HttpTestRequestcookielessRequest(FhirContext theFhirContext, String theUrl) Starts building a cookie-free request that can carry a FHIR resource body, encoded with the given context.ca.uhn.fhir.test.utilities.HttpTestRequestcookielessRequest(String theUrl) Starts building a request that neither sends nor stores cookies, while still using this client's connection pool.static SmileTestHttpClientcreate()Builds a new client with its own connection pool and cookie store.org.apache.hc.client5.http.impl.classic.CloseableHttpClientThe underlying Apache client, for callers that still need to build Apache request objects by hand.booleanisClosed()Whetherclose()has been called.ca.uhn.fhir.test.utilities.HttpTestRequestrequest(FhirContext theFhirContext, String theUrl) Starts building a request that can carry a FHIR resource body, encoded with the given context.ca.uhn.fhir.test.utilities.HttpTestRequestStarts building a request against a full URL, issued on this client so that it shares the cookie store and connection pool with everything else sent through it.
-
Method Details
-
create
Builds a new client with its own connection pool and cookie store. The caller owns the returned instance and mustclose()it. -
getClient
The underlying Apache client, for callers that still need to build Apache request objects by hand. Deliberately absent fromISmileTestHttpClient, because a caller holding this can close the pool out from under every other user of this client. It is the cookie-carrying client, so requests built on it share the session withrequest(String). -
request
Description copied from interface:ISmileTestHttpClientStarts building a request against a full URL, issued on this client so that it shares the cookie store and connection pool with everything else sent through it.- Specified by:
requestin interfaceISmileTestHttpClient- Parameters:
theUrl- the full request URL
-
request
@Nonnull public ca.uhn.fhir.test.utilities.HttpTestRequest request(@Nonnull FhirContext theFhirContext, @Nonnull String theUrl) Description copied from interface:ISmileTestHttpClientStarts building a request that can carry a FHIR resource body, encoded with the given context.- Specified by:
requestin interfaceISmileTestHttpClient- Parameters:
theFhirContext- the context used to encode resource bodiestheUrl- the full request URL- See Also:
-
cookielessRequest
@Nonnull public ca.uhn.fhir.test.utilities.HttpTestRequest cookielessRequest(@Nonnull String theUrl) Description copied from interface:ISmileTestHttpClientStarts building a request that neither sends nor stores cookies, while still using this client's connection pool. Use it to assert what a caller carrying no session gets back: a request built withISmileTestHttpClient.request(String)would replay whatever session an earlier request established, and could be answered as that user.- Specified by:
cookielessRequestin interfaceISmileTestHttpClient- Parameters:
theUrl- the full request URL
-
cookielessRequest
@Nonnull public ca.uhn.fhir.test.utilities.HttpTestRequest cookielessRequest(@Nonnull FhirContext theFhirContext, @Nonnull String theUrl) Description copied from interface:ISmileTestHttpClientStarts building a cookie-free request that can carry a FHIR resource body, encoded with the given context.- Specified by:
cookielessRequestin interfaceISmileTestHttpClient- Parameters:
theFhirContext- the context used to encode resource bodiestheUrl- the full request URL- See Also:
-
clearCookies
Description copied from interface:ISmileTestHttpClientDiscards every cookie held by this client, ending any session established by prior requests.- Specified by:
clearCookiesin interfaceISmileTestHttpClient
-
isClosed
Whetherclose()has been called. A holder that caches this client can use it to tell a live client from one a caller has already shut down. -
close
Closes both underlying clients and the connection pool they share.- Specified by:
closein interfaceAutoCloseable- Throws:
UncheckedIOException- if a client fails to close. Callers are tests, and a failure here is an infrastructure problem rather than a case under test, so it is not a checked exception that every@AfterAllwould have to declare.
-