PyRIT icon indicating copy to clipboard operation
PyRIT copied to clipboard

FEAT extend http target to allow custom http client

Open ayeganov opened this issue 10 months ago • 0 comments

Allow injection of pre-configured httpx client in HTTPTarget

Changes

  • Added alternative constructor with_client to allow direct injection of pre-configured httpx.AsyncClient instances
  • Modified __init__ to accept an optional _client parameter
  • Updated send_prompt_async to handle both injected and auto-created clients
  • Maintained full backward compatibility with existing usage

Why

This change enables more flexible configuration of the HTTP client used by HTTPTarget. Users can now:

  • Pre-configure custom timeout settings, proxies, or certificate handling
  • Reuse client connections across multiple HTTPTarget instances
  • Better control client lifecycle when needed
  • Inject mock clients for testing

Example Usage

# Original usage remains unchanged
target1 = HTTPTarget(http_request="...", use_tls=True)

# New usage with custom client
client = httpx.AsyncClient(timeout=30.0, verify=False)
target2 = HTTPTarget.with_client(client=client, http_request="...")

Description

Tests and Documentation

Added new test test_http_target_with_injected_client.

ayeganov avatar Mar 18 '25 19:03 ayeganov