Documentation ¶
Index ¶
- func CreateTestFiles(fs fs.FS, paths ...string) ([]fsutil.File, error)
- func FindRootDirectory() string
- func NewTestRequest(method, uri string, body io.Reader) *goyave.Request
- func NewTestResponse(request *goyave.Request) (*goyave.Response, *httptest.ResponseRecorder)
- func ReadJSONBody[T any](body io.Reader) (T, error)
- func ToJSON(data any) *bytes.Reader
- func WriteMultipartFile(writer *multipart.Writer, filesystem fs.FS, path, fieldName, fileName string) (err error)
- type TestServer
- func (s *TestServer) CloseDB()
- func (s *TestServer) NewTestRequest(method, uri string, body io.Reader) *goyave.Request
- func (s *TestServer) NewTestResponse(request *goyave.Request) (*goyave.Response, *httptest.ResponseRecorder)
- func (s *TestServer) TestMiddleware(middleware goyave.Middleware, request *goyave.Request, ...) *http.Response
- func (s *TestServer) TestRequest(request *http.Request) *http.Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateTestFiles ¶
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 Returns an empty string if not found.
func NewTestRequest ¶
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 ¶
ReadJSONBody decodes the given body reader into a new variable of type `*T`.
Types ¶
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, routeRegistrer func(*goyave.Server, *goyave.Router)) *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.
Automatically closes the DB connection (if there is one) using a test `Cleanup` function.
func NewTestServerWithOptions ¶
func NewTestServerWithOptions(t *testing.T, opts goyave.Options, routeRegistrer func(*goyave.Server, *goyave.Router)) *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.
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.