httpblock

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 17 Imported by: ? modules, 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 httpblock reads and runs http blocks: a request written the way it goes over the wire, an expected status, and Hurl's checks and captures (see docs/http/design.md).

```http {#login}
POST {{$API}}/sessions
Content-Type: application/json

{"user": "j"}

HTTP 201
[Asserts]
jsonpath $.token exists
[Captures]
token: jsonpath $.token
```

Index

Constants

View Source
const DefaultTimeout = 30 * time.Second

DefaultTimeout is how long a request may take when neither the block's timeout= nor [Options] says.

View Source
const Lang = "http"

Lang is the language of an http block's fence.

View Source
const Masked = "(secret)"

Masked stands for a credential header's value in the saved output.

View Source
const MaxBody = 8 << 20

MaxBody is the most of a response body mdsh reads; the rest is dropped, and the output says so.

Variables

View Source
var (
	Whats = []string{"status", "header", "jsonpath", "body", "regex", "duration"}
	Ops   = []string{"==", "!=", "<", "<=", ">", ">=", "contains", "startsWith", "endsWith", "matches", "exists", "isEmpty", "isNumber", "isString", "isBoolean", "isCollection"}
)

Whats are the queries checks and captures read, and Ops the predicates checks use, in Hurl's words.

View Source
var Methods = []string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "TRACE", "CONNECT"}

Methods are the request methods an http block takes.

View Source
var SecretHeaders = []string{"Set-Cookie", "Authorization", "Proxy-Authorization", "WWW-Authenticate", "X-API-Key"}

SecretHeaders are the headers whose values are always masked in the output, since they carry a credential. The front matter's mdsh.http.secret_headers adds to them; nothing takes one off.

Functions

func Body

func Body(resp *Response, raw bool) string

Body is the response body as Format writes it.

func CheckPath

func CheckPath(s string) error

CheckPath reports whether s is a JSONPath checks can read.

func Curl

func Curl(r *Request) string

Curl is r as a curl command line, for pasting into a shell: r is usually expanded first, so it holds the values the request would send.

func Format

func Format(resp *Response, more []string, raw bool) string

Format is the response as the block's output shows it and the file saves it: the status line, the headers sorted by name with the credential ones (SecretHeaders and more) masked, a blank line and the body. A JSON body is pretty-printed unless raw; a body that isn't text is described, not shown.

func Headers

func Headers(resp *Response, more []string) string

Headers is the response's header lines, as Format writes them.

func IsJSON

func IsJSON(mt string) bool

IsJSON reports whether media type mt is JSON: application/json, or a type ending in +json.

func IsSecretHeader

func IsSecretHeader(name string, more []string) bool

IsSecretHeader reports whether header name's value is masked: it is one of SecretHeaders or more, in any case.

func MaskValue

func MaskValue(text, v string) string

MaskValue returns text with every copy of v, a secret capture's value, replaced by Masked: as it is, and as a JSON string holds it, since the body may escape it (\u003c for <, \/ for /, \" for ") where the capture read it unescaped.

func Split

func Split(text string) (status, headers, body string, ok bool)

Split cuts a response as Format wrote it, or as a file saved it, into its status line, its header lines and its body. ok is false when text doesn't start with an HTTP status line.

Types

type Assert

type Assert struct {
	Query
	Not bool
	Op  string
	// Value is the value to compare with: a JSON literal, or a bare
	// word, as written.
	Value string
	// Text is the line as written, to report it.
	Text string
}

Assert is one line of [Asserts]: a query, a predicate and, for those that compare, a value.

func ParseAssert

func ParseAssert(line string) (Assert, error)

ParseAssert reads one [Asserts] line: QUERY [count] [not] OP [VALUE].

type Capture

type Capture struct {
	Name string
	Query
	// Secret is set by secret before the query, or redact after it.
	Secret bool
}

Capture is one line of [Captures]: name: QUERY.

type Check

type Check struct {
	Text string // as written: HTTP 200, or the [Asserts] line
	OK   bool
	// Got is what the response had, when the check failed.
	Got string
}

Check is one check's result.

type Config

