Expand description
§pdk-unit
A unit testing framework for PDK (Policy Development Kit) policies that enables testing Flex Gateway policies without requiring a running Envoy proxy or WebAssembly runtime.
§Overview
pdk-unit provides a complete in-process testing solution for PDK policies by stubbing
the Proxy-Wasm host environment. This allows developers to write fast, deterministic
unit tests that verify policy behavior without the overhead of integration testing.
§Quick Start
ⓘ
use pdk_unit::{UnitHttpRequest, UnitHttpResponse, UnitHttpMessage, UnitTestBuilder};
#[test]
fn test_my_policy() {
let mut tester = UnitTestBuilder::default()
.with_config(r#"{"key": "value"}"#)
.with_entrypoint(crate::configure);
let request = UnitHttpRequest::get()
.with_header(":path", "/api/test");
let response = tester.request(request);
assert_eq!(response.status_code(), 200);
}§Core Components
UnitTestBuilder- Fluent builder for configuring test instancesUnitTest- Main test orchestrator managing request lifecyclesUnitTestRequest- Handle to an in-flight request being processedUnitHttpRequest- HTTP request with method constructors (e.g.UnitHttpRequest::get)UnitHttpResponse- HTTP response with astatus_codeaccessorUnitHttpMessage- Trait providing common read accessors for both request and response
§Backends
Mock upstream services using the Backend and GrpcBackend traits:
TraceBackend- Records all incoming requests for later inspection; also acts as a configurable response backendprotobuf_grpc_backend- Macro for creating gRPC backends from protobuf definitions
§Features
enable_stop_iteration- EnablesStopIterationModefor handling paused requests
Structs§
- Trace
Backend - A Backend that stores the incoming requests to be later retrieved by the test. Delegates the response generation to the function provided in the constructor. This classes uses the interior mutability pattern to allow the test to retrieve the requests.
- Unit
Grpc Request - Represents a gRPC request in unit tests.
- Unit
Grpc Response - Represents a gRPC response in unit tests.
- Unit
Http Request - An HTTP request used in
pdk-unittests. - Unit
Http Response - An HTTP response used in
pdk-unittests. - Unit
Ldap Config - LDAP server configuration used to scope credential pairs registered with [
UnitTest::add_ldap_data]. - Unit
Test - The main test orchestrator for running PDK policy unit tests.
- Unit
Test Builder - A fluent builder for configuring and creating
UnitTestinstances. - Unit
Test Request - A handle to an in-flight request being processed by the policy.
Enums§
- Stop
Iteration Mode - Controls in which order how the test framework handles forwarding call responses and body processing when using the stop_iteration mode.
Traits§
- Backend
- The interface for a backend that can be used to mock the response of an http service.
- Grpc
Backend - The interface for a backend that can be used to mock the response of a grpc service. For automatic implementation see protobuf_grpc_backend
- Unit
Http Message - Common read-only accessors for HTTP messages in unit tests.
Functions§
- dw2pel
- Transform a DataWeave expression to PEL (Policy Expression Language) so it can be consumed by the PDK.
Attribute Macros§
- protobuf_
grpc_ backend - Attribute macro for implementing
GrpcBackendon an impl block.