Transforms
Transforms mutate a RequestSpec during batch derivation, adding variation to generated requests. They are applied in sequence by the Generator.
Transform Signature
go
type Transform func(spec *RequestSpec, index int, r *rand.Rand)Each transform receives:
spec— the cloned spec to mutate (safe to modify in place)index— the zero-based index in the batchr— a seeded*rand.Randfor deterministic randomness
Built-in Transforms (16)
| Name | Function | Mutates | Description |
|---|---|---|---|
server_ip | RandomServerIP | ServerIP | Random IPv4 address |
user_agent | RandomUserAgent | Headers["User-Agent"] | Random UA from pool of 20+ real-world agents |
path | RandomPath | URL path | Random suffix segment from pool |
query_noise | RandomQueryNoise | Query params | Add 1-3 random noise params |
status | RandomStatus | ResponseStatus | Random HTTP status (200, 201, 301, 302, 400, 401, 403, 404, 500, 502, 503) |
timings | RandomTimings | Timings | Random realistic timing values |
index | IndexSuffix | URL | Append /_idx=<i> query param |
port | RandomPort | URL port | Replace port with random from pool |
host | RandomHost | URL host | Random subdomain prefix |
resp_cookie | RandomResponseCookie | ResponseCookies | Random response cookie |
header | RandomHeader | Headers | Random request header |
body_noise | RandomBodyNoise | PostText | Inject noise into JSON body |
seq_status | SequentialStatus | ResponseStatus | Sequential status codes (200 to 500 and loop) |
priority | RandomPriority | Priority | Random Chrome priority value |
resource_type | RandomResourceType | ResourceType | Random Chrome resource type |
connection | RandomConnection | Connection | Random connection ID |
Default Transforms
When no --transform is specified, the CLI uses mock.DefaultTransforms:
go
var DefaultTransforms = []Transform{
RandomServerIP,
RandomUserAgent,
RandomPath,
RandomQueryNoise,
RandomStatus,
RandomTimings,
IndexSuffix,
}Transform Pipeline
Transforms are applied in the order they are specified:
mermaid
flowchart LR
Base["Base spec"] --> T1["server_ip"]
T1 --> T2["user_agent"]
T2 --> T3["path"]
T3 --> T4["query_noise"]
T4 --> T5["status"]
T5 --> T6["timings"]
T6 --> T7["index"]
T7 --> Out["Derived spec"]
style Base fill:#e1f5ff
style Out fill:#e8f5e9Custom Transforms
Implement the Transform function signature and pass it to GenerateSpecs:
go
package main
import (
"fmt"
"math/rand"
"github.com/cyberspacesec/request-mock-skills/pkg/mock"
)
func MyTransform(spec *mock.RequestSpec, i int, r *rand.Rand) {
spec.Headers["X-Custom"] = fmt.Sprintf("val-%d", r.Intn(1000))
}
func main() {
base := mock.GetJSON("https://api.example.com/users")
g := mock.NewGenerator(42)
specs := g.GenerateSpecs(base, 100, MyTransform, mock.RandomServerIP)
h, _ := mock.Generate(specs)
// ...
}Disabling Transforms
Pass --transform none to disable all transforms and produce exact copies of the base spec:
bash
request-mock generate -X GET -u https://api.example.com/users -n 10 --transform noneNext
- CLI: transforms — listing transforms in CLI
- SDK Guide — using transforms in Go code
- Templates — combine with templates