Skip to content

API Reference ​

Package: github.com/cyberspacesec/request-mock-skills/pkg/mock

Generate Functions ​

go
func GenerateOne(spec *RequestSpec) (*har.Har, error)
func Generate(specs []*RequestSpec) (*har.Har, error)
func MustGenerate(specs []*RequestSpec) *har.Har
func GenerateAndExport(spec *RequestSpec, format string) (*Result, error)
func GenerateOneAndExport(spec *RequestSpec, format string) (*Result, error)
func GenerateNAndExport(spec *RequestSpec, n int, format string, opts ...GenerateOption) (*Result, error)
func GenerateNAndExportToFile(spec *RequestSpec, n int, format string, output string, opts ...GenerateOption) error

Generator ​

go
func NewGenerator(seed int64) *Generator
func (g *Generator) Seed() int64
func (g *Generator) GenerateSpecs(base *RequestSpec, n int, transforms ...Transform) []*RequestSpec
func (g *Generator) GenerateSpecsBatch(counts []int, transforms ...Transform) []*RequestSpec

Templates ​

go
func GetTemplate(name string) *RequestSpec
func ListTemplates() []string

Built-in Templates (20) ​

Basic (6): getjson, postjson, postform, login, upload, bearer

HTTP Method (5): delete, put, patch, head, options

Recon (4): banner, redirect, error, portscan

Extended (5): graphql, sse, websocket, grpcweb, chunked

Transforms ​

go
type Transform func(spec *RequestSpec, index int, r *rand.Rand)

var DefaultTransforms []Transform

func LookupTransform(name string) Transform
func NamedTransforms() map[string]Transform

Built-in Transforms (16) ​

FunctionName stringDescription
RandomServerIPserver_ipRandom IPv4 address
RandomUserAgentuser_agentRandom UA from pool
RandomPathpathRandom suffix segment
RandomQueryNoisequery_noiseAdd random noise params
RandomStatusstatusRandom HTTP status
RandomTimingstimingsRandom timing values
IndexSuffixindexAppend /_idx=<i> query param
RandomPortportReplace port with random from pool
RandomHosthostRandom subdomain prefix
RandomResponseCookieresp_cookieRandom response cookie
RandomHeaderheaderRandom request header
RandomBodyNoisebody_noiseInject noise into JSON body
SequentialStatusseq_statusSequential status codes
RandomPrioritypriorityRandom Chrome priority
RandomResourceTyperesource_typeRandom Chrome resource type
RandomConnectionconnectionRandom connection ID

Export ​

go
func Export(h *har.Har, format string) (string, error)
func ExportToFile(h *har.Har, format string, output string) error
func ExportBytes(h *har.Har, format string, opts ...ExportOption) ([]byte, error)
func ExportWithOpts(h *har.Har, format string, opts ...ExportOption) (string, error)
func MustExport(h *har.Har, format string) string
func ExportString(h *har.Har, format string) (string, error)

type ExportOption func(*exportOptions)
func WithExportIndent(indent int) ExportOption

Format Constants ​

go
const FormatCurl = "curl"
const FormatWget = "wget"
const FormatPython = "python"
const FormatPostman = "postman"
const FormatXML = "xml"
const FormatYAML = "yaml"
const FormatHAR = "har"
const FormatJSON = "json"
const FormatJSONLines = "jsonl"
const FormatCSV = "csv"
const FormatMarkdown = "markdown"
const FormatHTML = "html"
const FormatText = "text"

Channel Streaming ​

go
func GenerateStream(ctx context.Context, specs []*RequestSpec, opts ...StreamOptions) (<-chan StreamItem, error)
func GenerateStreamFromSpecs(ctx context.Context, specs []*RequestSpec, opts ...StreamOptions) <-chan StreamItem
func CollectStream(ctx context.Context, stream <-chan StreamItem) ([]*StreamItem, error)

type StreamItem struct {
    Index int
    Data  string
    Error error
    Done  bool
}

type StreamOptions struct {
    Format  string
    Indent  int
    Mode    DeliveryMode
}

type DeliveryMode int
const DeliveryEntry DeliveryMode = iota
const DeliveryExport DeliveryMode = iota

Engine ​

go
func NewEngine(cfg *Config) *Engine
func (e *Engine) Run() (*har.Har, error)
func (e *Engine) RunExport() (*Result, error)
func (e *Engine) RunStream(ctx context.Context) (<-chan StreamItem, error)

Request Chain ​

go
func NewChain(g *Generator) *Chain
func (c *Chain) AddStep(name string, spec *RequestSpec) *Chain
func (c *Chain) Execute() (*har.Har, error)

Config ​

go
func LoadConfig(path string) (*Config, error)
func ParseConfig(data []byte) (*Config, error)
func (c *Config) Run() (*har.Har, error)

type Config struct {
    Seed       int64          `yaml:"seed,omitempty"`
    Count      int            `yaml:"count,omitempty"`
    Specs      []*RequestSpec `yaml:"specs,omitempty"`
    Templates  []TemplateRef  `yaml:"templates,omitempty"`
    Transforms []string       `yaml:"transforms,omitempty"`
    Export     *ExportConfig  `yaml:"export,omitempty"`
}

Split ​

go
func Split(h *har.Har, mode SplitMode, opts ...SplitOption) (map[string]*har.Har, error)

type SplitMode string
const SplitByPage   SplitMode = "page"
const SplitByDomain SplitMode = "domain"
const SplitByStatus SplitMode = "status"
const SplitByMethod SplitMode = "method"
const SplitBySize   SplitMode = "size"
const SplitByTime   SplitMode = "time"

type SplitOption func(*splitOptions)
func WithSplitSize(size int) SplitOption
func WithSplitInterval(interval string) SplitOption

Result ​

go
type Result struct {
    OK      bool        `json:"ok"`
    Count   int         `json:"count,omitempty"`
    Seed    int64       `json:"seed,omitempty"`
    Format  string      `json:"format,omitempty"`
    Data    interface{} `json:"data,omitempty"`
    Error   string      `json:"error,omitempty"`
}

func NewOKResult(data interface{}) *Result
func NewErrorResult(err error) *Result
func (r *Result) MarshalJSONString() (string, error)

GenerateOption ​

go
type GenerateOption func(*generateOptions)

func WithCreator(name, version string) GenerateOption
func WithBrowser(name, version string) GenerateOption
func WithHTTPVersion(version string) GenerateOption
func WithIndent(indent int) GenerateOption
func WithPages(pages []PageSpec) GenerateOption

Released under the MIT License.