Documentation ¶
Overview ¶
Package resp provides convenience methods for handling JSON responses
Index ¶
- func Assert(t *testing.T, resRec *httptest.ResponseRecorder, statusCode int, ...)
- func AssertError(t *testing.T, res *httptest.ResponseRecorder, code, message string)
- func AssertFail(t *testing.T, res *httptest.ResponseRecorder, code, message string)
- func AssertOK(t *testing.T, res *httptest.ResponseRecorder, message string)
- func BadRequest(w http.ResponseWriter, code string)
- func Error(w http.ResponseWriter, err error, code string)
- func ExtractData(t *testing.T, res *httptest.ResponseRecorder, data interface{})
- func Fail(w http.ResponseWriter, status int, data interface{}, message string, ...)
- func InvalidRequest(w http.ResponseWriter)
- func OK(w http.ResponseWriter, data interface{}, message string)
- func Success(w http.ResponseWriter, statusCode int, data interface{}, message string)
- func Unauthorized(w http.ResponseWriter)
- func UnknownResource(w http.ResponseWriter)
- func UnprocessableError(w http.ResponseWriter, message, code string)
- func ValidationError(w http.ResponseWriter, code string)
- func WriteJSON(w http.ResponseWriter, data interface{}, statusCode int)
- type Code
- type Resp
- type Status
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Assert ¶
func Assert(t *testing.T, resRec *httptest.ResponseRecorder, statusCode int, status, message, code string)
Assert makes an assertion about a Response, with optional status, message, and code checks.
func AssertError ¶
func AssertError(t *testing.T, res *httptest.ResponseRecorder, code, message string)
AssertError confirms that the response was an error
func AssertFail ¶
func AssertFail(t *testing.T, res *httptest.ResponseRecorder, code, message string)
AssertFail confirms that the response is a failure
func AssertOK ¶
func AssertOK(t *testing.T, res *httptest.ResponseRecorder, message string)
AssertOK confirms that the response was successful
func Error ¶
func Error(w http.ResponseWriter, err error, code string)
Error responds with 501 and the provided code and error message status code >= 500
func ExtractData ¶
func ExtractData(t *testing.T, res *httptest.ResponseRecorder, data interface{})
ExtractData extracts the `data` payload inside of the JSON response
func Fail ¶
func Fail(w http.ResponseWriter, status int, data interface{}, message string, code string)
Fail responds with the given status code and the provided code and message. Code should be machine readable. Data can optionally be provided status code >= 400 < 500
func InvalidRequest ¶
func InvalidRequest(w http.ResponseWriter)
InvalidRequest is used to notify that the request is invalid and cannot be handled
func OK ¶
func OK(w http.ResponseWriter, data interface{}, message string)
OK is wrapper func for Success with status code 200
func Success ¶
func Success(w http.ResponseWriter, statusCode int, data interface{}, message string)
Success responds with status 200 and the provided data and message status code >= 200 < 400
func Unauthorized ¶
func Unauthorized(w http.ResponseWriter)
Unauthorized is used when the user does not have a valid token
func UnknownResource ¶
func UnknownResource(w http.ResponseWriter)
UnknownResource is used when the URL is valid, but the requested resource could not be found
func UnprocessableError ¶
func UnprocessableError(w http.ResponseWriter, message, code string)
UnprocessableError is used to return error with 422 HTTP status code.
func ValidationError ¶
func ValidationError(w http.ResponseWriter, code string)
ValidationError is used to notify that the request's parameters/body/form-data contained invalid data
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, data interface{}, statusCode int)
WriteJSON writes out the given data to the `http.ResponseWriter`, along with required headers.
Types ¶
type Code ¶
type Code = string
Code type
const ( CodeUnknownError Code = "UNKNOWN_ERROR" CodeInvalidRequest Code = "INVALID_REQUEST" CodeInternalError Code = "INTERNAL_ERROR" CodeValidationError Code = "VALIDATION_ERROR" CodeUnknownResource Code = "UNKNOWN_RESOURCE" CodeAuthRequired Code = "AUTH_REQUIRED" CodeUnknownEndpoint Code = "UNKNOWN_ENDPOINT" )
General code
type Resp ¶
type Resp struct { Status Status `json:"status"` // Response status class Code Code `json:"code,omitempty"` // A machine-readable error code in the case of failure/error responses. Data interface{} `json:"data"` // A data payload provided for success/failure responses. Message string `json:"message"` // A user-friendly message detailing the result of the operation. }
Resp is the standard response format
func Get ¶
Get extracts a `resp.Resp` shaped response payload from a `http.Response`. The data will be available in the `dataPtr`, and the other JSON fields are returned.
func NewFail ¶
NewFail creates a failure JSON response with the given data, code, and optional message.
func NewSuccess ¶
NewSuccess creates a successful JSON response with the given data and optional message.