httputils

package
v0.312.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

README

httputils

High level easy to use HTTP client and testserver.

Documentation

Index

Constants

View Source
const STATUS_CODE_OK = 200

Variables

View Source
var ErrWebServerAlreadyRunning = errors.New("web server already running")

Functions

func MustSendRequestAndGetBodyAsString

func MustSendRequestAndGetBodyAsString(requestOptions *RequestOptions) (response string)

func SendRequestAndGetBodyAsString

func SendRequestAndGetBodyAsString(requestOptions *RequestOptions) (response string, err error)

Types

type Client

type Client interface {
	DownloadAsFile(downloadOptions *DownloadAsFileOptions) (downloadedFile files.File, err error)
	DownloadAsTemporaryFile(downloadOptions *DownloadAsFileOptions) (downloadedFile files.File, err error)
	SendRequest(requestOptions *RequestOptions) (response Response, err error)
	SendRequestAndGetBodyAsString(requestOptions *RequestOptions) (responseBody string, err error)
	SendRequestAndRunYqQueryAgainstBody(requestOptions *RequestOptions, query string) (result string, err error)
}

func GetNativeClient

func GetNativeClient() (client Client)

Get the HTTP client written using native go http implementation.

This is the default client to use when sending request from your running machine.

type DownloadAsFileOptions

type DownloadAsFileOptions struct {
	RequestOptions    *RequestOptions
	OutputPath        string
	OverwriteExisting bool
	Verbose           bool

	// If Sha256Sum is set:
	// - The download will be skipped if OutputPath has already the expected content.
	// - The download is validated.
	Sha256Sum string
}

func NewDownloadAsFileOptions

func NewDownloadAsFileOptions() (d *DownloadAsFileOptions)

func (*DownloadAsFileOptions) GetDeepCopy

func (d *DownloadAsFileOptions) GetDeepCopy() (copy *DownloadAsFileOptions)

func (*DownloadAsFileOptions) GetOutputPath

func (d *DownloadAsFileOptions) GetOutputPath() (outputPath string, err error)

func (*DownloadAsFileOptions) GetOverwriteExisting

func (d *DownloadAsFileOptions) GetOverwriteExisting() (overwriteExisting bool)

func (*DownloadAsFileOptions) GetRequestOptions

func (d *DownloadAsFileOptions) GetRequestOptions() (requestOptions *RequestOptions, err error)

func (*DownloadAsFileOptions) GetVerbose

func (d *DownloadAsFileOptions) GetVerbose() (verbose bool)

func (*DownloadAsFileOptions) SetOutputPath

func (d *DownloadAsFileOptions) SetOutputPath(outputPath string) (err error)

func (*DownloadAsFileOptions) SetOverwriteExisting

func (d *DownloadAsFileOptions) SetOverwriteExisting(overwriteExisting bool)

func (*DownloadAsFileOptions) SetRequestOptions

func (d *DownloadAsFileOptions) SetRequestOptions(requestOptions *RequestOptions) (err error)

func (*DownloadAsFileOptions) SetVerbose

func (d *DownloadAsFileOptions) SetVerbose(verbose bool)

type GenericResponse

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

This is the generic response type. It can also be seen as the default response to use.

func NewGenericResponse

func NewGenericResponse() (g *GenericResponse)

func (*GenericResponse) GetBody

func (g *GenericResponse) GetBody() (body []byte, err error)

func (*GenericResponse) GetBodyAsString

func (g *GenericResponse) GetBodyAsString() (body string, err error)

func (*GenericResponse) GetStatusCode

func (g *GenericResponse) GetStatusCode() (statusCode int, err error)

func (*GenericResponse) IsStatusCodeOk

func (g *GenericResponse) IsStatusCodeOk() (isStatusCodeOk bool, err error)

func (*GenericResponse) RunYqQueryAgainstBody

func (g *GenericResponse) RunYqQueryAgainstBody(query string) (result string, err error)

func (*GenericResponse) SetBody

func (g *GenericResponse) SetBody(body []byte) (err error)

func (*GenericResponse) SetStatusCode

func (g *GenericResponse) SetStatusCode(statusCode int) (err error)

type NativeClient

type NativeClient struct {
}

HTTP client written using native go http implementation.

func NewNativeClient

func NewNativeClient() (n *NativeClient)

func (*NativeClient) DownloadAsFile

func (n *NativeClient) DownloadAsFile(downloadOptions *DownloadAsFileOptions) (downloadedFile files.File, err error)

func (*NativeClient) DownloadAsTemporaryFile

func (n *NativeClient) DownloadAsTemporaryFile(downloadOptions *DownloadAsFileOptions) (downloadedFile files.File, err error)

func (*NativeClient) SendRequest

