Documentation ¶
Index ¶
- Constants
- func CurrentContextPath(ctx context.Context) string
- func CurrentPort(ctx context.Context) int
- func GinContextRecorder(gc *gin.Context) *httptest.ResponseRecorder
- func NewGinContext(ctx context.Context, method, path string, body io.Reader, ...) *gin.Context
- func NewGinContextWithRequest(req *http.Request) *gin.Context
- func NewRequest(ctx context.Context, method, target string, body io.Reader, ...) (req *http.Request)
- func WithMockedServer(opts ...TestServerOptions) test.Options
- func WithRealServer(opts ...TestServerOptions) test.Options
- func WithUtilities(opts ...TestServerOptions) test.Options
- type ExecResult
- type RequestOptions
- type TestServerConfig
- type TestServerOptions
Constants ¶
const (
DefaultContextPath = "/test"
)
Variables ¶
This section is empty.
Functions ¶
func CurrentContextPath ¶
CurrentContextPath utility func that extract current server context-path from testing context Return DefaultContextPath if not found
func CurrentPort ¶
CurrentPort utility func that extract current server port from testing context Return -1 if not found
func GinContextRecorder ¶
func GinContextRecorder(gc *gin.Context) *httptest.ResponseRecorder
func NewGinContext ¶
func NewRequest ¶
func NewRequest(ctx context.Context, method, target string, body io.Reader, opts ...RequestOptions) (req *http.Request)
NewRequest create a new *http.Request based on current execution mode. WithRealServer mode:
- Created request have Host, Port and ContextPath set to current TestServer.
- If the given target is relative path, "http" is used. and "context path" is prepended to the given path.
- If the given target is absolute URL, its Host, Port are overridden, and path is kept unchanged
WithMockedServer mode:
- the returned request is created by `httptest.NewRequest` and cannot be used by http.DefaultClient.Do()
- If the given target is relative path, "http" is used. and "context path" is prepended to the given path.
- If the given target is absolute URL, host, port and path are kept unchanged
This function panic if given target is not valid absolute/relative URL or test server is not enabled
func WithMockedServer ¶
func WithMockedServer(opts ...TestServerOptions) test.Options
WithMockedServer initialize web package without starting an actual web server. NewRequest(), Exec() and MustExec() can be used to create/send request and verifying result without creating an actual http connection. By default, the server doesn't allow CORS and have no security configured. When using this mode, *web.Registrar became available to inject Note: In this mode, httptest package is used internally and http.Handler (*web.Engine in our case) is invoked directly
func WithRealServer ¶
func WithRealServer(opts ...TestServerOptions) test.Options
WithRealServer start a real web server at random port with context-path as DefaultContextPath. NewRequest(), Exec() and MustExec() can be used to create/send request and verifying result By default, the server doesn't allow CORS and have no security configured. Actual server port can be retrieved via CurrentPort() When using this mode, *web.Registrar became available to inject
func WithUtilities ¶
func WithUtilities(opts ...TestServerOptions) test.Options
WithUtilities DOES NOT initialize web package, it only provide properties and setup utilities (e.g. MustExec) Important: this mode is mostly for go-lanai internal tests. DO NOT use it in microservices
NewRequest(), Exec() and MustExec() can be used to create/send request and verifying result without creating an actual http connection. Note: In this mode, httptest package is used internally and http.Handler (*web.Engine in our case) is invoked directly
Types ¶
type ExecResult ¶
type ExecResult struct { Response *http.Response ResponseRecorder *httptest.ResponseRecorder }
func Exec ¶
func Exec(ctx context.Context, req *http.Request, opts ...RequestOptions) (ExecResult, error)
Exec execute given request depending on test server mode (real vs mocked) returned ExecResult is guaranteed to have non-nil ExecResult.Response if there is no error. ExecResult.ResponseRecorder is non-nil if test server mode is WithMockedServer() this func might return error if test server mode is WithRealServer() Note: don't forget to close the response's body when done with it
func MustExec ¶
func MustExec(ctx context.Context, req *http.Request, opts ...RequestOptions) ExecResult
MustExec is same as Exec, but panic instead of returning error Note: don't forget to close the response's body when done with it
type RequestOptions ¶
func ContentType ¶
func ContentType(v string) RequestOptions
ContentType returns a RequestOptions that set content type in header
func Cookies ¶
func Cookies(resp *http.Response) RequestOptions
Cookies returns a RequestOptions that carry on cookies from given response
func Headers ¶
func Headers(kvs ...string) RequestOptions
Headers returns a RequestOptions that set additional headers
func Queries ¶
func Queries(kvs ...string) RequestOptions
Queries returns a RequestOptions that set additional queries
type TestServerConfig ¶
type TestServerConfig struct { Port int ContextPath string LogLevel log.LoggingLevel RequestOptions []RequestOptions }
type TestServerOptions ¶
type TestServerOptions func(conf *TestServerConfig)
func AddDefaultRequestOptions ¶
func AddDefaultRequestOptions(opts ...RequestOptions) TestServerOptions
AddDefaultRequestOptions returns a TestServerOptions that add default RequestOptions on every request created via NewRequest.
func UseContextPath ¶
func UseContextPath(contextPath string) TestServerOptions
UseContextPath returns a TestServerOptions that overwrite the context-path of the test server
func UseLogLevel ¶
func UseLogLevel(lvl log.LoggingLevel) TestServerOptions
UseLogLevel returns a TestServerOptions that overwrite the default log level of the test server
func UsePort ¶
func UsePort(port int) TestServerOptions
UsePort returns a TestServerOptions that use given port. Note: using fixed port might cause issues when run in CI/CD