httpassert

package
v1.31.1 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: GPL-3.0 Imports: 20 Imported by: 0

README

httpassert

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

Index

Constants

const IgnoreJsonValue = "<IGNORE>"

func AssertJSONCanonicalEq

func AssertJSONCanonicalEq(t *testing.T, expected, actual string) bool

AssertJSONCanonicalEq compares two JSON strings by normalizing both first. On mismatch, it prints a readable diff of the normalized forms.

func NormalizeJSON

func NormalizeJSON(t *testing.T, s string) string

NormalizeJSON parses JSON and re-marshals it with stable key ordering and indentation. - Uses Decoder.UseNumber() to preserve numeric fidelity (avoid float64 surprises).

type Extractor

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

func ExtractRegexTo
func ExtractRegexTo(value string, ptr any) Extractor

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: JsonPath("$.data.name", httpassert.Contains("foo"))

func HasSize
func HasSize(e int) Matcher

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

func IsUUID
func IsUUID() Matcher

IsUUID checks if a string is a UUID Example: JsonPath("$.id", httpassert.IsUUID())

func NotEmpty
func NotEmpty() Matcher

NotEmpty checks if a string is not empty Example: JsonPath("$.data.name", httpassert.NotEmpty())

func Regex
func Regex(expr string) Matcher

Regex checks if a string matches the given regular expression Example: JsonPath("$.data.name", httpassert.Regex("^foo.*bar$"))

type Request

nolint:interfacebloat Request provides fluent request configuration

type Request interface {
    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
    ContentFile(string) Request

    JsonContent(string) Request
    JsonContentTemplate(body string, values map[string]any) Request
    JsonContentObject(any) Request
    JsonContentFile(path string) Request

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

type RequestStart

nolint:interfacebloat RequestStart provides fluent HTTP *method + path* selection. Each call returns a fresh Request

type RequestStart 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
}

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

New returns a new RequestStart instance for the given router. All method calls (Get/Post/...) return a *fresh* Request.

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

    Header(name string, value any) 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

func AssertJSONCanonicalEq added in v1.31.0

func AssertJSONCanonicalEq(t *testing.T, expected, actual string) bool

AssertJSONCanonicalEq compares two JSON strings by normalizing both first. On mismatch, it prints a readable diff of the normalized forms.

func NormalizeJSON added in v1.31.0

func NormalizeJSON(t *testing.T, s string) string

NormalizeJSON parses JSON and re-marshals it with stable key ordering and indentation. - Uses Decoder.UseNumber() to preserve numeric fidelity (avoid float64 surprises).

Types

type Extractor

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

func ExtractRegexTo added in v1.26.0

func ExtractRegexTo(value string, ptr any) Extractor

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: JsonPath("$.data.name", httpassert.Contains("foo"))

func HasSize

func HasSize(e int) Matcher

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

func IsUUID added in v1.31.0

func IsUUID() Matcher

IsUUID checks if a string is a UUID Example: JsonPath("$.id", httpassert.IsUUID())

func NotEmpty added in v1.30.0

func NotEmpty() Matcher

NotEmpty checks if a string is not empty Example: JsonPath("$.data.name", httpassert.NotEmpty())

func Regex added in v1.26.0

func Regex(expr string) Matcher

Regex checks if a string matches the given regular expression Example: JsonPath("$.data.name", httpassert.Regex("^foo.*bar$"))

type Request

type Request interface {
	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
	ContentFile(string) Request

	JsonContent(string) Request
	JsonContentTemplate(body string, values map[string]any) Request
	JsonContentObject(any) Request
	JsonContentFile(path string) Request

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

nolint:interfacebloat Request provides fluent request configuration

type RequestStart added in v1.31.0

type RequestStart 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
}

nolint:interfacebloat RequestStart provides fluent HTTP *method + path* selection. Each call returns a fresh Request

func New

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

New returns a new RequestStart instance for the given router. All method calls (Get/Post/...) return a *fresh* Request.

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

	Header(name string, value any) 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