extractors

package
v0.117.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 15 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package extractors pulls a value out of a node's result for assertions and outputs: jsonPath/xmlPath into a parsed body, the raw body, an HTTP status code, a header.

One typed extractor per type behind the AnyExtractor interface, dispatched through a registry: RegisterExtractor binds an spi.ExtractorType to a factory that decodes the extractor's JSON config into its concrete struct, and UnmarshalExtractor peeks the wire "type" field to pick the factory. Adding an extractor = a new struct + RegisterExtractor from an init() — no switch to edit. Built-ins register in builtin.go; the HTTP-only ones (statusCode/header) register from the extractors/http subpackage.

Extract returns any: the extracted value's type is only known at runtime (a JSONPath into an arbitrary body), so this is the deliberate untyped boundary.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotImplemented = errors.New("extractor not implemented")

Functions

func FirstGroup added in v0.107.0

func FirstGroup(pattern *regexp.Regexp) int

FirstGroup is the capture group a pattern's value comes from: 1 when the pattern has a group, else 0, the whole match.

func HTMLLinks(raw string) []string

HTMLLinks lists the href of every <a> element in document order, with entities decoded and surrounding space trimmed.

func HTMLText added in v0.107.0

func HTMLText(raw string) string

HTMLText renders an HTML document as the text a reader sees: no script, style or head content, a line break after each block element and <br>, and runs of spaces collapsed.

func RegisterExtractor

func RegisterExtractor(extType spi.ExtractorType, factory func([]byte) (AnyExtractor, error))

RegisterExtractor registers a factory function for an extractor type.

Types

type AnyExtractor

type AnyExtractor interface {
	Extract(ctx ResponseContext) (any, error)
	GetType() spi.ExtractorType
}

func UnmarshalExtractor

func UnmarshalExtractor(data []byte) (AnyExtractor, error)

UnmarshalExtractor creates an Extractor from raw JSON via the registry. Built-in extractors register in builtin.go; statusCode/header in the http package — so this is a pure lookup with no per-type switch to edit.

type BodyExtractor

type BodyExtractor struct {
}

BodyExtractor extracts the entire response body. It can be used to capture the complete response as a value.

func (BodyExtractor) Extract

func (e BodyExtractor) Extract(ctx ResponseContext) (any, error)

func (BodyExtractor) ExtractText added in v0.64.0

func (e BodyExtractor) ExtractText(ctx ResponseContext) (any, error)

ExtractText returns the body as received when it parsed into a JSON object or array, so a string operator matches the wire text instead of a Go map printout. A scalar body keeps its parsed value.

func (BodyExtractor) GetType

func (e BodyExtractor) GetType() spi.ExtractorType

type HTMLExtractor added in v0.107.0

type HTMLExtractor struct {
	Selector  string `json:"selector"`
	Attribute string `json:"attribute,omitempty"`
	All       bool   `json:"all,omitempty"`
}

HTMLExtractor reads an HTML document with a CSS selector. It parses the way a browser does, so the unclosed tags and stray markup of real email and web pages never fail the parse. The value is the text of the first matching element, or its Attribute when set; with All, a list over every match.

func (HTMLExtractor) Extract added in v0.107.0

func (e HTMLExtractor) Extract(ctx ResponseContext) (any, error)

func (HTMLExtractor) GetType added in v0.107.0

func (e HTMLExtractor) GetType() spi.ExtractorType

func (HTMLExtractor) Target added in v0.110.0

func (e HTMLExtractor) Target() string

Target names the selector the extractor reads, with its attribute as selector@attribute.

type HTMLReader added in v0.107.0

type HTMLReader interface {
	GetHTML() []byte
}

HTMLReader provides the HTML a context holds when its raw body is something else, such as the HTML part of an email whose body reads as text.

type HeaderAccessor

type HeaderAccessor interface {
	GetHeader(key string) string
	Headers() http.Header
}

HeaderAccessor provides access to HTTP headers.

