httpboomer

package module
v0.0.0-...-291b972 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2021 License: Apache-2.0 Imports: 20 Imported by: 0

README

HttpBoomer

Go Reference Github Actions codecov Go Report Card FOSSA Status

HttpBoomer = HttpRunner + Boomer

HttpBoomer is a golang implementation of HttpRunner. Ideally, HttpBoomer will be fully compatible with HttpRunner, including testcase format and usage. What's more, HttpBoomer will integrate Boomer natively to be a better load generator for locust.

Key Features

flow chart

  • Full support for HTTP(S) requests, more protocols are also in the plan.
  • Testcases can be described in multiple formats, YAML/JSON/Golang, and they are interchangeable.
  • With HAR support, you can use Charles/Fiddler/Chrome/etc as a script recording generator.
  • Supports variables/extract/validate/hooks mechanisms to create extremely complex test scenarios.
  • Built-in integration of rich functions, and you can also use go plugin to create and call custom functions.
  • Inherit all powerful features of Boomer and locust, you can run load test without extra work.
  • Use it as a CLI tool or as a library are both supported.

Quick Start

Install
$ go get -u github.com/httprunner/httpboomer
Examples

This is an example of HttpBoomer testcase. You can find more in the examples directory.

import (
    "testing"

    "github.com/httprunner/httpboomer"
)

func TestCaseDemo(t *testing.T) {
    testcase := &httpboomer.TestCase{
        Config: httpboomer.TConfig{
            Name:    "demo with complex mechanisms",
            BaseURL: "https://postman-echo.com",
            Variables: map[string]interface{}{ // global level variables
                "n":       5,
                "a":       12.3,
                "b":       3.45,
                "varFoo1": "${gen_random_string($n)}",
                "varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function
            },
        },
        TestSteps: []httpboomer.IStep{
            httpboomer.Step("get with params").
                WithVariables(map[string]interface{}{ // step level variables
                    "n":       3,                // inherit config level variables if not set in step level, a/varFoo1
                    "b":       34.5,             // override config level variable if existed, n/b/varFoo2
                    "varFoo2": "${max($a, $b)}", // 34.5; override variable b and eval again
                }).
                GET("/get").
                WithParams(map[string]interface{}{"foo1": "$varFoo1", "foo2": "$varFoo2"}). // request with params
                WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}).                 // request with headers
                Extract().
                WithJmesPath("body.args.foo1", "varFoo1"). // extract variable with jmespath
                Validate().
                AssertEqual("status_code", 200, "check response status code").        // validate response status code
                AssertStartsWith("headers.\"Content-Type\"", "application/json", ""). // validate response header
                AssertLengthEqual("body.args.foo1", 5, "check args foo1").            // validate response body with jmespath
                AssertLengthEqual("$varFoo1", 5, "check args foo1").                  // assert with extracted variable from current step
                AssertEqual("body.args.foo2", "34.5", "check args foo2"),             // notice: request params value will be converted to string
            httpboomer.Step("post json data").
                POST("/post").
                WithJSON(map[string]interface{}{
                    "foo1": "$varFoo1",       // reference former extracted variable
                    "foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here
                }).
                Validate().
                AssertEqual("status_code", 200, "check status code").
                AssertLengthEqual("body.json.foo1", 5, "check args foo1").
                AssertEqual("body.json.foo2", 12.3, "check args foo2"),
        },
    }

    err := httpboomer.Run(t, testcase)
    if err != nil {
        t.Fatalf("run testcase error: %v", err)
    }
}

Documentation

Index

Constants

View Source
const VERSION = "v0.1.0"

Variables

View Source
var ErrUnsupportedFileExt = fmt.Errorf("unsupported testcase file extension")

Functions

func Boom

func Boom(testcases ...ITestCase)

run load test with default configs

func Run

func Run(t *testing.T, testcases ...ITestCase) error

run API test with default configs

func Step

func Step(name string) *step

Types

type Boomer

type Boomer struct {
	*boomer.Boomer
	// contains filtered or unexported fields
}

func NewBoomer

func NewBoomer(masterHost string, masterPort int) *Boomer

func (*Boomer) Run

func (b *Boomer) Run(testcases ...ITestCase)

func (*Boomer) SetDebug

func (b *Boomer) SetDebug(debug bool) *Boomer

type EnumHTTPMethod

type EnumHTTPMethod string
const (
	GET     EnumHTTPMethod = "GET"
	HEAD    EnumHTTPMethod = "HEAD"
	POST    EnumHTTPMethod = "POST"
	PUT     EnumHTTPMethod = "PUT"
	DELETE  EnumHTTPMethod = "DELETE"
	OPTIONS EnumHTTPMethod = "OPTIONS"
	PATCH   EnumHTTPMethod = "PATCH"
)

type IStep

type IStep interface {
	Name() string
	Type() string
	ToStruct() *TStep
}

interface for all types of steps

type ITestCase

