Documentation ¶
Index ¶
- Constants
- Variables
- func GetEnvironment(ctx context.Context) map[string]string
- func ResetEnvironment(ctx context.Context)
- func WithEnvironment(ctx context.Context, env map[string]string) context.Context
- func WithLogger(ctx context.Context, logger Logger) context.Context
- func WithRequestRunner(ctx context.Context, runner RequestRunner) context.Context
- func WithRuntime(ctx context.Context, rt *Runtime) context.Context
- type Assertion
- type Header
- type Headers
- type Logger
- type Request
- type RequestRunner
- type Response
- type Runtime
Examples ¶
Constants ¶
View Source
const (
RequestSeparator = "###"
)
Variables ¶
View Source
var ( ErrInvalidRequest = errors.New("invalid request") ErrSkipped = errors.New("request skipped") )
Functions ¶
func ResetEnvironment ¶
func WithEnvironment ¶
func WithLogger ¶ added in v0.4.0
WithLogger returns a new context with the given logger. When a request is executed, the logger is used to log out the pre-request and post-request script logs
func WithRequestRunner ¶
func WithRequestRunner(ctx context.Context, runner RequestRunner) context.Context
Types ¶
type Logger ¶ added in v0.4.0
type Logger interface {
Log(...any)
}
Logger is the interface that wraps Log method. Log entries made in the pre-request and post-request scripts are passed to the Log method.
type Request ¶
type Request struct { // The name of the request // Example: Get User Name string // PreRequestScript is a piece of JavaScript code that executes before the request is sent. // Example: var token = req.environment.get("token"); PreRequestScript string // PreRequestAssertions is a list resulting from the assertions that were executed before the request is sent. PreRequestAssertions []Assertion // PostRequestScript is a piece of JavaScript code that executes after the request is sent. PostRequestScript string // The HTTP method used (GET, POST, PUT, DELETE, etc.) Method string // The URL of the request URL string // The HTTP body // Note: The body is not parsed. // Example: {"foo":"bar"} // Example: <xml><foo>bar</foo></xml> // Example: foo=bar&baz=qux Body string // The http Headers Headers Headers // Skip is a flag that indicates if the request should be skipped Skip bool // Logs is a list of logs generated by any scripts in the request Logs []string }
Request is a struct that holds the HTTP request data.
Example ¶
mux := http.NewServeMux() mux.HandleFunc("/users", func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(map[string]string{ "userId": "1234", }) }) srv := httptest.NewServer(mux) defer srv.Close() input := `### Get User < {% if(!getEnv('host')) { request.skip = true; } %} POST http://{{host}}/users Accept: application/json { "name": "Fred Flintstone" } < {% assert(response.statusCode === 201, 'status code is 201 Created') assert(response.json.userId != undefined, 'a user id is returned') %} ` reqs, err := ParseRequests(input) if err != nil { panic(err) } result, err := reqs[0].Do(WithEnvironment(context.Background(), map[string]string{ "host": srv.URL, })) if err != nil { panic(err) } fmt.Println(result.String())
Output:
func ParseFromFile ¶
func ParseRequests ¶
func (Request) DisplayName ¶
type Response ¶
func (*Response) PrettyString ¶
Source Files ¶
Click to show internal directories.
Click to hide internal directories.