Class TestRequestConfig

java.lang.Object
ru.vyarus.dropwizard.guice.test.client.builder.TestRequestConfig
All Implemented Interfaces:
Consumer<jakarta.ws.rs.client.Invocation.Builder>, Function<jakarta.ws.rs.client.WebTarget,jakarta.ws.rs.client.WebTarget>

public class TestRequestConfig extends Object implements Function<jakarta.ws.rs.client.WebTarget,jakarta.ws.rs.client.WebTarget>, Consumer<jakarta.ws.rs.client.Invocation.Builder>
Request configuration. Used to configure client defaults and for exact request configuration. The main problem with the jersey client api is that it split into two phases: request target (including query params) and request itself. So we have to collect all required data separately to apply both phases at once (to provide simpler api).

The configuration is hierarchical: sub-clients could inherit top level configurations (defaults copies on creation).

Most properties provide Supplier-based configuration variant for lazy evaluation (applicable for defaults, when actual value is resolved in time of request creation and not in time of default registration).

Also provides direct access for WebTarget and Invocation.Builder objects with configurePath(java.util.function.Function) and configureRequest(java.util.function.Consumer) methods. Config itself is registered in these methods to apply all collected data.

Object also keeps data formatters to be used in form builder.

Since:
13.09.2025
  • Constructor Details

    • TestRequestConfig

      public TestRequestConfig(@Nullable TestRequestConfig base)
      Create a request config.
      Parameters:
      base - base configuration (optional) to copy values from
  • Method Details

    • configurePath

      public TestRequestConfig configurePath(Function<jakarta.ws.rs.client.WebTarget,jakarta.ws.rs.client.WebTarget> modifier)
      Provides direct access for jersey WebTarget object configuration.
      Parameters:
      modifier - function, applying request target configuration
      Returns:
      builder instance for chained calls
    • configureRequest

      public TestRequestConfig configureRequest(Consumer<jakarta.ws.rs.client.Invocation.Builder> modifier)
      Provides direct access for jersey Invocation.Builder object configuration.
      Parameters:
      modifier - consumer, applying request configuration
      Returns:
      builder instance for chained calls
    • property

      public TestRequestConfig property(String name, Object value)
      Jersey client property configuration (JerseyClient.property(String, Object)).

      Multiple calls override previous value.

      Parameters:
      name - property name
      value - property value
      Returns:
      builder instance for chained calls
      See Also:
      • ClientProperties
    • property

      public TestRequestConfig property(String name, Supplier<Object> supplier)
      Configure jersey client property configuration (JerseyClient.property(String, Object)).

      Multiple calls override previous value.

      Parameters:
      name - property name
      supplier - value supplier
      Returns:
      builder instance for chained calls
      See Also:
      • ClientProperties
    • register

      public <K> TestRequestConfig register(Class<K> type)
      Configure jersey client extension registration (JerseyClient.register(Class)). Could be useful, for example, to register custom MessageBodyReader.

      Multiple registrations of the same class will be ignored (extensions tracked by type).

      Type Parameters:
      K - extension type
      Parameters:
      type - extension class
      Returns:
      builder instance for chained calls
    • register

      public <K> TestRequestConfig register(K extension)
      Configure jersey client extension registration (JerseyClient.register(Object)). Could be useful, for example, to register custom MessageBodyReader (as instance).

      When called with different instances of the same class: only the latest registration will be used (extensions tracked by type).

      Type Parameters:
      K - extension type
      Parameters:
      extension - extensions instance
      Returns:
      builder instance for chained calls
    • register

      public <K> TestRequestConfig register(Class<K> type, @Nullable Supplier<K> supplier)
      Configure jersey client extension registration (JerseyClient.register(Object)). Supplier could return either class or extension instance.

      When called with different instances of the same class: only the latest registration will be used (extensions tracked by type).

      Type Parameters:
      K - extension type
      Parameters:
      type - extension type
      supplier - extension instance or null (for class)
      Returns:
      builder instance for chained calls
    • accept

      public TestRequestConfig accept(String... accept)
      Configure request Accept header (Invocation.Builder.accept(String...)).

      Multiple calls override previous value.

      Parameters:
      accept - media types required for response
      Returns:
      builder instance for chained calls
      See Also:
      • MediaType
    • acceptLanguage

      public TestRequestConfig acceptLanguage(String... language)
      Configure request Accept-Language header (Invocation.Builder.acceptLanguage(String...)).

      Multiple calls override previous value.

      Parameters:
      language - languages to accept
      Returns:
      builder instance for chained calls
    • acceptEncoding

      public TestRequestConfig acceptEncoding(String... encoding)
      Configure request Accept-Encoding header (Invocation.Builder.acceptEncoding(String...)).

      Multiple calls override previous value.

      Parameters:
      encoding - encodings to accept
      Returns:
      builder instance for chained calls
    • cacheControl

      public TestRequestConfig cacheControl(String cacheControl)
      Configure request Cache-Control header (Invocation.Builder.cacheControl(CacheControl)).

      Multiple calls override previous value.

      Parameters:
      cacheControl - cache control header value (as string).
      Returns:
      builder instance for chained calls
    • cacheControl

      public TestRequestConfig cacheControl(jakarta.ws.rs.core.CacheControl cacheControl)
      Configure request Cache-Control header (Invocation.Builder.cacheControl(CacheControl)).

      Multiple calls override previous value.

      Parameters:
      cacheControl - cache control settings to apply to request.
      Returns:
      builder instance for chained calls
    • queryParam

      public TestRequestConfig queryParam(String name, Object value)
      Configure request query parameter (WebTarget.queryParam(String, Object...)).

      Note: jersey api supports multiple query parameters with the same name (in this case multiple parameters added). The same result could be achieved by passing list or array into this api.

      Multiple calls with the same name override the previous value (the only way to specify multiple values is using list or array as value).

      Parameters:
      name - query parameter name
      value - parameter value (list or array for multiple values)
      Returns:
      builder instance for chained calls
    • queryParam

      public TestRequestConfig queryParam(String name, Supplier<Object> supplier)
      Configure request query parameter (WebTarget.queryParam(String, Object...)).

      Note: jersey api supports multiple query parameters with the same name (in this case multiple parameters added). The same result could be achieved by passing list or array into this api.

      Multiple calls with the same name override the previous value (the only way to specify multiple values is using list or array as value).

      Parameters:
      name - param name
      supplier - value supplier (list or array for multiple values)
      Returns:
      builder instance for chained calls
    • pathParam

      public TestRequestConfig pathParam(String name, Object value)
      Configure request path parameter (WebTarget.resolveTemplateFromEncoded(String, Object)).

      Note: parameter value assumed to be encoded to be able to specify matrix parameters (in the middle of the path): pathParam("var", "/path;var=1;val=2").

      Parameters:
      name - parameter name
      value - parameter value
      Returns:
      builder instance for chained calls
    • pathParam

      public TestRequestConfig pathParam(String name, Supplier<Object> supplier)
      Configure request path parameter (WebTarget.resolveTemplateFromEncoded(String, Object)).
      Parameters:
      name - parameter name
      supplier - parameter value supplier
      Returns:
      builder instance for chained calls
    • matrixParam

      public TestRequestConfig matrixParam(String name, Object value)
      Configure request matrix parameter (WebTarget.matrixParam(String, Object...)).

      Note: jersey api supports multiple matrix parameters with the same name (in this case multiple parameters added). The same result could be achieved by passing list or array into this api.

      Multiple calls with the same name override the previous value (the only way to specify multiple values is using list or array as value).

      Parameters:
      name - matrix parameter name
      value - parameter value (list or array for multiple values)
      Returns:
      builder instance for chained calls
    • matrixParam

      public TestRequestConfig matrixParam(String name, Supplier<Object> supplier)
      Configure request matrix parameter (WebTarget.matrixParam(String, Object...)).

      Note: jersey api supports multiple matrix parameters with the same name (in this case multiple parameters added). The same result could be achieved by passing list or array into this api.

      Multiple calls with the same name override the previous value (the only way to specify multiple values is using list or array as value).

      Parameters:
      name - param name
      supplier - value supplier (list or array for multiple values)
      Returns:
      builder instance for chained calls
    • header

      public TestRequestConfig header(String name, Object value)
      Configure request header (Invocation.Builder.header(String, Object)).

      Multiple calls override previous value.

      Parameters:
      name - header name
      value - header value
      Returns:
      builder instance for chained calls
    • header

      public TestRequestConfig header(String name, Supplier<Object> supplier)
      Configure request header (Invocation.Builder.header(String, Object)).

      Multiple calls override previous value.

      Parameters:
      name - header name
      supplier - value supplier
      Returns:
      builder instance for chained calls
    • cookie

      public TestRequestConfig cookie(jakarta.ws.rs.core.Cookie cookie)
      Configure request cookie (Invocation.Builder.cookie(jakarta.ws.rs.core.Cookie).

      Use cookie builder: new NewCookie.Builder(name).value(value).build().

      Multiple calls override previous value.

      Parameters:
      cookie - cookie value
      Returns:
      builder instance for chained calls
      See Also:
      • NewCookie
    • cookie

      public TestRequestConfig cookie(String name, Supplier<jakarta.ws.rs.core.Cookie> supplier)
      Configure request cookie (Invocation.Builder.cookie(jakarta.ws.rs.core.Cookie).

      Multiple calls override previous value.

      Parameters:
      name - name
      supplier - value suppler
      Returns:
      builder instance for chained calls
      See Also:
      • NewCookie
    • formDateFormatter

      public TestRequestConfig formDateFormatter(DateFormat formatter)
      Configure java.util date formatter for form fields (used in FormBuilder).
      Parameters:
      formatter - date formatter
      Returns:
      builder instance for chained calls
    • formDateTimeFormatter

      public TestRequestConfig formDateTimeFormatter(DateTimeFormatter formatter)
      Configure java.time formatter for form fields (used in FormBuilder).
      Parameters:
      formatter - date formatter
      Returns:
      builder instance for chained calls
    • debug

      public TestRequestConfig debug(boolean debug)
      Enable debug output for configured requests.
      Parameters:
      debug - true to enable debug, false to disable
      Returns:
      builder instance for chained calls
    • assertRequest

      public TestRequestConfig assertRequest(Consumer<RequestTracker> assertion)
      Assert configuration applied to WebTarget and Invocation.Builder.

      Option implicitly enables debug.

      Assertions would be executed just before request execution

      Parameters:
      assertion - assertions
      Returns:
      builder instance for chained calls
    • applyRequestConfiguration

      public jakarta.ws.rs.client.Invocation.Builder applyRequestConfiguration(jakarta.ws.rs.client.WebTarget webTarget)
      Build jersey client request with configured values.
      Parameters:
      webTarget - target
      Returns:
      pre-configured jersey client request builder
    • clear

      public TestRequestConfig clear()
      Clear configuration.
      Returns:
      builder instance for chained calls
    • hasConfiguration

      public boolean hasConfiguration()
      Returns:
      true if any configuration applied, false otherwise
    • printConfiguration

      public String printConfiguration()
      Returns:
      render configured values as string
    • getConfiguredQueryParams

      public Set<String> getConfiguredQueryParams()
      Returns:
      configured query parameter names or empty set if not configured
    • getConfiguredQueryParamsMap

      public Map<String,Object> getConfiguredQueryParamsMap()
      Returns:
      configured query parameters map or empty map if not configured
    • getConfiguredQueryParamsSource

      public Map<String,SourceAwareValue<Object>> getConfiguredQueryParamsSource()
      Returns:
      query parameters with declaration sources or empty map if not configured
    • getConfiguredMatrixParams

      public Set<String> getConfiguredMatrixParams()
      Returns:
      configured matrix parameter names or empty set if not configured
    • getConfiguredMatrixParamsMap

      public Map<String,Object> getConfiguredMatrixParamsMap()
      Returns:
      configured matrix parameters map or empty map if not configured
    • getConfiguredMatrixParamsSource

      public Map<String,SourceAwareValue<Object>> getConfiguredMatrixParamsSource()
      Returns:
      matrix parameters with declaration sources or empty map if not configured
    • getConfiguredHeaders

      public Set<String> getConfiguredHeaders()
      Returns:
      configured header names or empty set if not configured
    • getConfiguredHeadersMap

      public Map<String,Object> getConfiguredHeadersMap()
      Returns:
      configured headers map or empty map if not configured
    • getConfiguredHeadersSource

      public Map<String,SourceAwareValue<Object>> getConfiguredHeadersSource()
      Returns:
      configured headers with declaration sources or empty map if not configured
    • getConfiguredCookies

      public Set<String> getConfiguredCookies()
      Returns:
      configured cookie names or empty set if not configured
    • getConfiguredCookiesMap

      public Map<String,jakarta.ws.rs.core.Cookie> getConfiguredCookiesMap()
      Returns:
      configured cookies map or empty map if not configured
    • getConfiguredCookiesSource

      public Map<String,SourceAwareValue<jakarta.ws.rs.core.Cookie>> getConfiguredCookiesSource()
      Returns:
      configured cookie with declaration sources or empty map if not configured
    • getConfiguredProperties

      public Set<String> getConfiguredProperties()
      Returns:
      configured property names or empty set if not configured
    • getConfiguredPropertiesMap

      public Map<String,Object> getConfiguredPropertiesMap()
      Returns:
      configured properties map or empty map if not configured
    • getConfiguredPropertiesSource

      public Map<String,SourceAwareValue<Object>> getConfiguredPropertiesSource()
      Returns:
      configured properties with declaration sources or empty map if not configured.
    • getConfiguredExtensions

      public Set<Class<?>> getConfiguredExtensions()
      Returns:
      configured extension classes or empty set if not configured.
    • getConfiguredExtensionsMap

      public Map<Class<?>,Object> getConfiguredExtensionsMap()
      Note: extensions registered with class will have class in value.
      Returns:
      configured extensions map or empty map if not configured
    • getConfiguredExtensionsSource

      public Map<Class<?>,SourceAwareValue<?>> getConfiguredExtensionsSource()
      Returns:
      configured extensions with declaration sources or empty map if not configured
    • getConfiguredPathParams

      public Set<String> getConfiguredPathParams()
      Returns:
      configured path param names or empty set if not configured
    • getConfiguredPathParamsMap

      public Map<String,Object> getConfiguredPathParamsMap()
      Returns:
      configured path params map or empty map if not configured
    • getConfiguredPathParamsSource

      public Map<String,SourceAwareValue<Object>> getConfiguredPathParamsSource()
      Returns:
      configured path params with declaration sources or empty map if not configured
    • getConfiguredAccepts

      public List<String> getConfiguredAccepts()
      Returns:
      configured accept media types or empty list if not configured
    • getConfiguredAcceptsSource

      public @Nullable SourceAwareValue<String[]> getConfiguredAcceptsSource()
      Returns:
      configured media types with declaration source or null if not configured
    • getConfiguredLanguages

      public List<String> getConfiguredLanguages()
      Returns:
      configured languages or empty list if not configured
    • getConfiguredLanguagesSource

      public @Nullable SourceAwareValue<String[]> getConfiguredLanguagesSource()
      Returns:
      configured languages with declaration source or null if not configured
    • getConfiguredEncodings

      public List<String> getConfiguredEncodings()
      Returns:
      configured accept languages or empty list if not configured
    • getConfiguredEncodingsSource

      public @Nullable SourceAwareValue<String[]> getConfiguredEncodingsSource()
      Returns:
      configured encodings with declaration source or null if not configured
    • getConfiguredFormDateFormatter

      public @Nullable DateFormat getConfiguredFormDateFormatter()
      Returns:
      configured java.util date formatter or null if not configured
    • getConfiguredFormDateFormatterSource

      public @Nullable SourceAwareValue<DateFormat> getConfiguredFormDateFormatterSource()
      Returns:
      configured java.util date formatter supplier with declaration source or null if not configured
    • getConfiguredFormDateTimeFormatter

      public @Nullable DateTimeFormatter getConfiguredFormDateTimeFormatter()
      Returns:
      configured java.time date formatter or null if not configured
    • getConfiguredFormDateTimeFormatterSource

      public @Nullable SourceAwareValue<DateTimeFormatter> getConfiguredFormDateTimeFormatterSource()
      Returns:
      configured java.time date formatter supplier with declaration source or null if not configured
    • getConfiguredCacheControl

      public @Nullable jakarta.ws.rs.core.CacheControl getConfiguredCacheControl()
      Returns:
      configured cache control or null if not configured
    • getConfiguredCacheControlSource

      public @Nullable SourceAwareValue<jakarta.ws.rs.core.CacheControl> getConfiguredCacheControlSource()
      Returns:
      configured cache control with declaration source or null
    • getConfiguredPathModifiers

      public List<Function<jakarta.ws.rs.client.WebTarget,jakarta.ws.rs.client.WebTarget>> getConfiguredPathModifiers()
      Returns:
      custom path modifiers (excluding config object itself)
    • getConfiguredPathModifiersSource

      public List<? extends SourceAwareValue<Function<jakarta.ws.rs.client.WebTarget,jakarta.ws.rs.client.WebTarget>>> getConfiguredPathModifiersSource()
      Returns:
      configured path modifiers with declaration sources or empty list
    • getConfiguredRequestModifiers

      public List<Consumer<jakarta.ws.rs.client.Invocation.Builder>> getConfiguredRequestModifiers()
      Returns:
      custom request modifiers (excluding config object itself)
    • getConfiguredRequestModifiersSource

      public List<? extends SourceAwareValue<Consumer<jakarta.ws.rs.client.Invocation.Builder>>> getConfiguredRequestModifiersSource()
      Returns:
      configured request modifiers with declaration sources or empty list
    • isDebugEnabled

      public boolean isDebugEnabled()
      Returns:
      true if debug enabled
    • apply

      public jakarta.ws.rs.client.WebTarget apply(jakarta.ws.rs.client.WebTarget webTarget)
      Specified by:
      apply in interface Function<jakarta.ws.rs.client.WebTarget,jakarta.ws.rs.client.WebTarget>
      Parameters:
      webTarget - target to configure
      Returns:
      configured target
    • accept

      public void accept(jakarta.ws.rs.client.Invocation.Builder request)
      Specified by:
      accept in interface Consumer<jakarta.ws.rs.client.Invocation.Builder>
      Parameters:
      request - builder to configure.