testing

package
v0.1.386 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: Apache-2.0 Imports: 38 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CombineTransports

func CombineTransports(transports ...http.RoundTripper) http.RoundTripper

CombineTransports returns a transport that delegates to the given array of transports, in the given order. First request will go to the first transport, sceond request to the second transport, so on.

func DefaultJWKS

func DefaultJWKS() []byte

DefaultJWKS generates the JSON web key set used for tests.

func ErrorTransport

func ErrorTransport(err error) http.RoundTripper

ErrorTransport creates a transport that always returns the given error.

func EvaluateTemplate

func EvaluateTemplate(source string, args ...interface{}) string

EvaluateTemplate generates a string from the given templlate source and name value pairs. For example the following code:

EvaluateTemplate(
	`{
		"access_token": "{{ .AccessToken }}"
		"refresh_token": "{{ .RefreshToken }}"
	}`,
	"AccessToken", "myaccesstoken",
	"RefreshToken", "myrefreshtoken",
)

Will generate the following string:

{
	"access_token": "myaccesstoken"
	"access_token": "myrefreshtoken"
}

To simplify embeding of the templates in Go source the function also removes the leading tabs from the generated text.

func JQ

func JQ(filter string, input interface{}) (results []interface{}, err error)

JQ runs the given `jq` filter on the given object and returns the list of results. The returned slice will never be nil; if there are no results it will be empty.

func JSONTransport

func JSONTransport(code int, body string) http.RoundTripper

JSONTransport creates a transport that always returns the given status code and JSON body.

func LocalhostCertificate

func LocalhostCertificate() tls.Certificate

LocalhostCertificate returns a self signed TLS certificate valid for the name `localhost` DNS name, for the `127.0.0.1` IPv4 address and for the `::1` IPv6 address.

A similar certificate can be generated with the following command:

openssl req \
-x509 \
-newkey rsa:4096 \
-nodes \
-keyout tls.key \
-out tls.crt \
-subj '/CN=localhost' \
-addext 'subjectAltName=DNS:localhost,IP:127.0.0.1,IP:::1' \
-days 1

func MakeClaims

func MakeClaims() jwt.MapClaims

MakeClaims generates a default set of claims to be used to issue a token.

func MakeTCPH2CServer

func MakeTCPH2CServer() *ghttp.Server

MakeTCPH2CServer creates a test server that supports HTTP/2 without TLS, configured so that it sends log messages to the Ginkgo writer.

func MakeTCPServer

func MakeTCPServer() *ghttp.Server

MakeTCPServer creates a test server that listens in a TCP socket and configured so that it sends log messages to the Ginkgo writer.

func MakeTCPTLSServer

func MakeTCPTLSServer() (server *ghttp.Server, ca string)

MakeTCPTLSServer creates a test server configured so that it sends log messages to the Ginkgo writer. It returns the created server and the name of a temporary file that contains the CA certificate that the client should trust in order to connect to the server. It is the responsibility of the caller to delete this temporary file when it is no longer needed.

func MakeTokenObject

func MakeTokenObject(claims jwt.MapClaims) *jwt.Token

MakeTokenObject generates a token with the claims resulting from merging the default claims and the claims explicitly given.

func MakeTokenString

func MakeTokenString(typ string, life time.Duration) string

MakeTokenString generates a token issued by the default OpenID server and with the given type and with the given life. If the life is zero the token will never expire. If the life is positive the token will be valid, and expire after that time. If the life is negative the token will be already expired that time ago.

func MakeUnixH2CServer

func MakeUnixH2CServer() (server *ghttp.Server, socket string)

MakeUnixH2cServer creates a test server that listens in a Unix socket and supports HTTP/2 without TLS, configured so that it sends log messages to the Ginkgo writer. It returns the created server and name of a temporary file containing the Unix socket. This file will be in a temporary directory, and the caller is resposible for removing the directory once it is no longer needed.

func MakeUnixServer

func MakeUnixServer() (server *ghttp.Server, socket string)

MakeUnixServer creates a test server that listens in a Unix socket and configured so that it sends log messages to the Ginkgo writer. It returns the created server and name of a temporary file containing the Unix socket. This file will be in a temporary directory, and the caller is resposible for removing the directory once it is no longer needed.

func MakeUnixTLSServer

func MakeUnixTLSServer() (server *ghttp.Server, ca, socket string)

MakeUnixTLSServer creates a test server that listens in a Unix socket and configured so that it sends log messages to the Ginkgo writer. It returns the created server, the name of a temporary file that contains the CA certificate that the client should trust in order to connect to the server and the name of a directory containing the Unix sockets. This file will be in a temporary directory. It is the responsibility of the caller to remove these temporary directories and files.

func MatchJQ