type ITestCase interface {
	ToTestCase() (*TestCase, error)
	ToTCase() (*TCase, error)
}

type ResponseObject

type ResponseObject struct {
	// contains filtered or unexported fields
}

func NewResponseObject

func NewResponseObject(t *testing.T, resp *req.Resp) (*ResponseObject, error)

func (*ResponseObject) Extract

func (v *ResponseObject) Extract(extractors map[string]string) map[string]interface{}

func (*ResponseObject) Validate

func (v *ResponseObject) Validate(validators []TValidator, variablesMapping map[string]interface{}) (err error)

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

func NewRunner

func NewRunner() *Runner

func (*Runner) GetSummary

func (r *Runner) GetSummary() *TestCaseSummary

func (*Runner) Run

func (r *Runner) Run(testcases ...ITestCase) error

func (*Runner) SetDebug

func (r *Runner) SetDebug(debug bool) *Runner

func (*Runner) SetProxyUrl

func (r *Runner) SetProxyUrl(proxyUrl string) *Runner

func (*Runner) WithTestingT

func (r *Runner) WithTestingT(t *testing.T) *Runner

type StepData

type StepData struct {
	Name       string                 // step name
	Success    bool                   // step execution result
	ExportVars map[string]interface{} // extract variables
}

type TCase

type TCase struct {
	Config    TConfig  `json:"config" yaml:"config"`
	TestSteps []*TStep `json:"teststeps" yaml:"teststeps"`
}

used for testcase json loading and dumping

func (*TCase) Dump2JSON

func (tc *TCase) Dump2JSON(path string) error

func (*TCase) Dump2YAML

func (tc *TCase) Dump2YAML(path string) error

func (*TCase) ToTestCase

func (tc *TCase) ToTestCase() (*TestCase, error)

type TConfig

type TConfig struct {
	Name       string                 `json:"name" yaml:"name"`
	Verify     bool                   `json:"verify,omitempty" yaml:"verify,omitempty"`
	BaseURL    string                 `json:"base_url,omitempty" yaml:"base_url,omitempty"`
	Variables  map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
	Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
	Export     []string               `json:"export,omitempty" yaml:"export,omitempty"`
	Weight     int                    `json:"weight,omitempty" yaml:"weight,omitempty"`
}

type TRequest

type TRequest struct {
	Method         EnumHTTPMethod         `json:"method" yaml:"method"`
	URL            string                 `json:"url" yaml:"url"`
	Params         map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"`
	Headers        map[string]string      `json:"headers,omitempty" yaml:"headers,omitempty"`
	Cookies        map[string]string      `json:"cookies,omitempty" yaml:"cookies,omitempty"`
	Body           interface{}            `json:"body,omitempty" yaml:"body,omitempty"`
	Timeout        float32                `json:"timeout,omitempty" yaml:"timeout,omitempty"`
	AllowRedirects bool                   `json:"allow_redirects,omitempty" yaml:"allow_redirects,omitempty"`
	Verify         bool                   `json:"verify,omitempty" yaml:"verify,omitempty"`
}

type TStep

type TStep struct {
	Name          string                 `json:"name" yaml:"name"`
	Request       *TRequest              `json:"request,omitempty" yaml:"request,omitempty"`
	TestCase      *TestCase              `json:"testcase,omitempty" yaml:"testcase,omitempty"`
	Variables     map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
	SetupHooks    []string               `json:"setup_hooks,omitempty" yaml:"setup_hooks,omitempty"`
	TeardownHooks []string               `json:"teardown_hooks,omitempty" yaml:"teardown_hooks,omitempty"`
	Extract       map[string]string      `json:"extract,omitempty" yaml:"extract,omitempty"`
	Validators    []TValidator           `json:"validate,omitempty" yaml:"validate,omitempty"`
	Export        []string               `json:"export,omitempty" yaml:"export,omitempty"`
}

type TValidator

type TValidator struct {
	Check   string      `json:"check,omitempty" yaml:"check,omitempty"` // get value with jmespath
	Assert  string      `json:"assert,omitempty" yaml:"assert,omitempty"`
	Expect  interface{} `json:"expect,omitempty" yaml:"expect,omitempty"`
	Message string      `json:"msg,omitempty" yaml:"msg,omitempty"`
}

type TestCase

type TestCase struct {
	Config    TConfig
	TestSteps []IStep
}

used for testcase runner

func (*TestCase) ToTCase

func (tc *TestCase) ToTCase() (*TCase, error)

func (*TestCase) ToTestCase

func (tc *TestCase) ToTestCase() (*TestCase, error)

type TestCasePath

type TestCasePath struct {
	Path string
}

func (*TestCasePath) ToTCase

func (path *TestCasePath) ToTCase() (*TCase, error)

func (*TestCasePath) ToTestCase

func (path *TestCasePath) ToTestCase() (*TestCase, error)

type TestCaseSummary

type TestCaseSummary struct{}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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