type JSONPathExtractor

type JSONPathExtractor struct {
	Path string `json:"path"`
}

JSONPathExtractor extracts values from JSON using JSONPath expressions (RFC 9535).

func (JSONPathExtractor) Extract

func (e JSONPathExtractor) Extract(ctx ResponseContext) (any, error)

func (JSONPathExtractor) GetType

func (e JSONPathExtractor) GetType() spi.ExtractorType

func (JSONPathExtractor) Target added in v0.110.0

func (e JSONPathExtractor) Target() string

Target names the JSON path the extractor reads.

type ParsedBodyReader

type ParsedBodyReader interface {
	GetParsedBody() any
	GetRawBody() []byte
}

ParsedBodyReader provides access to pre-parsed body (JSON, XML, etc.)

type QueryParamAccessor added in v0.64.0

type QueryParamAccessor interface {
	GetQueryParam(name string) (string, bool)
}

QueryParamAccessor provides access to the query parameters of a received request.

type RegexExtractor added in v0.107.0

type RegexExtractor struct {
	Pattern string `json:"pattern"`
	Group   *int   `json:"group,omitempty"`
}

RegexExtractor returns the first match of Pattern in the raw body: the text of an email, the body of a response. Group picks a capture group; without it, the value is group 1 when the pattern has a group, else the whole match.

func (RegexExtractor) Extract added in v0.107.0

func (e RegexExtractor) Extract(ctx ResponseContext) (any, error)

func (RegexExtractor) GetType added in v0.107.0

func (e RegexExtractor) GetType() spi.ExtractorType

func (RegexExtractor) Target added in v0.110.0

func (e RegexExtractor) Target() string

Target names the pattern the extractor matches.

type ResponseContext

type ResponseContext interface {
	// All extractors can query what capabilities are available
	HasCapability(capability string) bool
}

ResponseContext is the main context interface that all extractors receive. Concrete implementations can opt-in to specific capability interfaces.

func NewLazyResponseContext added in v0.95.0

func NewLazyResponseContext(resp *http.Response, rawBody []byte, parse func() any) ResponseContext

NewLazyResponseContext is NewResponseContext with the parsed body computed by parse when an extractor first asks for it.

func NewResponseContext

func NewResponseContext(
	resp *http.Response, rawBody []byte, parsedBody any,
) ResponseContext

NewResponseContext creates a new ResponseContext from an HTTP response.

func NewValueResponseContext

func NewValueResponseContext(value any) ResponseContext

NewValueResponseContext builds a ResponseContext backed by an arbitrary value. The value is exposed as the parsed body (GetParsedBody) and its JSON encoding as the raw body (GetRawBody). Status is 0 and headers are empty — only the body / parsed_body capabilities are advertised, which is what the jsonPath and body extractors need.

type StatusReader

type StatusReader interface {
	GetStatus() int
}

StatusReader provides access to HTTP status code.

type Targeted added in v0.110.0

type Targeted interface {
	Target() string
}

Targeted is the optional interface an extractor implements when it reads one named part of the response: a JSON path, a header name, a query parameter. A failure message quotes the target so the reader knows what was checked. Extractors that read the whole response (status_code, body) do not implement it.

type TextExtractor added in v0.64.0

type TextExtractor interface {
	ExtractText(ctx ResponseContext) (any, error)
}

TextExtractor is an extractor whose value has a text form that differs from its structured value. String operators compare against the text form.

type XMLPathExtractor

type XMLPathExtractor struct {
	Path string `json:"path"`
}

XMLPathExtractor extracts values from XML using XPath expressions.

func (XMLPathExtractor) Extract

func (e XMLPathExtractor) Extract(_ ResponseContext) (any, error)

func (XMLPathExtractor) GetType

func (e XMLPathExtractor) GetType() spi.ExtractorType

func (XMLPathExtractor) Target added in v0.110.0

func (e XMLPathExtractor) Target() string

Target names the XML path the extractor reads.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL