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 ¶
- Variables
- func FirstGroup(pattern *regexp.Regexp) int
- func HTMLLinks(raw string) []string
- func HTMLText(raw string) string
- func RegisterExtractor(extType spi.ExtractorType, factory func([]byte) (AnyExtractor, error))
- type AnyExtractor
- type BodyExtractor
- type HTMLExtractor
- type HTMLReader
- type HeaderAccessor
- type JSONPathExtractor
- type ParsedBodyReader
- type QueryParamAccessor
- type RegexExtractor
- type ResponseContext
- type StatusReader
- type TextExtractor
- type XMLPathExtractor
Constants ¶
This section is empty.
Variables ¶
var ErrNotImplemented = errors.New("extractor not implemented")
Functions ¶
func FirstGroup ¶ added in v0.107.0
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 ¶ added in v0.107.0
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
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
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 ¶
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
type ParsedBodyReader ¶
ParsedBodyReader provides access to pre-parsed body (JSON, XML, etc.)
type QueryParamAccessor ¶ added in v0.64.0
QueryParamAccessor provides access to the query parameters of a received request.
type RegexExtractor ¶ added in v0.107.0
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
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 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