Documentation ¶
Index ¶
- Constants
- Variables
- func BaseURL() string
- func ClearShutdownHooks()
- func ClearStartupHooks()
- func DisableMaintenance()
- func EnableMaintenance()
- func ErrorStatusHandler(response *Response, _ *Request)
- func IsMaintenanceEnabled() bool
- func IsReady() bool
- func PanicStatusHandler(response *Response, _ *Request)
- func ProxyBaseURL() string
- func RegisterShutdownHook(hook func())
- func RegisterStartupHook(hook func())
- func RunTest(t *testing.T, suite ITestSuite) bool
- func Start(routeRegistrer func(*Router)) error
- func Stop()
- func ValidationStatusHandler(response *Response, _ *Request)
- type Error
- type Handler
- type ITestSuite
- type Middleware
- type NativeMiddlewareFunc
- type PreWriter
- type Request
- func (r *Request) BasicAuth() (username, password string, ok bool)
- func (r *Request) BearerToken() (string, bool)
- func (r *Request) Bool(field string) bool
- func (r *Request) CORSOptions() *cors.Options
- func (r *Request) ContentLength() int64
- func (r *Request) Cookies() []*http.Cookie
- func (r *Request) Date(field string) time.Time
- func (r *Request) File(field string) []fsutil.File
- func (r *Request) Has(field string) bool
- func (r *Request) Header() http.Header
- func (r *Request) IP(field string) net.IP
- func (r *Request) Integer(field string) int
- func (r *Request) Method() string
- func (r *Request) Numeric(field string) float64
- func (r *Request) Object(field string) map[string]interface{}
- func (r *Request) Protocol() string
- func (r *Request) Referrer() string
- func (r *Request) RemoteAddress() string
- func (r *Request) Request() *http.Request
- func (r *Request) Route() *Route
- func (r *Request) String(field string) string
- func (r *Request) Timezone(field string) *time.Location
- func (r *Request) ToStruct(dst interface{}) error
- func (r *Request) URI() *url.URL
- func (r *Request) URL(field string) *url.URL
- func (r *Request) UUID(field string) uuid.UUID
- func (r *Request) UserAgent() string
- type Response
- func (r *Response) Cookie(cookie *http.Cookie)
- func (r *Response) Download(file string, fileName string) error
- func (r *Response) Error(err interface{}) error
- func (r *Response) File(file string) error
- func (r *Response) GetError() interface{}
- func (r *Response) GetStacktrace() string
- func (r *Response) GetStatus() int
- func (r *Response) HandleDatabaseError(db *gorm.DB) bool
- func (r *Response) Header() http.Header
- func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (r *Response) Hijacked() bool
- func (r *Response) IsEmpty() bool
- func (r *Response) IsHeaderWritten() bool
- func (r *Response) JSON(responseCode int, data interface{}) error
- func (r *Response) PreWrite(b []byte)
- func (r *Response) Redirect(url string)
- func (r *Response) Render(responseCode int, templatePath string, data interface{}) error
- func (r *Response) RenderHTML(responseCode int, templatePath string, data interface{}) error
- func (r *Response) SetWriter(writer io.Writer)
- func (r *Response) Status(status int)
- func (r *Response) String(responseCode int, message string) error
- func (r *Response) TemporaryRedirect(url string)
- func (r *Response) Write(data []byte) (int, error)
- func (r *Response) WriteHeader(status int)
- func (r *Response) Writer() io.Writer
- type Route
- func (r *Route) BuildURI(parameters ...string) string
- func (r *Route) BuildURL(parameters ...string) string
- func (r *Route) GetFullURI() string
- func (r *Route) GetFullURIAndParameters() (string, []string)
- func (r *Route) GetHandler() Handler
- func (r *Route) GetMethods() []string
- func (r *Route) GetName() string
- func (p *Route) GetParameters() []string
- func (r *Route) GetURI() string
- func (r *Route) GetValidationRules() *validation.Rules
- func (r *Route) Middleware(middleware ...Middleware) *Route
- func (r *Route) Name(name string) *Route
- func (r *Route) Validate(validationRules validation.Ruler) *Route
- type Router
- func (r *Router) CORS(options *cors.Options)
- func (r *Router) ClearRegexCache()
- func (r *Router) Delete(uri string, handler Handler) *Route
- func (r *Router) Get(uri string, handler Handler) *Route
- func (p *Router) GetParameters() []string
- func (r *Router) GetRoute(name string) *Route
- func (r *Router) GetRoutes() []*Route
- func (r *Router) GetSubrouters() []*Router
- func (r *Router) GlobalMiddleware(middleware ...Middleware)
- func (r *Router) Group() *Router
- func (r *Router) Middleware(middleware ...Middleware)
- func (r *Router) Options(uri string, handler Handler) *Route
- func (r *Router) Patch(uri string, handler Handler) *Route
- func (r *Router) Post(uri string, handler Handler) *Route
- func (r *Router) Put(uri string, handler Handler) *Route
- func (r *Router) Route(methods string, uri string, handler Handler) *Route
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) Static(uri string, directory string, download bool, middleware ...Middleware)
- func (r *Router) StatusHandler(handler Handler, status int, additionalStatuses ...int)
- func (r *Router) Subrouter(prefix string) *Router
- type TestSuite
- func (s *TestSuite) ClearDatabase()
- func (s *TestSuite) ClearDatabaseTables()
- func (s *TestSuite) CreateTestFiles(paths ...string) []fsutil.File
- func (s *TestSuite) CreateTestRequest(rawRequest *http.Request) *Request
- func (s *TestSuite) CreateTestResponse(recorder http.ResponseWriter) *Response
- func (s *TestSuite) CreateTestResponseWithRequest(recorder http.ResponseWriter, rawRequest *http.Request) *Response
- func (s *TestSuite) Delete(route string, headers map[string]string, body io.Reader) (*http.Response, error)
- func (s *TestSuite) Get(route string, headers map[string]string) (*http.Response, error)
- func (s *TestSuite) GetBody(response *http.Response) []byte
- func (s *TestSuite) GetJSONBody(response *http.Response, data interface{}) error
- func (s *TestSuite) Middleware(middleware Middleware, request *Request, procedure Handler) *http.Response
- func (s *TestSuite) Patch(route string, headers map[string]string, body io.Reader) (*http.Response, error)
- func (s *TestSuite) Post(route string, headers map[string]string, body io.Reader) (*http.Response, error)
- func (s *TestSuite) Put(route string, headers map[string]string, body io.Reader) (*http.Response, error)
- func (s *TestSuite) Request(method, route string, headers map[string]string, body io.Reader) (*http.Response, error)
- func (s *TestSuite) RunServer(routeRegistrer func(*Router), procedure func())
- func (s *TestSuite) SetTimeout(timeout time.Duration)
- func (s *TestSuite) Timeout() time.Duration
- func (s *TestSuite) WriteField(writer *multipart.Writer, fieldName, value string)
- func (s *TestSuite) WriteFile(writer *multipart.Writer, path, fieldName, fileName string)
Constants ¶
const ( // ExitInvalidConfig the exit code returned when the config // validation doesn't pass. ExitInvalidConfig = 3 // ExitNetworkError the exit code returned when an error // occurs when opening the network listener ExitNetworkError = 4 // ExitHTTPError the exit code returned when an error // occurs in the HTTP server (port already in use for example) ExitHTTPError = 5 )
Variables ¶
var ( // Logger the logger for default output // Writes to stdout by default. Logger = log.New(os.Stdout, "", log.LstdFlags) // AccessLogger the logger for access. This logger // is used by the logging middleware. // Writes to stdout by default. AccessLogger = log.New(os.Stdout, "", 0) // ErrLogger the logger in which errors and stacktraces are written. // Writes to stderr by default. ErrLogger = log.New(os.Stderr, "", log.LstdFlags) )
var ( // ErrNotHijackable returned by response.Hijack() if the underlying // http.ResponseWriter doesn't implement http.Hijacker. This can // happen with HTTP/2 connections. ErrNotHijackable = errors.New("Underlying http.ResponseWriter doesn't implement http.Hijacker") )
Functions ¶
func DisableMaintenance ¶
func DisableMaintenance()
DisableMaintenance replace the main server handler with the original router.
func EnableMaintenance ¶
func EnableMaintenance()
EnableMaintenance replace the main server handler with the "Service Unavailable" handler.
func ErrorStatusHandler ¶
ErrorStatusHandler a generic status handler for non-success codes. Writes the corresponding status message to the response.
func IsMaintenanceEnabled ¶
func IsMaintenanceEnabled() bool
IsMaintenanceEnabled return true if the server is currently in maintenance mode.
func IsReady ¶
func IsReady() bool
IsReady returns true if the server has finished initializing and is ready to serve incoming requests.
func PanicStatusHandler ¶
PanicStatusHandler for the HTTP 500 error. If debugging is enabled, writes the error details to the response and print stacktrace in the console. If debugging is not enabled, writes `{"error": "Internal Server Error"}` to the response.
func ProxyBaseURL ¶
func ProxyBaseURL() string
ProxyBaseURL returns the base URL of your application based on the "server.proxy" configuration. This is useful when you want to generate an URL when your application is served behind a reverse proxy. If "server.proxy.host" configuration is not set, returns the same value as "BaseURL()".
func RegisterShutdownHook ¶
func RegisterShutdownHook(hook func())
RegisterShutdownHook to execute some code after the server stopped. Shutdown hooks are executed before goyave.Start() returns.
func RegisterStartupHook ¶
func RegisterStartupHook(hook func())
RegisterStartupHook to execute some code once the server is ready and running.
func RunTest ¶
func RunTest(t *testing.T, suite ITestSuite) bool
RunTest run a test suite with prior initialization of a test environment. The GOYAVE_ENV environment variable is automatically set to "test" and restored to its original value at the end of the test run. All tests are run using your project's root as working directory. This directory is determined by the presence of a "go.mod" file.
func Start ¶
Start starts the web server. The routeRegistrer parameter is a function aimed at registering all your routes and middleware.
import ( "goyave.dev/goyave/v4" "github.com/username/projectname/route" ) func main() { if err := goyave.Start(route.Register); err != nil { os.Exit(err.(*goyave.Error).ExitCode) } }
Errors returned can be safely type-asserted to "*goyave.Error". Panics if the server is already running.
func Stop ¶
func Stop()
Stop gracefully shuts down the server without interrupting any active connections.
Make sure the program doesn't exit and waits instead for Stop to return.
Stop does not attempt to close nor wait for hijacked connections such as WebSockets. The caller of Stop should separately notify such long-lived connections of shutdown and wait for them to close, if desired.
func ValidationStatusHandler ¶
ValidationStatusHandler for HTTP 400 and HTTP 422 errors. Writes the validation errors to the response.
Types ¶
type Error ¶
Error wrapper for errors directely related to the server itself. Contains an exit code and the original error.
type Handler ¶
Handler is a controller or middleware function
func NativeHandler ¶
NativeHandler is an adapter function for "http.Handler". With this adapter, you can plug non-Goyave handlers to your application.
Just remember that the body contains the raw data, which haven't been validated nor converted. This means that native handlers are not guaranteed to work and cannot modify the request data. Request properties, such as headers, can still be modified. Prefer implementing a Goyave handler.
This feature is a compatibility layer with the rest of the Golang web ecosystem. Prefer using Goyave handlers if possible.
type ITestSuite ¶
type ITestSuite interface { RunServer(func(*Router), func()) Timeout() time.Duration SetTimeout(time.Duration) Middleware(Middleware, *Request, Handler) *http.Response Get(string, map[string]string) (*http.Response, error) Post(string, map[string]string, io.Reader) (*http.Response, error) Put(string, map[string]string, io.Reader) (*http.Response, error) Patch(string, map[string]string, io.Reader) (*http.Response, error) Delete(string, map[string]string, io.Reader) (*http.Response, error) Request(string, string, map[string]string, io.Reader) (*http.Response, error) T() *testing.T SetT(*testing.T) SetS(suite suite.TestingSuite) GetBody(*http.Response) []byte GetJSONBody(*http.Response, interface{}) error CreateTestFiles(paths ...string) []fsutil.File WriteFile(*multipart.Writer, string, string, string) WriteField(*multipart.Writer, string, string) CreateTestRequest(*http.Request) *Request CreateTestResponse(http.ResponseWriter) *Response // contains filtered or unexported methods }
ITestSuite is an extension of testify's Suite for Goyave-specific testing.
type Middleware ¶
Middleware function generating middleware handler function.
Request data is available to middleware, but bear in mind that it had not been validated yet. That means that you can modify or filter data. (Trim strings for example)
func NativeMiddleware ¶
func NativeMiddleware(middleware NativeMiddlewareFunc) Middleware
NativeMiddleware is an adapter function for standard library middleware.
Native middleware work like native handlers. See "NativeHandler" for more details.
type NativeMiddlewareFunc ¶
NativeMiddlewareFunc is a function which receives an http.Handler and returns another http.Handler.
type PreWriter ¶
type PreWriter interface {
PreWrite(b []byte)
}
PreWriter is a writter that needs to alter the response headers or status before they are written. If implemented, PreWrite will be called right before the Write operation.
type Request ¶
type Request struct { Rules *validation.Rules Params map[string]string Data map[string]interface{} Extra map[string]interface{} User interface{} Lang string // contains filtered or unexported fields }
Request struct represents an http request. Contains the validated body in the Data attribute if the route was defined with a request generator function
func (*Request) BasicAuth ¶
BasicAuth returns the username and password provided in the request's Authorization header, if the request uses HTTP Basic Authentication.
func (*Request) BearerToken ¶
BearerToken extract the auth token from the "Authorization" header. Only takes tokens of type "Bearer". Returns empty string if no token found or the header is invalid.
func (*Request) Bool ¶
Bool get a bool field from the request data. Panics if the field is not a bool.
func (*Request) CORSOptions ¶
CORSOptions returns the CORS options applied to this request, or nil. The returned object is a copy of the options applied to the router. Therefore, altering the returned object will not alter the router's options.
func (*Request) ContentLength ¶
ContentLength records the length of the associated content. The value -1 indicates that the length is unknown.
func (*Request) Date ¶
Date get a date field from the request data. Panics if the field is not a date.
func (*Request) File ¶
File get a file field from the request data. Panics if the field is not numeric.
func (*Request) Header ¶
Header contains the request header fields either received by the server or to be sent by the client. Header names are case-insensitive.
If the raw request has the following header lines,
Host: example.com accept-encoding: gzip, deflate Accept-Language: en-us fOO: Bar foo: two
then the header map will look like this:
Header = map[string][]string{ "Accept-Encoding": {"gzip, deflate"}, "Accept-Language": {"en-us"}, "Foo": {"Bar", "two"}, }
func (*Request) Integer ¶
Integer get an integer field from the request data. Panics if the field is not an integer.
func (*Request) Numeric ¶
Numeric get a numeric field from the request data. Panics if the field is not numeric.
func (*Request) Object ¶
Object get an object field from the request data. Panics if the field is not an object.
func (*Request) RemoteAddress ¶
RemoteAddress allows to record the network address that sent the request, usually for logging.
func (*Request) Request ¶
Request return the raw http request. Prefer using the "goyave.Request" accessors.
func (*Request) String ¶
String get a string field from the request data. Panics if the field is not a string.
func (*Request) Timezone ¶
Timezone get a timezone field from the request data. Panics if the field is not a timezone.
func (*Request) ToStruct ¶
ToStruct map the request data to a struct.
type UserInsertRequest struct { Username string Email string } //... userInsertRequest := UserInsertRequest{} if err := request.ToStruct(&userInsertRequest); err != nil { panic(err) }
func (*Request) URI ¶
URI specifies the URI being requested. Use this if you absolutely need the raw query params, url, etc. Otherwise use the provided methods and fields of the "goyave.Request".
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response represents a controller response.
func (*Response) Cookie ¶
Cookie add a Set-Cookie header to the response. The provided cookie must have a valid Name. Invalid cookies may be silently dropped.
func (*Response) Download ¶
Download write a file as an attachment element. Automatically detects the file MIME type and sets the "Content-Type" header accordingly. If the file doesn't exist, respond with status 404 Not Found. The given path can be relative or absolute.
The "fileName" parameter defines the name the client will see. In other words, it sets the header "Content-Disposition" to "attachment; filename="${fileName}""
If you want the file to be sent as an inline element ("Content-Disposition: inline"), use the "File" function instead.
func (*Response) Error ¶
Error print the error in the console and return it with an error code 500 (or previously defined status code using `response.Status()`). If debugging is enabled in the config, the error is also written in the response and the stacktrace is printed in the console. If debugging is not enabled, only the status code is set, which means you can still write to the response, or use your error status handler.
func (*Response) File ¶
File write a file as an inline element. Automatically detects the file MIME type and sets the "Content-Type" header accordingly. If the file doesn't exist, respond with status 404 Not Found. The given path can be relative or absolute.
If you want the file to be sent as a download ("Content-Disposition: attachment"), use the "Download" function instead.
func (*Response) GetError ¶
func (r *Response) GetError() interface{}
GetError return the value which caused a panic in the request's handling, or nil.
func (*Response) GetStacktrace ¶
GetStacktrace return the stacktrace of when the error occurred, or an empty string. The stacktrace is captured by the recovery middleware.
func (*Response) GetStatus ¶
GetStatus return the response code for this request or 0 if not yet set.
func (*Response) HandleDatabaseError ¶
HandleDatabaseError takes a database query result and checks if any error has occurred.
Automatically writes HTTP status code 404 Not Found if the error is a "Not found" error. Calls "Response.Error()" if there is another type of error.
Returns true if there is no error.
func (*Response) Hijack ¶
Hijack implements the Hijacker.Hijack method. For more details, check http.Hijacker.
Returns ErrNotHijackable if the underlying http.ResponseWriter doesn't implement http.Hijacker. This can happen with HTTP/2 connections.
Middleware executed after controller handlers, as well as status handlers, keep working as usual after a connection has been hijacked. Callers should properly set the response status to ensure middleware and status handler execute correctly. Usually, callers of the Hijack method set the HTTP status to http.StatusSwitchingProtocols. If no status is set, the regular behavior will be kept and `204 No Content` will be set as the response status.
func (*Response) Hijacked ¶
Hijacked returns true if the underlying connection has been successfully hijacked via the Hijack method.
func (*Response) IsEmpty ¶
IsEmpty return true if nothing has been written to the response body yet.
func (*Response) IsHeaderWritten ¶
IsHeaderWritten return true if the response header has been written. Once the response header is written, you cannot change the response status and headers anymore.
func (*Response) JSON ¶
JSON write json data as a response. Also sets the "Content-Type" header automatically.
func (*Response) PreWrite ¶
PreWrite writes the response header after calling PreWrite on the child writer if it implements PreWriter.
func (*Response) Render ¶
Render a text template with the given data. The template path is relative to the "resources/template" directory.
func (*Response) RenderHTML ¶
RenderHTML an HTML template with the given data. The template path is relative to the "resources/template" directory.
func (*Response) SetWriter ¶
SetWriter set the writer used to write the response. This can be used to chain writers, for example to enable gzip compression, or for logging.
The original http.ResponseWriter is always kept.
func (*Response) Status ¶
Status set the response status code. Calling this method a second time will have no effect.
func (*Response) TemporaryRedirect ¶
TemporaryRedirect send a temporary redirect response
func (*Response) WriteHeader ¶
WriteHeader sends an HTTP response header with the provided status code. Prefer using "Status()" method instead. Calling this method a second time will have no effect.
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
Route stores information for matching and serving.
func (*Route) BuildURI ¶
BuildURI build a full URI pointing to this route. The returned string doesn't include the protocol and domain. (e.g. "/user/login") Panics if the amount of parameters doesn't match the amount of actual parameters for this route.
func (*Route) BuildURL ¶
BuildURL build a full URL pointing to this route. Panics if the amount of parameters doesn't match the amount of actual parameters for this route.
func (*Route) GetFullURI ¶
GetFullURI get the full URI of this route.
Note that this URI may contain route parameters in their définition format. Use the request's URI if you want to see the URI as it was requested by the client.
func (*Route) GetFullURIAndParameters ¶
GetFullURIAndParameters get the full uri and parameters for this route and all its parent routers.
func (*Route) GetHandler ¶
GetHandler returns the Handler associated with this route.
func (*Route) GetMethods ¶
GetMethods returns the methods the route matches against.
func (*Route) GetParameters ¶
func (p *Route) GetParameters() []string
GetParameters returns the URI parameters' names (in order of appearance).
func (*Route) GetURI ¶
GetURI get the URI of this route. The returned URI is relative to the parent router of this route, it is NOT the full path to this route.
Note that this URI may contain route parameters in their définition format. Use the request's URI if you want to see the URI as it was requested by the client.
func (*Route) GetValidationRules ¶
func (r *Route) GetValidationRules() *validation.Rules
GetValidationRules returns the validation rules associated with this route.
func (*Route) Middleware ¶
func (r *Route) Middleware(middleware ...Middleware) *Route
Middleware register middleware for this route only.
Returns itself.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router registers routes to be matched and executes a handler.
func NewRouter ¶
func NewRouter() *Router
NewRouter create a new root-level Router that is pre-configured with core middleware (recovery, parse and language), as well as status handlers for all standard HTTP status codes.
You don't need to manually build your router using this function if you are using `goyave.Start()`. This method can however be useful for external tooling that build routers without starting the HTTP server. Don't forget to call router.ClearRegexCache() when you are done registering routes.
func (*Router) CORS ¶
CORS set the CORS options for this route group. If the options are not nil, the CORS middleware is automatically added.
func (*Router) ClearRegexCache ¶
func (r *Router) ClearRegexCache()
ClearRegexCache set internal router's regex cache used for route parameters optimisation to nil so it can be garbage collected. You don't need to call this function if you are using `goyave.Start()`. However, this method SHOULD be called by external tooling that build routers without starting the HTTP server when they are done registering routes and subrouters.
func (*Router) GetParameters ¶
func (p *Router) GetParameters() []string
GetParameters returns the URI parameters' names (in order of appearance).
func (*Router) GetSubrouters ¶
GetSubrouters returns the list of subrouters belonging to this router.
func (*Router) GlobalMiddleware ¶
func (r *Router) GlobalMiddleware(middleware ...Middleware)
GlobalMiddleware apply one or more global middleware. Global middleware are executed for every request, including when the request doesn't match any route or if it results in "Method Not Allowed". These middleware are global to the main Router: they will also be executed for subrouters. Global Middleware are always executed first. Use global middleware for logging and rate limiting for example.
func (*Router) Middleware ¶
func (r *Router) Middleware(middleware ...Middleware)
Middleware apply one or more middleware to the route group.
func (*Router) Route ¶
Route register a new route.
Multiple methods can be passed using a pipe-separated string.
"PUT|PATCH"
The validation rules set is optional. If you don't want your route to be validated, pass "nil".
If the route matches the "GET" method, the "HEAD" method is automatically added to the matcher if it's missing.
If the router has CORS options set, the "OPTIONS" method is automatically added to the matcher if it's missing, so it allows preflight requests.
Returns the generated route.
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP dispatches the handler registered in the matched route.
func (*Router) Static ¶
func (r *Router) Static(uri string, directory string, download bool, middleware ...Middleware)
Static serve a directory and its subdirectories of static resources. Set the "download" parameter to true if you want the files to be sent as an attachment instead of an inline element.
If no file is given in the url, or if the given file is a directory, the handler will send the "index.html" file if it exists.
func (*Router) StatusHandler ¶
StatusHandler set a handler for responses with an empty body. The handler will be automatically executed if the request's life-cycle reaches its end and nothing has been written in the response body.
Multiple status codes can be given. The handler will be executed if one of them matches.
This method can be used to define custom error handlers for example.
Status handlers are inherited as a copy in sub-routers. Modifying a child's status handler will not modify its parent's.
Codes in the 400 and 500 ranges have a default status handler.
type TestSuite ¶
TestSuite is an extension of testify's Suite for Goyave-specific testing.
func (*TestSuite) ClearDatabase ¶
func (s *TestSuite) ClearDatabase()
ClearDatabase delete all records in all tables. This function only clears the tables of registered models, ignoring models implementing `database.IView`.
func (*TestSuite) ClearDatabaseTables ¶
func (s *TestSuite) ClearDatabaseTables()
ClearDatabaseTables drop all tables. This function only clears the tables of registered models.
func (*TestSuite) CreateTestFiles ¶
CreateTestFiles create a slice of "fsutil.File" from the given paths. Files are passed to a temporary http request and parsed as Multipart form, to reproduce the way files are obtained in real scenarios.
func (*TestSuite) CreateTestRequest ¶
CreateTestRequest create a "goyave.Request" from the given raw request. This function is aimed at making it easier to unit test Requests.
If passed request is "nil", a default GET request to "/" is used.
rawRequest := httptest.NewRequest("GET", "/test-route", nil) rawRequest.Header.Set("Content-Type", "application/json") request := goyave.CreateTestRequest(rawRequest) request.Lang = "en-US" request.Data = map[string]interface{}{"field": "value"}
func (*TestSuite) CreateTestResponse ¶
func (s *TestSuite) CreateTestResponse(recorder http.ResponseWriter) *Response
CreateTestResponse create an empty response with the given response writer. This function is aimed at making it easier to unit test Responses.
writer := httptest.NewRecorder() response := suite.CreateTestResponse(writer) response.Status(http.StatusNoContent) result := writer.Result() fmt.Println(result.StatusCode) // 204
func (*TestSuite) CreateTestResponseWithRequest ¶
func (s *TestSuite) CreateTestResponseWithRequest(recorder http.ResponseWriter, rawRequest *http.Request) *Response
CreateTestResponseWithRequest create an empty response with the given response writer HTTP request. This function is aimed at making it easier to unit test Responses needing the raw request's information, such as redirects.
writer := httptest.NewRecorder() rawRequest := httptest.NewRequest("POST", "/test-route", strings.NewReader("body")) response := suite.CreateTestResponseWithRequest(writer, rawRequest) response.Status(http.StatusNoContent) result := writer.Result() fmt.Println(result.StatusCode) // 204
func (*TestSuite) Delete ¶
func (s *TestSuite) Delete(route string, headers map[string]string, body io.Reader) (*http.Response, error)
Delete execute a DELETE request on the given route. Headers and body are optional.
func (*TestSuite) GetBody ¶
GetBody read the whole body of a response. If read failed, test fails and return empty byte slice.
func (*TestSuite) GetJSONBody ¶
GetJSONBody read the whole body of a response and decode it as JSON. If read or decode failed, test fails.
func (*TestSuite) Middleware ¶
func (s *TestSuite) Middleware(middleware Middleware, request *Request, procedure Handler) *http.Response
Middleware executes the given middleware and returns the HTTP response. Core middleware (recovery, parsing and language) is not executed.
func (*TestSuite) Patch ¶
func (s *TestSuite) Patch(route string, headers map[string]string, body io.Reader) (*http.Response, error)
Patch execute a PATCH request on the given route. Headers and body are optional.
func (*TestSuite) Post ¶
func (s *TestSuite) Post(route string, headers map[string]string, body io.Reader) (*http.Response, error)
Post execute a POST request on the given route. Headers and body are optional.
func (*TestSuite) Put ¶
func (s *TestSuite) Put(route string, headers map[string]string, body io.Reader) (*http.Response, error)
Put execute a PUT request on the given route. Headers and body are optional.
func (*TestSuite) Request ¶
func (s *TestSuite) Request(method, route string, headers map[string]string, body io.Reader) (*http.Response, error)
Request execute a request on the given route. Headers and body are optional.
func (*TestSuite) RunServer ¶
RunServer start the application and run the given functional test procedure.
This function is the equivalent of "goyave.Start()". The test fails if the suite's timeout is exceeded. The server automatically shuts down when the function ends. This function is synchronized, that means that the server is properly stopped when the function returns.
func (*TestSuite) SetTimeout ¶
SetTimeout set the timeout for test failure when using RunServer or requests.
func (*TestSuite) Timeout ¶
Timeout get the timeout for test failure when using RunServer or requests.
func (*TestSuite) WriteField ¶
WriteField create and write a new multipart form field. The test fails if the field couldn't be written.