Saloon Documentation

repository·v4·Indexed 25 days ago

https://github.com/saloonphp/saloon

A PHP HTTP client wrapper designed to make API requests easy and testable. Saloon features a robust faking system using Fixtures to record and replay HTTP interactions, a MockClient for defining expected responses and assertions, and comprehensive OAuth2 authentication support via OAuthConfig and AccessTokenAuthenticator.

Tokens
4.5K
Snippets
0
Records
39
Agent score
82%

What's inside Saloon

  1. Usage guidelines for the Saloon logo

    v4

    The Saloon logo is subject to specific licensing conditions.

    Permitted Uses

    You may use the logo for:

    • Marketing materials for technical events (meetups, hackathons, conferences, workshops) related to Laravel.
    • Open source projects related to Laravel.
    • Technical articles, videos, books, or papers for educational purposes.
    • Illustrating a commercial product (to show integration).

    Prohibited Uses

    You may NOT use the logo without prior written consent for:

    • Using the logo in a commercial product for purposes other than illustrating its integration.
    • Selling physical products that use the Saloon logo or its variants (e.g., t-shirts).

    All rights are reserved, and the owner reserves the right of final explanation for any use case not explicitly stated above.

  2. Use MockClient for HTTP testing

    v4

    The MockClient is used to fake HTTP requests during testing, allowing you to define expected responses without making real network calls. You can instantiate a new MockClient or use a global instance.

    Responses can be added to the client in several ways:

    1. Sequence: Responses added without a specific capture method are played back in the order they were added.
    2. Request-based: Map a specific Request class to a response.
    3. Connector-based: Map a specific Connector class to a response.
    4. URL-based: Map a URL pattern (using URLHelper matching) to a response.
  3. Register a global MockClient

    v4

    You can register a global MockClient that is available throughout your application's lifecycle. This is useful for integration tests where the client is instantiated deep within the application code.

    Important: You must call MockClient::destroyGlobal() after each test to prevent state leakage between tests.

  4. Saloon Brand Colors and Fonts

    v4

    The Saloon brand identity uses the Lato font. The color palette consists of the following hex and RGB values:

    ColorHexRGB
    50#FFFBEBrgb(255,251,235)
    100#FEF3C7rgb(254,243,199)
    200#FDE68Argb(253,230,138)
    300#FCD34Drgb(252,211,77)
    400#FBBF24rgb(251,191,36)
    500#F59E0Brgb(245,158,11)
    600#D97706rgb(217,119,6)
    700#B45309rgb(180,83,9)
    800#92400Ergb(146,64,14)
    900#78350Frgb(120,53,15)
  5. Access response headers and body

    v4

    Methods for retrieving raw response data:

    • body(): Returns the response body as a string.
    • stream(): Returns the response body as a Psr\\Http\\Message\\StreamInterface.
    • headers(): Returns an ArrayStore containing the response headers.
    • header(string $header): Returns a specific header value. If the header has multiple values, it returns an array; otherwise, it returns a string.
  6. Access data from a FakeResponse

    v4

    Once a FakeResponse is created, you can inspect its contents using the following methods:

    • status(): Returns the HTTP status code as an int.
    • headers(): Returns the headers as an ArrayStoreContract.
    • body(): Returns the response body as a BodyRepository (either JsonBodyRepository or StringBodyRepository).
  7. Configure OAuth2 authentication with OAuthConfig

    v4

    The OAuthConfig class is used to store and manage the configuration required for OAuth2 authentication flows. You can instantiate it using the make() method and configure its properties using a fluent interface.

    Key configuration options include:

    • setClientId(string $clientId): The OAuth2 client identifier.
    • setClientSecret(string $clientSecret): The OAuth2 client secret.
    • setRedirectUri(string $redirectUri): The URI to which the authorization server will redirect the user after authorization.
    • setAuthorizeEndpoint(string $endpoint): The endpoint used for the authorization URL (defaults to authorize).
    • setTokenEndpoint(string $endpoint): The endpoint used to create and refresh tokens (defaults to token).
    • setUserEndpoint(string $endpoint): The endpoint used to retrieve user information (defaults to user).
    • setDefaultScopes(array $scopes): An array of default scopes applied to every authorization URL.
    • setAllowBaseUrlOverride(bool $allow): If set to true, OAuth endpoints can be absolute URLs that differ from the connector's base URL. Warning: Do not enable this if endpoint values are user-controlled to prevent SSRF or credential leakage.
    • setRequestModifier(callable $modifier): A callback that allows you to modify the Saloon\Http\Request object before it is sent.

    You should call validate() to ensure the configuration is complete before attempting to use it in an OAuth2 flow.

  8. Create a mock response with FakeResponse::make()

    v4

    Use FakeResponse::make() to create a mock HTTP response for testing. You can specify the response body (as an array or string), the HTTP status code, and an array of headers.

    If you provide an array as the body, it is treated as a JsonBodyRepository. If you provide a string, it is treated as a StringBodyRepository.

  9. Define requests for a Pool

    v4

    The setRequests() method allows you to define the collection of requests to be processed. It accepts:

    • An iterable of Saloon\Http\Request objects or GuzzleHttp\Promise\PromiseInterface instances.
    • A callable that receives the Connector and returns an iterable of requests.

    Using a callable is useful for lazily generating requests that depend on the connector configuration.

  10. Retrieve values from MultipartBodyRepository

    v4

    You can retrieve data from the repository using:

    • get(string|int $key, mixed $default = null): MultipartValue|array: Returns a single MultipartValue if there is one match for the key, or an array of MultipartValue objects if multiple matches exist. Returns $default if no match is found.
    • all(): array: Returns an array of all MultipartValue objects currently in the repository.
  11. Modify mock response data using merge() and through()

    v4

    When using Saloon fixtures to fake HTTP responses, you can dynamically alter the data returned by the fixture before it is used in your tests. This is useful for making static fixtures more flexible (e.g., changing an ID or a timestamp).

    • merge(array $merge): Uses dot-notation to overwrite or add specific keys to the response body.
    • through(\Closure $through): Allows you to pass the entire response body through a custom closure for complex transformations.