Skip to content

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 batch
  • r — a seeded *rand.Rand for deterministic randomness

Built-in Transforms (16) ​

NameFunctionMutatesDescription
server_ipRandomServerIPServerIPRandom IPv4 address
user_agentRandomUserAgentHeaders["User-Agent"]Random UA from pool of 20+ real-world agents
pathRandomPathURL pathRandom suffix segment from pool
query_noiseRandomQueryNoiseQuery paramsAdd 1-3 random noise params
statusRandomStatusResponseStatusRandom HTTP status (200, 201, 301, 302, 400, 401, 403, 404, 500, 502, 503)
timingsRandomTimingsTimingsRandom realistic timing values
indexIndexSuffixURLAppend /_idx=<i> query param
portRandomPortURL portReplace port with random from pool
hostRandomHostURL hostRandom subdomain prefix
resp_cookieRandomResponseCookieResponseCookiesRandom response cookie
headerRandomHeaderHeadersRandom request header
body_noiseRandomBodyNoisePostTextInject noise into JSON body
seq_statusSequentialStatusResponseStatusSequential status codes (200 to 500 and loop)
priorityRandomPriorityPriorityRandom Chrome priority value
resource_typeRandomResourceTypeResourceTypeRandom Chrome resource type
connectionRandomConnectionConnectionRandom 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:#e8f5e9

Custom 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 none

Next ​

Released under the MIT License.