func MatchJQ(filter string, expected interface{}) types.GomegaMatcher

MatchJQ creates a matcher that checks that the all the results of applying a `jq` filter to the actual value is the given expected value.

func MatchJSONTemplate

func MatchJSONTemplate(template string, args ...interface{}) types.GomegaMatcher

MatchJSONTemplate succeeds if actual is a string or stringer of JSON that matches the result of evaluating the given template with the given arguments.

func MatchLine

func MatchLine(regexp string, args ...interface{}) OmegaMatcher

MatchLine succeeds if actual is an slice of strings that contains at least one items that matches the passed regular expression.

func RemoveLeadingTabs

func RemoveLeadingTabs(s string) string

RemoveLeadingTabs removes the leading tabs from the lines of the given string.

func RespondWithAccessAndRefreshTokens

func RespondWithAccessAndRefreshTokens(accessToken, refreshToken string) http.HandlerFunc

func RespondWithAccessToken

func RespondWithAccessToken(accessToken string) http.HandlerFunc

func RespondWithContent

func RespondWithContent(status int, contentType, body string) http.HandlerFunc

RespondeWithContent responds with the given status code, content type and body.

func RespondWithCookie

func RespondWithCookie(name, value string) http.HandlerFunc

RespondWithCookie responds to the request adding a cookie with the given name and value.

func RespondWithJSON

func RespondWithJSON(status int, body string) http.HandlerFunc

RespondWithJSON responds with the given status code and JSON body.

func RespondWithJSONTemplate

func RespondWithJSONTemplate(status int, source string, args ...interface{}) http.HandlerFunc

RespondWithJSONTemplate responds with the given status code and with a JSON body that is generated from the given template and arguments. See the EvaluateTemplate function for details on how the template and the arguments are combined.

func RespondWithPatchedJSON

func RespondWithPatchedJSON(status int, body string, patch string) http.HandlerFunc

RespondWithPatchedJSON responds with the given status code and the result of patching the given JSON with the given patch.

func RespondWithTokenError

func RespondWithTokenError(err, description string) http.HandlerFunc

func TextTransport

func TextTransport(code int, body string) http.RoundTripper

TextTransport creates a transport that always returns the given status code and plan text body.

func VerifyCookie

func VerifyCookie(name, value string) http.HandlerFunc

VerifyCookie checks that the request contains a cookie with the given name and value.

func VerifyJQ

func VerifyJQ(filter string, expected interface{}) http.HandlerFunc

VerifyJQ verifies that the result of applying the given `jq` filter to the request body matches the given expected value.

Types

type Database

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

Database is a PostgreSQL database.

func (*Database) Close

func (d *Database) Close()

Close deletes the database.

func (*Database) MakeHandle

func (d *Database) MakeHandle() *sql.DB

MakeHandle creates a new database handle for this database.

type DatabaseServer

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

DatabaseServer knows how to start a PostgreSQL database server inside a container, and how to create databases to be used for tests.

func MakeDatabaseServer

func MakeDatabaseServer() *DatabaseServer

MakeDatabaseServer creates a new database server.

func (*DatabaseServer) Close

func (s *DatabaseServer) Close()

Close stops the database server.

func (*DatabaseServer) MakeDatabase

func (s *DatabaseServer) MakeDatabase() *Database

MakeDatabase creates a new database.

type MetricsServer

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

MetricsServer is an HTTP server configured to return Prometheus metrics. Don't create objects of this type directly, use the MakeMetricsServer function instead.

func NewMetricsServer

func NewMetricsServer() *MetricsServer

NewMetricsServer creates a metrics server.

func (*MetricsServer) Close

func (s *MetricsServer) Close()

Close stops the server and releases the resources it uses.

func (*MetricsServer) Metrics

func (s *MetricsServer) Metrics() []string

Metrics returns an array of strings containing the metrics available in this server. Each item in this array is a line in the Prometheus exposition format. This is intended to be used together with the MatchLine matcher.

func (*MetricsServer) Registry

func (s *MetricsServer) Registry() prometheus.Registerer

Registry returns the registry that should be used to register metrics for this server.

type TransportFunc

type TransportFunc func(*http.Request) (*http.Response, error)

TransportFunc is a function that implements the http.RoundTripper interface. This is intended to siplify writing tests that require custom round trippers because it doesn't require a new type. For example, to create a round tripper that always returns an error:

transport := testing.TransportFunc(func (*http.Request) (*http.Response, error) {
	return nil, errors.New("my error")
})

func (TransportFunc) RoundTrip

func (f TransportFunc) RoundTrip(request *http.Request) (response *http.Response, err error)

RoundTrip is the implementation of the http.RoundTripper interface.

Jump to

Keyboard shortcuts

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