type Config struct {
	// Timeout overrides [Options]' timeout and DefaultTimeout when set.
	Timeout time.Duration
	// ReadFile reads an @file body, as the document's includes are
	// read. Nil refuses @file bodies.
	ReadFile func(name string) ([]byte, error)
	// Transport, if set, makes the request instead of a transport with
	// the system's proxy and certificates. Tests use it.
	Transport http.RoundTripper
	// SecretHeaders are headers masked in the output besides
	// SecretHeaders, from the front matter's mdsh.http.secret_headers.
	// A failed check on one of them reports Masked, not the value.
	SecretHeaders []string
}

Config is how Do makes a request.

type Error

type Error struct {
	Line int
	Msg  string
}

Error is a problem in an http block's text, on a line counted from 1 at the block's first body line.

func (*Error) Error

func (e *Error) Error() string

type Pair

type Pair struct {
	Name, Value string
}

Pair is a name and value, as a header or a parameter.

type Query

type Query struct {
	What string // status, header, jsonpath, body, regex, duration
	Arg  string // the header name, path or pattern, unquoted
	// Count is the count filter: the number of items the query found.
	Count bool
}

Query is what a check or a capture reads from the response: status, header NAME, jsonpath PATH, body, regex PATTERN or duration, optionally followed by the count filter.

func (Query) String

func (q Query) String() string

type Request

type Request struct {
	Method string
	URL    string
	// Headers are the request's headers, in order.
	Headers []Pair
	// Query are [QueryStringParams] lines, added to the URL.
	Query []Pair
	// Form are [FormParams] lines, sent as a form body.
	Form []Pair
	// Body is the raw body, without the blank lines around it. "" when
	// there is none.
	Body string
	// BodyFile is the file named by an @file body, relative to the
	// document. "" when the body isn't one.
	BodyFile string
	// Insecure and Follow are [Options]' insecure and follow.
	Insecure, Follow bool
	// Timeout is [Options]' timeout, or 0.
	Timeout time.Duration
	// Status is the status HTTP NNN expects, or 0 when there is no
	// HTTP line or it says HTTP *.
	Status int
	// StatusLine is the HTTP line as written, or "".
	StatusLine string
	Asserts    []Assert
	Captures   []Capture
}

Request is an http block as written. Its text still holds the placeholders; Expand fills them in.

func Parse

func Parse(src string) (*Request, error)

Parse reads an http block's body.

func (*Request) Expand

func (r *Request) Expand(fill func(string) (string, error)) (*Request, error)

Expand returns a copy of r with fill run over every text that can hold a placeholder: the URL, header values, parameters, the body, the body file's name and the values checks compare with. A header or a URL that fills in with a line break is an error, so a value can't add a header of its own.

type Response

type Response struct {
	Proto  string // HTTP/1.1
	Status string // 201 Created
	Code   int
	Header http.Header
	Body   []byte
	// Truncated is set when the body was longer than MaxBody.
	Truncated bool
	Duration  time.Duration
	// Checks are the status check, if any, then each [Asserts] line,
	// in order.
	Checks []Check
	// Vars are the captures' values; Secret names the captures marked
	// secret.
	Vars   map[string]string
	Secret []string
	// CaptureErrs are the captures that found nothing, said as for
	// stderr.
	CaptureErrs []string
}

Response is what a request got back, with its checks' and captures' results.

func Do

func Do(ctx context.Context, r *Request, cfg Config) (*Response, error)

Do makes the request r, an expanded Request, and runs its checks and captures on the response. The error is for a request that got no response; failed checks are in the Response. The error names the URL's scheme and host only: its path, query and user info hold the placeholders' values, which the saved file and the audit log, where the error goes, must not.

func (*Response) Failed

func (r *Response) Failed() bool

Failed reports whether a check failed or a capture found nothing.

func (*Response) Failures

func (r *Response) Failures() []string

Failures are the lines a failed run reports on stderr: one per failed check, then one per capture that found nothing.

func (*Response) Lookup

func (resp *Response) Lookup(fn, arg string) (value string, ok bool)

Lookup reads a response for a placeholder or a condition: fn is "json" with a path, "header" with a name, or "status". ok is false when there is nothing there. A value that isn't a string is JSON.

Jump to

Keyboard shortcuts

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