Class ApacheHttpClient

java.lang.Object
io.karatelabs.http.ApacheHttpClient
All Implemented Interfaces:
HttpClient, Closeable, AutoCloseable, org.apache.hc.core5.http.HttpRequestInterceptor

public class ApacheHttpClient extends Object implements HttpClient, org.apache.hc.core5.http.HttpRequestInterceptor
  • Constructor Details

    • ApacheHttpClient

      public ApacheHttpClient()
  • Method Details

    • apply

      public void apply(KarateConfig config)
      Description copied from interface: HttpClient
      Apply a typed configuration snapshot. KarateConfig is the single source of truth — implementations read every relevant setting via its typed getters and (re)build their internal client state.

      Must be idempotent: an implementation may not tear down or rebuild anything when the settings it cares about are unchanged. This is a requirement rather than an optimisation, because it is what lets callers stop deciding. Config reaches a client from four places — a configure <key> step, a shared-scope call returning, a callee inheriting from its caller, and a cached callonce replaying — and the last three copy a whole config across a scenario boundary with no key to test. Any caller-side shortcut therefore has to be conservative on those paths, which means rebuilding a live client and its connection pool for a config that did not change. Deciding here, against the settings actually in use, is the only place the question can be answered exactly.

      Callers should call this whenever config may have changed and let the implementation sort it out. They must not try to predict the answer.

      Specified by:
      apply in interface HttpClient
    • invoke

      public HttpResponse invoke(HttpRequest request)
      Specified by:
      invoke in interface HttpClient
    • abort

      public void abort()
      Specified by:
      abort in interface HttpClient
    • process

      public void process(org.apache.hc.core5.http.HttpRequest hr, org.apache.hc.core5.http.EntityDetails entity, org.apache.hc.core5.http.protocol.HttpContext context)
      Specified by:
      process in interface org.apache.hc.core5.http.HttpRequestInterceptor
    • sharedConnectionManager

      protected org.apache.hc.client5.http.io.HttpClientConnectionManager sharedConnectionManager()
      The connection manager this client should use, or null to build a private one per instance — which is the default and what every functional test wants.

      Override to share one across scenarios. See karate-profiling's PooledHttpClientFactory for the shape: one manager per run, one subclass instance per scenario. Do not return a manager from a factory that hands the same ApacheHttpClient instance to concurrent scenarios — that is unsafe for reasons unrelated to pooling, see HttpClientFactory.release(io.karatelabs.http.HttpClient).

    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException