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
- Variables
- func Body(resp *Response, raw bool) string
- func CheckPath(s string) error
- func Curl(r *Request) string
- func Format(resp *Response, more []string, raw bool) string
- func Headers(resp *Response, more []string) string
- func IsJSON(mt string) bool
- func IsSecretHeader(name string, more []string) bool
- func MaskValue(text, v string) string
- func Split(text string) (status, headers, body string, ok bool)
- type Assert
- type Capture
- type Check
- type Config
- type Error
- type Pair
- type Query
- type Request
- type Response
Constants ¶
const DefaultTimeout = 30 * time.Second
DefaultTimeout is how long a request may take when neither the block's timeout= nor [Options] says.
const Lang = "http"
Lang is the language of an http block's fence.
const Masked = "(secret)"
Masked stands for a credential header's value in the saved output.
const MaxBody = 8 << 20
MaxBody is the most of a response body mdsh reads; the rest is dropped, and the output says so.
Variables ¶
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.
var Methods = []string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "TRACE", "CONNECT"}
Methods are the request methods an http block takes.
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 Curl ¶
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 ¶
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 IsJSON ¶
IsJSON reports whether media type mt is JSON: application/json, or a type ending in +json.
func IsSecretHeader ¶
IsSecretHeader reports whether header name's value is masked: it is one of SecretHeaders or more, in any case.
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 ¶
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 ¶
Error is a problem in an http block's text, on a line counted from 1 at the block's first body line.
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.
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 (*Request) Expand ¶
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 ¶
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.