http

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2021 License: Apache-2.0 Imports: 20 Imported by: 5

README

Venom - Executor HTTP

Step for execute a HTTP Request

Input

In your yaml file, you can use:

  - method optional, default value: GET
  - url mandatory
  - unix_sock optional
  - path optional
  - body optional
  - bodyFile optional
  - headers optional
  - proxy optional: set to use a proxy server for connection to url
  - resolve optional: array of custom resolver of host and port pair. example: foo.com:443:127.0.0.1
  - ignore_verify_ssl optional: set to true if you use a self-signed SSL on remote for example
  - basic_auth_user optional: username to use for HTTP basic authentification
  - basic_auth_password optional: password to use for HTTP basic authentification
  - no_follow_redirect optional: indicates that you don't want to follow Location if server returns a Redirect (301/302/...)
  - skip_body: skip the body and bodyjson result
  - skip_headers: skip the headers result
  - tls_client_cert optional: a chain of certificates to identify the caller, first certificate in the chain is considered as the leaf, followed by intermediates. Setting it enable mutual TLS authentication
  - tls_client_key optional: private key corresponding to the certificate.
  - tls_root_ca optional: defines additional root CAs to perform the call. can contains multiple CAs concatenated together


name: HTTP testsuite
testcases:
- name: get http testcase
  steps:
  - type: http
    method: GET
    url: https://eu.api.ovh.com/1.0/
    assertions:
    - result.body ShouldContainSubstring /dedicated/server
    - result.body ShouldContainSubstring /ipLoadbalancing
    - result.statuscode ShouldEqual 200
    - result.bodyjson.api ShouldBeNil
    - result.bodyjson.apis ShouldNotBeEmpty
    - result.bodyjson.apis.apis0 ShouldNotBeNil
    - result.bodyjson.apis.apis0.path ShouldEqual /allDom

- name: post http multipart
  steps:
  - type: http
    method: POST
    url: https://eu.api.ovh.com/1.0/auth/logout
    multipart_form:
      file: '@./venom.gif'
    assertions:
    - result.statuscode ShouldEqual 401

- name: post http enhanced assertions
  steps:
  - type: http
    method: POST
    url: https://eu.api.ovh.com/1.0/newAccount/rules
    assertions:
      - result.statuscode ShouldEqual 200
      - result.bodyjson.__type__ ShouldEqual Array
      # Ensure a minimum of fields are present.
      - result.bodyjson.__len__ ShouldBeGreaterThanOrEqualTo 8
      # Ensure fields have the right keys.
      - result.bodyjson.bodyjson0 ShouldContainKey fieldName
      - result.bodyjson.bodyjson0 ShouldContainKey mandatory
      - result.bodyjson.bodyjson0 ShouldContainKey regularExpression
      - result.bodyjson.bodyjson0 ShouldContainKey prefix
      - result.bodyjson.bodyjson0 ShouldContainKey examples
      - result.bodyjson.bodyjson0 ShouldNotContainKey lol
      - result.statuscode ShouldNotEqual {{.post-http-multipart.result.statuscode}}

- name: get http (with options)
  steps:
  - type: http
    method: POST
    url: https://eu.api.ovh.com/1.0
    skip_body: true
    skip_headers: true
    info: request is {{.result.request.method}} {{.result.request.url}} {{.result.request.body}}
    assertions:
      - result.statuscode ShouldEqual 405
      - result.body ShouldBeEmpty
      - result.headers ShouldBeEmpty


NB: to post a file with multipart_form, prefix the path to the file with '@'

Output

result.request
result.timeseconds
result.statuscode
result.body
result.bodyjson
result.headers
result.err
  • result.timeseconds: execution duration
  • result.request.method: HTTP method of the request
  • result.request.url: HTTP URL of the request
  • result.request.body: body content as string
  • result.request.form: HTTP form map
  • result.request.post_form: HTTP post form map
  • result.err: if exists, this field contains error
  • result.body: body of HTTP response
  • result.bodyjson: body of HTTP response if it's a JSON. You can access json data as result.bodyjson.yourkey for example.
  • result.headers: headers of HTTP response
  • result.statuscode: Status Code of HTTP response
JSON keys

JSON keys are lowercased automatically (eg. use result.bodyjson.yourkey, not result.bodyjson.YourKey).

On top of that, if a JSON key contains special characters, they will be translate to underscores.

JSON arrays

When a HTTP response contains a JSON array, you have to use following syntax to access specific key of an array: result.bodyjson.array_name.array_name_index_in_array.key

Example if you want to get value of path key of second element in apis array: result.bodyjson.apis.apis1.path

Default assertion

