# PDK Test &emsp; [![Latest Version]][crates.io] [![pdk--test msrv]][Rust 1.88]

<!-- Update links on release. -->
[Latest Version]: https://img.shields.io/badge/crates.io-v1.9.0-blue
[crates.io]: https://crates.io/crates/pdk-test
[pdk--test msrv]: https://img.shields.io/badge/pdk--test_msrv-1.88-lightgray
[Rust 1.88]: https://blog.rust-lang.org/2025/06/26/Rust-1.88.0

**pdk-test is an async framework designed to test PDK-based custom policies.**

---

You may be looking for:

- [An overview of PDK](https://docs.mulesoft.com/pdk/latest/policies-pdk-overview)
- [Writing Integration tests](https://docs.mulesoft.com/pdk/latest/policies-pdk-integration-tests)
- [Examples](https://github.com/mulesoft/pdk-custom-policy-examples/blob/main/dataweave/tests/requests.rs)
- [Release notes](https://docs.mulesoft.com/release-notes/pdk/pdk-release-notes)

## pdk-test in action

```toml
[dev-dependencies]
pdk-test = { version = "1.9"}
```

```rust
#[pdk_test]
async fn query() -> anyhow::Result<()> {
    // Configure upstream service
    let upstream_config = HttpMockConfig::builder()
        .port(80)
        .hostname("backend")
        .build();

    // Configure a Flex service
    let policy_config = PolicyConfig::builder()
        .name(POLICY_NAME)
        .configuration(serde_json::json!({"query": ["key", "extra", "missing"]}))
        .build();

    let api_config = ApiConfig::builder()
        .name("ingress-http")
        .upstream(&upstream_config)
        .path("/anything/echo/")
        .port(FLEX_PORT)
        .policies([policy_config])
        .build();

    let flex_config = FlexConfig::builder()
        .version("1.10.0")
        .hostname("local-flex")
        .with_api(api_config)
        .config_mounts([(POLICY_DIR, "policy"), (COMMON_CONFIG_DIR, "common")])
        .build();

    // Compose the services
    let composite = TestComposite::builder()
        .with_service(flex_config)
        .with_service(upstream_config)
        .build()
        .await?;

    // Get a handle to the Flex service
    let flex: Flex = composite.service()?;

    // Get an external URL to point the Flex service
    let flex_url = flex.external_url(FLEX_PORT).unwrap();

    // Get a handle to the upstream service
    let upstream: HttpMock = composite.service()?;

    // Connect to the handle of the upstream service
    let upstream_server = MockServer::connect_async(upstream.socket()).await;

    // Set up mock that should not be invoked if everything works correctly. In this case it's a mock for the case when
    // the header that should not be present, actually is
    let failed_mock = upstream_server
        .mock_async(|when, then| {
            when.header_exists("X-Query-Absent");
            then.status(500)
                .body("Header X-Query-Absent should not get to the backend");
        })
        .await;

    // Set up mock that represents the desired behavior
    let success_mock = upstream_server
        .mock_async(|when, then| {
            when.header("X-Query-Key", "value")
                .header("X-Query-Missing", "Undefined")
                .header("X-Query-Extra", "")
                .query_param("absent", "absent")
                .query_param("removed", "extra")
                .query_param("removed", "key");
            then.status(200);
        })
        .await;

    // Send a request with two query parameters.
    let response = reqwest::Client::new()
        .get(format!("{flex_url}/hello"))
        .query(&[("key", "value"), ("extra", ""), ("absent", "absent")])
        .send()
        .await?;

    assert_eq!(response.status().as_u16(), 200u16);
    success_mock.assert();
    failed_mock.assert_hits(0);

    Ok(())
}
```

### Support

For assistance with this project, you can access support resources or submit a support case through the official support channel at https://help.mulesoft.com/s/support.

### License

```
Copyright 2026 Salesforce, Inc. All rights reserved. 

These Terms of Use govern the download, installation, and/or use of this software provided by Salesforce, Inc. (“Salesforce”) (the “Software”), were last updated on April 15, 2022, and constitute a legally binding agreement between you and Salesforce. If you do not agree to these Terms of Use, do not install or use the Software.  << The Software may link to third-party software components licensed under various open source licenses (“Open Source Components”). These Terms of Use pertain solely to Salesforce's proprietary code in the Software. It does not alter or extend any rights or obligations regarding the Open Source Components. For clarity, your use of the Open Source Components is governed by the terms of the applicable open source license(s). You are solely responsible for complying with the terms and conditions of those open source licenses.>>
 
Salesforce grants you a worldwide, non-exclusive, no-charge, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute the Software and derivative works subject to these Terms. These Terms shall be included in all copies or substantial portions of the Software. 

Subject to the limited rights expressly granted hereunder, Salesforce reserves all rights, title, and interest in and to all intellectual property subsisting in the Software. No rights are granted to you hereunder other than as expressly set forth herein. Users residing in countries on the United States Office of Foreign Assets Control sanction list, or which are otherwise subject to a US export embargo, may not use the Software.
 
Implementation of the Software may require development work, for which you are responsible. The Software may contain bugs, errors and incompatibilities and is made available on an AS IS basis without support, updates, or service level commitments. 
 
Salesforce reserves the right at any time to modify, suspend, or discontinue, the Software (or any part thereof) with or without notice. You agree that Salesforce shall not be liable to you or to any third party for any modification, suspension, or discontinuance.

You agree to defend Salesforce against any claim, demand, suit or proceeding made or brought against Salesforce by a third party arising out of or accruing from (a) your use of the Software, and (b) any application you develop with the Software that infringes any copyright, trademark, trade secret, trade dress, patent, or other intellectual property right of any person or defames any person or violates their rights of publicity or privacy (each a “Claim Against Salesforce”), and will indemnify Salesforce from any damages, attorney fees, and costs finally awarded against Salesforce as a result of, or for any amounts paid by Salesforce under a settlement approved by you in writing of, a Claim Against Salesforce, provided Salesforce (x) promptly gives you written notice of the Claim Against Salesforce, (y) gives you sole control of the defense and settlement of the Claim Against Salesforce (except that you may not settle any Claim Against Salesforce unless it unconditionally releases Salesforce of all liability), and (z) gives you all reasonable assistance, at your expense.

WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, THE SOFTWARE IS NOT SUPPORTED AND IS PROVIDED “AS IS,” WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. IN NO EVENT SHALL SALESFORCE HAVE ANY LIABILITY FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE, OR CONSEQUENTIAL DAMAGES, OR DAMAGES BASED ON LOST PROFITS, DATA, OR USE, IN CONNECTION WITH THE SOFTWARE, HOWEVER CAUSED AND WHETHER IN CONTRACT, TORT, OR UNDER ANY OTHER THEORY OF LIABILITY, WHETHER OR NOT YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 
These Terms of Use shall be governed exclusively by the internal laws of the State of California, without regard to its conflicts of laws rules. Each party hereby consents to the exclusive jurisdiction of the state and federal courts located in San Francisco County, California to adjudicate any dispute arising out of or relating to these Terms of Use and the download, installation, and/or use of the Software. Except as expressly stated herein, these Terms of Use constitute the entire agreement between the parties, and supersede all prior and contemporaneous agreements, proposals, or representations, written or oral, concerning their subject matter. No modification, amendment, or waiver of any provision of these Terms of Use shall be effective unless it is by an update to these Terms of Use that Salesforce makes available, or is in writing and signed by the party against whom the modification, amendment, or waiver is to be asserted.

Data Privacy: Salesforce may collect, process, and store device, system, and other information related to your use of the Software. This information includes, but is not limited to, IP address, user metrics, and other data (“Usage Data”). Salesforce may use Usage Data for analytics, product development, and marketing purposes. You acknowledge that files generated in conjunction with the Software may contain sensitive or confidential data, and you are solely responsible for anonymizing and protecting such data.
```
