testutil

package
v5.0.0-rc9 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateTestFiles

func CreateTestFiles(fs fs.FS, paths ...string) ([]fsutil.File, error)

CreateTestFiles create a slice of "fsutil.File" from the given FS. To reproduce the way the files are obtained in real scenarios, files are first encoded in a multipart form, then decoded with a multipart form reader.

Paths are relative to the caller, not relative to the project's root directory.

func FindRootDirectory

func FindRootDirectory() string

FindRootDirectory find relative path to the project's root directory based on the existence of a `go.mod` file. The returned path is relative to the working directory and contains a trailing slash. Returns an empty string if not found.

func NewTestRequest

func NewTestRequest(method, uri string, body io.Reader) *goyave.Request

NewTestRequest create a new `goyave.Request` with an underlying HTTP request created usin the `httptest` package.

func NewTestResponse

func NewTestResponse(request *goyave.Request) (*goyave.Response, *httptest.ResponseRecorder)

NewTestResponse create a new `goyave.Response` with an underlying HTTP response recorder created using the `httptest` package. This function uses a temporary `goyave.Server` with all defaults values loaded so all functions of `*goyave.Response` can be used safely.

func ReadJSONBody

func ReadJSONBody[T any](body io.Reader) (T, error)

ReadJSONBody decodes the given body reader into a new variable of type `*T`.

func ToJSON

func ToJSON(data any) *bytes.Reader

ToJSON marshals the given data and creates a bytes reader from the result. Panics on error.

func WriteMultipartFile

func WriteMultipartFile(writer *multipart.Writer, filesystem fs.FS, path, fieldName, fileName string) (err error)

WriteMultipartFile reads a file from the given FS and writes it to the given multipart writer.

Types

type LogWriter

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

LogWriter implementation of `io.Writer` redirecting the logs to `testing.T.Log()`

func (LogWriter) Write

func (w LogWriter) Write(b []byte) (int, error)

type TestServer

type TestServer struct {
	*goyave.Server
}

TestServer extension of `goyave.Server` providing useful functions for testing.

func NewTestServer

func NewTestServer(t *testing.T, configFileName string) *TestServer

NewTestServer creates a new server using the given config file. The config path is relative to the project's directory. If not nil, the given `routeRegistrer` function is called to register routes without starting the server.

A default logger redirecting the output to `testing.T.Log()` is used.

Automatically closes the DB connection (if there is one) using a test `Cleanup` function.

func NewTestServerWithOptions

func NewTestServerWithOptions(t *testing.T, opts goyave.Options) *TestServer

NewTestServerWithOptions creates a new server using the given options. If not nil, the given `routeRegistrer` function is called to register routes without starting the server.

By default, if no `Logger` is given in the options, a default logger redirecting the output to `testing.T.Log()` is used.

Automatically closes the DB connection (if there is one) using a test `Cleanup` function.

func (*TestServer) CloseDB

func (s *TestServer) CloseDB()

CloseDB close the server DB if one is open. It is a good practice to always call this in a test `Cleanup` function when using a database.

func (*TestServer) NewTestRequest

func (s *TestServer) NewTestRequest(method, uri string, body io.Reader) *goyave.Request

NewTestRequest create a new `goyave.Request` with an underlying HTTP request created usin the `httptest` package. This function sets the request language using the default language of the server.

func (*TestServer) NewTestResponse

func (s *TestServer) NewTestResponse(request *goyave.Request) (*goyave.Response, *httptest.ResponseRecorder)

NewTestResponse create a new `goyave.Response` with an underlying HTTP response recorder created using the `httptest` package.

func (*TestServer) TestMiddleware

func (s *TestServer) TestMiddleware(middleware goyave.Middleware, request *goyave.Request, procedure goyave.Handler) *http.Response

TestMiddleware executes with the given request and returns the response. The `procedure` parameter is the `next` handler passed to the middleware and can be used to make assertions. Keep in mind that this procedure won't be executed if your middleware is blocking.

The request will go through the entire lifecycle like a regular request.

func (*TestServer) TestRequest

func (s *TestServer) TestRequest(request *http.Request) *http.Response

TestRequest execute a request by calling the root Router's `ServeHTTP()` implementation.

Jump to

Keyboard shortcuts

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