result.statuscode ShouldEqual 200

Documentation

Index

Constants

View Source
const Name = "http"

Name of executor

Variables

This section is empty.

Functions

func GetTransport added in v1.0.0

func GetTransport(opts ...func(*http.Transport) error) (*http.Transport, error)

func New

func New() venom.Executor

New returns a new Executor

func WithProxyFromEnv added in v1.0.0

func WithProxyFromEnv() func(*http.Transport) error

func WithTLSClientAuth added in v1.0.0

func WithTLSClientAuth(cert tls.Certificate) func(*http.Transport) error

func WithTLSInsecureSkipVerify added in v1.0.0

func WithTLSInsecureSkipVerify(v bool) func(*http.Transport) error

func WithTLSRootCA added in v1.0.0

func WithTLSRootCA(ctx context.Context, caCert []byte) func(*http.Transport) error

WithTLSRootCA should be called only once, with multiple PEM encoded certificates as input if needed.

Types

type Executor

type Executor struct {
	Method            string      `json:"method" yaml:"method"`
	URL               string      `json:"url" yaml:"url"`
	Path              string      `json:"path" yaml:"path"`
	Body              string      `json:"body" yaml:"body"`
	BodyFile          string      `json:"bodyfile" yaml:"bodyfile"`
	MultipartForm     interface{} `json:"multipart_form" yaml:"multipart_form"`
	Headers           Headers     `json:"headers" yaml:"headers"`
	IgnoreVerifySSL   bool        `json:"ignore_verify_ssl" yaml:"ignore_verify_ssl" mapstructure:"ignore_verify_ssl"`
	BasicAuthUser     string      `json:"basic_auth_user" yaml:"basic_auth_user" mapstructure:"basic_auth_user"`
	BasicAuthPassword string      `json:"basic_auth_password" yaml:"basic_auth_password" mapstructure:"basic_auth_password"`
	SkipHeaders       bool        `json:"skip_headers" yaml:"skip_headers" mapstructure:"skip_headers"`
	SkipBody          bool        `json:"skip_body" yaml:"skip_body" mapstructure:"skip_body"`
	Proxy             string      `json:"proxy" yaml:"proxy" mapstructure:"proxy"`
	Resolve           []string    `json:"resolve" yaml:"resolve" mapstructure:"resolve"`
	NoFollowRedirect  bool        `json:"no_follow_redirect" yaml:"no_follow_redirect" mapstructure:"no_follow_redirect"`
	UnixSock          string      `json:"unix_sock" yaml:"unix_sock" mapstructure:"unix_sock"`
	TLSClientCert     string      `json:"tls_client_cert" yaml:"tls_client_cert" mapstructure:"tls_client_cert"`
	TLSClientKey      string      `json:"tls_client_key" yaml:"tls_client_key" mapstructure:"tls_client_key"`
	TLSRootCA         string      `json:"tls_root_ca" yaml:"tls_root_ca" mapstructure:"tls_root_ca"`
}

Executor struct. Json and yaml descriptor are used for json output

func (Executor) GetDefaultAssertions

func (Executor) GetDefaultAssertions() *venom.StepAssertions

GetDefaultAssertions return default assertions for this executor

func (Executor) Run

func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, error)

Run execute TestStep

func (Executor) ZeroValueResult added in v0.17.0

func (Executor) ZeroValueResult() interface{}

ZeroValueResult return an empty implementation of this executor result

type HTTPRequest added in v1.0.0

type HTTPRequest struct {
	Method   string      `json:"method,omitempty"`
	URL      string      `json:"url,omitempty"`
	Header   http.Header `json:"headers,omitempty"`
	Body     string      `json:"body,omitempty"`
	Form     url.Values  `json:"form,omitempty"`
	PostForm url.Values  `json:"post_form,omitempty"`
}

type Headers

type Headers map[string]string

Headers represents header HTTP for Request

type Result

type Result struct {
	TimeSeconds float64     `json:"timeseconds,omitempty" yaml:"timeseconds,omitempty"`
	StatusCode  int         `json:"statuscode,omitempty" yaml:"statuscode,omitempty"`
	Request     HTTPRequest `json:"request,omitempty" yaml:"request,omitempty"`
	Body        string      `json:"body,omitempty" yaml:"body,omitempty"`
	BodyJSON    interface{} `json:"bodyjson,omitempty" yaml:"bodyjson,omitempty"`
	Headers     Headers     `json:"headers,omitempty" yaml:"headers,omitempty"`
	Err         string      `json:"err,omitempty" yaml:"err,omitempty"`
}

Result represents a step result. Json and yaml descriptor are used for json output

Jump to

Keyboard shortcuts

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