Documentation ¶
Overview ¶
Package ut provides a convenient way to write unit test for the business logic.
Index ¶
- func CreateUtRequestContext(method, url string, body *Body, headers ...Header) *app.RequestContext
- type Body
- type Header
- type ResponseRecorder
- func (rw *ResponseRecorder) Flush()
- func (rw *ResponseRecorder) Header() *protocol.ResponseHeader
- func (rw *ResponseRecorder) Result() *protocol.Response
- func (rw *ResponseRecorder) Write(buf []byte) (int, error)
- func (rw *ResponseRecorder) WriteHeader(code int)
- func (rw *ResponseRecorder) WriteString(str string) (int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateUtRequestContext ¶ added in v0.6.2
func CreateUtRequestContext(method, url string, body *Body, headers ...Header) *app.RequestContext
CreateUtRequestContext returns an app.RequestContext for testing purposes
Types ¶
type ResponseRecorder ¶
type ResponseRecorder struct { // Code is the HTTP response code set by WriteHeader. // // Note that if a Handler never calls WriteHeader or Write, // this might end up being 0, rather than the implicit // http.StatusOK. To get the implicit value, use the Result // method. Code int // Body is the buffer to which the Handler's Write calls are sent. // If nil, the Writes are silently discarded. Body *bytes.Buffer // Flushed is whether the Handler called Flush. Flushed bool // contains filtered or unexported fields }
ResponseRecorder records handler's response for later test
func NewRecorder ¶
func NewRecorder() *ResponseRecorder
NewRecorder returns an initialized ResponseRecorder.
func PerformRequest ¶
func PerformRequest(engine *route.Engine, method, url string, body *Body, headers ...Header) *ResponseRecorder
PerformRequest send a constructed request to given engine without network transporting
Url can be a standard relative URI or a simple absolute path ¶
If engine.streamRequestBody is true, it sets body as bodyStream if not, it sets body as bodyBytes
ResponseRecorder returned are flushed, which means its StatusCode is always set (default 200)
See ./request_test.go for more examples
func (*ResponseRecorder) Flush ¶
func (rw *ResponseRecorder) Flush()
Flush implements http.Flusher. To test whether Flush was called, see rw.Flushed.
func (*ResponseRecorder) Header ¶
func (rw *ResponseRecorder) Header() *protocol.ResponseHeader
Header returns the response headers to mutate within a handler. To test the headers that were written after a handler completes, use the Result method and see the returned Response value's Header.
func (*ResponseRecorder) Result ¶
func (rw *ResponseRecorder) Result() *protocol.Response
Result returns the response generated by the handler.
The returned Response will have at least its StatusCode, Header, Body, and optionally Trailer populated. More fields may be populated in the future, so callers should not DeepEqual the result in tests.
The Response.Header is a snapshot of the headers at the time of the first write call, or at the time of this call, if the handler never did a write.
The Response.Body is guaranteed to be non-nil and Body.Read call is guaranteed to not return any error other than io.EOF.
Result must only be called after the handler has finished running.
func (*ResponseRecorder) Write ¶
func (rw *ResponseRecorder) Write(buf []byte) (int, error)
Write implements io.Writer. The data in buf is written to rw.Body, if not nil.
func (*ResponseRecorder) WriteHeader ¶
func (rw *ResponseRecorder) WriteHeader(code int)
WriteHeader sends an HTTP response header with the provided status code.
func (*ResponseRecorder) WriteString ¶
func (rw *ResponseRecorder) WriteString(str string) (int, error)
WriteString implements io.StringWriter. The data in str is written to rw.Body, if not nil.