func (c *NativeClient) SendRequest(requestOptions *RequestOptions) (response Response, err error)

func (*NativeClient) SendRequestAndGetBodyAsString

func (c *NativeClient) SendRequestAndGetBodyAsString(requestOptions *RequestOptions) (responseBody string, err error)

func (*NativeClient) SendRequestAndRunYqQueryAgainstBody

func (c *NativeClient) SendRequestAndRunYqQueryAgainstBody(requestOptions *RequestOptions, query string) (result string, err error)

type RequestOptions

type RequestOptions struct {
	// Url to request
	Url string

	// Port to use.
	// Overrides the port defined in URL if specified.
	Port int

	// Request method like GET, POST...
	Method string

	// Skip TLS validation
	SkipTLSvalidation bool

	// Enable verbose output
	Verbose bool
}

func NewRequestOptions

func NewRequestOptions() (r *RequestOptions)

func (*RequestOptions) GetDeepCopy

func (r *RequestOptions) GetDeepCopy() (copy *RequestOptions)

func (*RequestOptions) GetMethod

func (r *RequestOptions) GetMethod() (method string, err error)

func (*RequestOptions) GetMethodOrDefault

func (r *RequestOptions) GetMethodOrDefault() (method string, err error)

func (*RequestOptions) GetPort

func (r *RequestOptions) GetPort() (port int, err error)

func (*RequestOptions) GetSkipTLSvalidation

func (r *RequestOptions) GetSkipTLSvalidation() (skipTLSvalidation bool)

func (*RequestOptions) GetUrl

func (r *RequestOptions) GetUrl() (url string, err error)

func (*RequestOptions) GetVerbose

func (r *RequestOptions) GetVerbose() (verbose bool)

func (*RequestOptions) IsMethodSet

func (r *RequestOptions) IsMethodSet() (isSet bool)

func (*RequestOptions) SetMethod

func (r *RequestOptions) SetMethod(method string) (err error)

func (*RequestOptions) SetPort

func (r *RequestOptions) SetPort(port int) (err error)

func (*RequestOptions) SetSkipTLSvalidation

func (r *RequestOptions) SetSkipTLSvalidation(skipTLSvalidation bool)

func (*RequestOptions) SetUrl

func (r *RequestOptions) SetUrl(url string) (err error)

func (*RequestOptions) SetVerbose

func (r *RequestOptions) SetVerbose(verbose bool)

type Response

type Response interface {
	GetBodyAsString() (body string, err error)
	IsStatusCodeOk() (isStatusCodeOK bool, err error)
	SetBody(body []byte) (err error)
	SetStatusCode(statusCode int) (err error)
	RunYqQueryAgainstBody(query string) (result string, err error)
}

type Server

type Server interface {
	GetPort() (port int, err error)
	StartInBackground(verbose bool) (err error)
	Stop(verbose bool) (err error)
}

func GetTestWebServer

func GetTestWebServer(port int) (webServer Server, err error)

func GetTlsTestWebServer

func GetTlsTestWebServer(ctx context.Context, port int) (webServer Server, err error)

type TestWebServer

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

A simple webserver mostly used for testing.

func NewTestWebServer

func NewTestWebServer() (t *TestWebServer)

func (*TestWebServer) GetMux

func (t *TestWebServer) GetMux() (mux *http.ServeMux, err error)

func (*TestWebServer) GetPort

func (t *TestWebServer) GetPort() (port int, err error)

func (*TestWebServer) GetServer

func (t *TestWebServer) GetServer() (server *http.Server, err error)

func (*TestWebServer) GetTlsCert

func (t *TestWebServer) GetTlsCert() (cert *x509.Certificate, err error)

func (*TestWebServer) GetWebServerWaitGroup

func (t *TestWebServer) GetWebServerWaitGroup() (webServerWaitGroup *sync.WaitGroup, err error)

func (*TestWebServer) SetMux

func (t *TestWebServer) SetMux(mux *http.ServeMux) (err error)

func (*TestWebServer) SetPort

func (t *TestWebServer) SetPort(port int) (err error)

func (*TestWebServer) SetServer

func (t *TestWebServer) SetServer(server *http.Server) (err error)

func (*TestWebServer) SetTlsCertAndKey

func (t *TestWebServer) SetTlsCertAndKey(ctx context.Context, certAndKey *x509utils.X509CertKeyPair) (err error)

func (*TestWebServer) SetWebServerWaitGroup

func (t *TestWebServer) SetWebServerWaitGroup(webServerWaitGroup *sync.WaitGroup) (err error)

func (*TestWebServer) StartInBackground

func (t *TestWebServer) StartInBackground(verbose bool) (err error)

func (*TestWebServer) Stop

func (t *TestWebServer) Stop(verbose bool) (err error)

Jump to

Keyboard shortcuts

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