Examples
Quick Start
Generate a single request and export as curl:
go
package main
import (
"fmt"
"github.com/cyberspacesec/request-mock-skills/pkg/mock"
)
func main() {
spec := mock.Login("https://api.example.com/login", "alice", "secret")
h, err := mock.GenerateOne(spec)
if err != nil {
panic(err)
}
curl, err := mock.Export(h, mock.FormatCurl)
if err != nil {
panic(err)
}
fmt.Println(curl)
}Run it:
bash
go run main.go
# curl -X POST 'https://api.example.com/login' \
# -H 'Content-Type: application/json' \
# -d '{"username":"alice","password":"secret"}'Batch Generation with Transforms
Generate 50 derived requests with deterministic transforms:
go
package main
import (
"fmt"
"github.com/cyberspacesec/request-mock-skills/pkg/mock"
)
func main() {
base := mock.GetJSON("https://api.example.com/users")
g := mock.NewGenerator(42)
specs := g.GenerateSpecs(base, 50, mock.DefaultTransforms...)
h, _ := mock.Generate(specs)
jsonl, _ := mock.Export(h, mock.FormatJSONLines)
fmt.Println(jsonl)
}Custom Transform
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, 10, MyTransform, mock.RandomServerIP)
h, _ := mock.Generate(specs)
// ...
}Engine Streaming
Use the Engine with Channel-based streaming delivery:
go
package main
import (
"context"
"fmt"
"github.com/cyberspacesec/request-mock-skills/pkg/mock"
)
func main() {
cfg := &mock.Config{
Count: 10,
Seed: 42,
Templates: []mock.TemplateRef{{Name: "getjson", URL: "https://api.example.com/users"}},
Transforms: []string{"server_ip", "user_agent"},
Export: &mock.ExportConfig{Format: "jsonl"},
}
engine := mock.NewEngine(cfg)
stream, err := engine.RunStream(context.Background())
if err != nil {
panic(err)
}
for item := range stream {
if item.Error != nil {
fmt.Printf("error at index %d: %v\n", item.Index, item.Error)
continue
}
fmt.Println(item.Data)
}
}Request Chain
Chain multiple requests where each step builds on the previous:
go
package main
import (
"fmt"
"github.com/cyberspacesec/request-mock-skills/pkg/mock"
)
func main() {
g := mock.NewGenerator(42)
chain := mock.NewChain(g)
chain.AddStep("login", mock.Login("https://api.example.com/login", "alice", "secret"))
chain.AddStep("profile", mock.GetJSON("https://api.example.com/me"))
chain.AddStep("dashboard", mock.GetJSON("https://api.example.com/dashboard"))
h, err := chain.Execute()
if err != nil {
panic(err)
}
data, _ := mock.ExportBytes(h, mock.FormatJSON, mock.WithExportIndent(2))
fmt.Println(string(data))
}Batch Config
Use a YAML config file to drive batch generation:
go
package main
import (
"fmt"
"github.com/cyberspacesec/request-mock-skills/pkg/mock"
)
func main() {
cfg, err := mock.LoadConfig("examples/config/batch.yaml")
if err != nil {
panic(err)
}
cfg.Count = 100
cfg.Export = &mock.ExportConfig{Format: "jsonl"}
h, err := cfg.Run()
if err != nil {
panic(err)
}
out, _ := mock.Export(h, mock.FormatJSONLines)
fmt.Println(out)
}Generate and Export to File
go
package main
import (
"github.com/cyberspacesec/request-mock-skills/pkg/mock"
)
func main() {
spec := mock.GetJSON("https://api.example.com/users")
g := mock.NewGenerator(42)
specs := g.GenerateSpecs(spec, 100, mock.DefaultTransforms...)
h, _ := mock.Generate(specs)
mock.ExportToFile(h, mock.FormatHAR, "output.har")
mock.ExportToFile(h, mock.FormatCurl, "output.sh")
mock.ExportToFile(h, mock.FormatPostman, "collection.json")
}More examples
See the examples/ directory in the repository for complete runnable programs.