Class SmileTestHttpClient

java.lang.Object
ca.cdr.test.app.clients.common.SmileTestHttpClient
All Implemented Interfaces:
ISmileTestHttpClient, AutoCloseable

public final class SmileTestHttpClient extends Object implements ISmileTestHttpClient, AutoCloseable
An Apache HttpClient 5.x client configured the way Smile's tests need it, bundled with the mutable state that callers have to be able to reach ? its cookie store and its connection pool.

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 Type
    Method
    Description
    void
    Discards every cookie held by this client, ending any session established by prior requests.
    void
    Closes both underlying clients and the connection pool they share.
    ca.uhn.fhir.test.utilities.HttpTestRequest
    cookielessRequest(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.HttpTestRequest
    Starts building a request that neither sends nor stores cookies, while still using this client's connection pool.
    Builds a new client with its own connection pool and cookie store.
    org.apache.hc.client5.http.impl.classic.CloseableHttpClient
    The underlying Apache client, for callers that still need to build Apache request objects by hand.
    boolean
    Whether close() has been called.
    ca.uhn.fhir.test.utilities.HttpTestRequest
    request(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.HttpTestRequest
    request(String theUrl)
    Starts 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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      @Nonnull public static SmileTestHttpClient create()
      Builds a new client with its own connection pool and cookie store. The caller owns the returned instance and must close() it.
    • getClient

      @Nonnull public org.apache.hc.client5.http.impl.classic.CloseableHttpClient getClient()
      The underlying Apache client, for callers that still need to build Apache request objects by hand. Deliberately absent from ISmileTestHttpClient, 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 with request(String).
    • request

      @Nonnull public ca.uhn.fhir.test.utilities.HttpTestRequest request(@Nonnull String theUrl)
      Description copied from interface: ISmileTestHttpClient
      Starts 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:
      request in interface ISmileTestHttpClient
      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: ISmileTestHttpClient
      Starts building a request that can carry a FHIR resource body, encoded with the given context.
      Specified by:
      request in interface ISmileTestHttpClient
      Parameters:
      theFhirContext - the context used to encode resource bodies
      theUrl - the full request URL
      See Also:
    • cookielessRequest

      @Nonnull public ca.uhn.fhir.test.utilities.HttpTestRequest cookielessRequest(@Nonnull String theUrl)
      Description copied from interface: ISmileTestHttpClient
      Starts 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 with ISmileTestHttpClient.request(String) would replay whatever session an earlier request established, and could be answered as that user.
      Specified by:
      cookielessRequest in interface ISmileTestHttpClient
      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: ISmileTestHttpClient
      Starts building a cookie-free request that can carry a FHIR resource body, encoded with the given context.
      Specified by:
      cookielessRequest in interface ISmileTestHttpClient
      Parameters:
      theFhirContext - the context used to encode resource bodies
      theUrl - the full request URL
      See Also:
    • clearCookies

      public void clearCookies()
      Description copied from interface: ISmileTestHttpClient
      Discards every cookie held by this client, ending any session established by prior requests.
      Specified by:
      clearCookies in interface ISmileTestHttpClient
    • isClosed

      public boolean isClosed()
      Whether close() 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

      public void close()
      Closes both underlying clients and the connection pool they share.
      Specified by:
      close in interface AutoCloseable
      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 @AfterAll would have to declare.