httpassert

package
v1.24.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: GPL-3.0 Imports: 16 Imported by: 0

README

httpassert

import "github.com/greenbone/opensight-golang-libraries/pkg/httpassert"

Index

Constants

const IgnoreJsonValue = "<IGNORE>"

type Extractor

type Extractor func(t *testing.T, actual any) any

func ExtractTo
func ExtractTo(ptr any) Extractor

ExtractTo sets the value read from JSONPath into the given pointer variable. Example:

var id string
request.Expect().JsonPath("$.data.id", httpassert.ExtractTo(&id))

type Matcher

type Matcher func(t *testing.T, actual any) bool

func Contains
func Contains(v string) Matcher

Contains checks if a string contains the value Example: ExpectJsonPath("$.data.name", httpassert.Contains("foo"))

func HasSize
func HasSize(e int) Matcher

HasSize checks the length of arrays, maps, or strings. Example: ExpectJsonPath("$.data", httpassert.HasSize(11))

type Request

nolint:interfacebloat Request interface provides fluent HTTP request building.

type Request interface {
    Get(path string) Request
    Getf(format string, a ...interface{}) Request
    Post(path string) Request
    Postf(format string, a ...interface{}) Request
    Put(path string) Request
    Putf(format string, a ...interface{}) Request
    Delete(path string) Request
    Deletef(format string, a ...interface{}) Request
    Options(path string) Request
    Optionsf(format string, a ...interface{}) Request
    Patch(path string) Request
    Patchf(format string, a ...interface{}) Request

    Perform(verb string, path string) Request
    Performf(verb string, path string, a ...interface{}) Request

    AuthHeader(header string) Request
    Headers(headers map[string]string) Request
    Header(key, value string) Request
    AuthJwt(jwt string) Request

    ContentType(string) Request

    Content(string) Request
    JsonContent(string) Request
    JsonContentObject(any) Request
    ContentFile(string) Request
    JsonContentFile(path string) Request

    Expect() Response
    ExpectEventually(check func(r Response), timeout time.Duration, interval time.Duration) Response
}

func New
func New(t *testing.T, router http.Handler) Request

New returns a new Request instance for the given router.

type Response

nolint:interfacebloat Response interface provides fluent response assertions.

type Response interface {
    StatusCode(int) Response

    JsonPath(string, any) Response
    JsonPathJson(path string, expectedJson string) Response

    ContentType(contentType string) Response

    NoContent() Response

    Json(json string) Response
    JsonTemplate(json string, values map[string]any) Response
    JsonTemplateFile(path string, values map[string]any) Response
    JsonFile(path string) Response

    Body(body string) Response
    GetJsonBodyObject(target any) Response
    GetBody() string

    Log() Response
}

Generated by gomarkdoc

Documentation

Index

Constants

View Source
const IgnoreJsonValue = "<IGNORE>"

Variables

This section is empty.

Functions

This section is empty.

Types

type Extractor

type Extractor func(t *testing.T, actual any) any

func ExtractTo

func ExtractTo(ptr any) Extractor

ExtractTo sets the value read from JSONPath into the given pointer variable. Example:

var id string
request.Expect().JsonPath("$.data.id", httpassert.ExtractTo(&id))

type Matcher

type Matcher func(t *testing.T, actual any) bool

func Contains

func Contains(v string) Matcher

Contains checks if a string contains the value Example: ExpectJsonPath("$.data.name", httpassert.Contains("foo"))

func HasSize

func HasSize(e int) Matcher

HasSize checks the length of arrays, maps, or strings. Example: ExpectJsonPath("$.data", httpassert.HasSize(11))

type Request

type Request interface {
	Get(path string) Request
	Getf(format string, a ...interface{}) Request
	Post(path string) Request
	Postf(format string, a ...interface{}) Request
	Put(path string) Request
	Putf(format string, a ...interface{}) Request
	Delete(path string) Request
	Deletef(format string, a ...interface{}) Request
	Options(path string) Request
	Optionsf(format string, a ...interface{}) Request
	Patch(path string) Request
	Patchf(format string, a ...interface{}) Request

	Perform(verb string, path string) Request
	Performf(verb string, path string, a ...interface{}) Request

	AuthHeader(header string) Request
	Headers(headers map[string]string) Request
	Header(key, value string) Request
	AuthJwt(jwt string) Request

	ContentType(string) Request

	Content(string) Request
	JsonContent(string) Request
	JsonContentObject(any) Request
	ContentFile(string) Request
	JsonContentFile(path string) Request

	Expect() Response
	ExpectEventually(check func(r Response), timeout time.Duration, interval time.Duration) Response
}

nolint:interfacebloat Request interface provides fluent HTTP request building.

func New

func New(t *testing.T, router http.Handler) Request

New returns a new Request instance for the given router.

type Response

type Response interface {
	StatusCode(int) Response

	JsonPath(string, any) Response
	JsonPathJson(path string, expectedJson string) Response

	ContentType(contentType string) Response

	NoContent() Response

	Json(json string) Response
	JsonTemplate(json string, values map[string]any) Response
	JsonTemplateFile(path string, values map[string]any) Response
	JsonFile(path string) Response

	Body(body string) Response
	GetJsonBodyObject(target any) Response
	GetBody() string

	Log() Response
}

nolint:interfacebloat Response interface provides fluent response assertions.

Jump to

Keyboard shortcuts

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