mirror of
https://gitlab.durp.info/durfy/modules/durpify.git
synced 2026-05-07 08:00:30 -05:00
formating
This commit is contained in:
parent
b4562a1da4
commit
3573b46fd9
3 changed files with 63 additions and 15 deletions
|
|
@ -52,8 +52,13 @@ func (message *StandardError) SendReponse(w http.ResponseWriter) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFailureResponse returns a new instance of StandardError with the given message, status code and description.
|
// NewFailureResponse returns a new instance of StandardError with the given
|
||||||
func NewFailureResponse(message string, status int, description []string) StandardError {
|
// message, status code and description.
|
||||||
|
func NewFailureResponse(
|
||||||
|
message string,
|
||||||
|
status int,
|
||||||
|
description []string,
|
||||||
|
) StandardError {
|
||||||
return StandardError{
|
return StandardError{
|
||||||
Message: message,
|
Message: message,
|
||||||
Status: status,
|
Status: status,
|
||||||
|
|
@ -61,7 +66,8 @@ func NewFailureResponse(message string, status int, description []string) Standa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMessageResponse returns a new instance of StandardMessage with the given message and status code.
|
// NewMessageResponse returns a new instance of StandardMessage with the given
|
||||||
|
// message and status code.
|
||||||
func NewMessageResponse(message interface{}, status int) *StandardMessage {
|
func NewMessageResponse(message interface{}, status int) *StandardMessage {
|
||||||
return &StandardMessage{
|
return &StandardMessage{
|
||||||
Message: message,
|
Message: message,
|
||||||
|
|
@ -69,7 +75,8 @@ func NewMessageResponse(message interface{}, status int) *StandardMessage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBasicResponse returns a new basic instance of StandardMessage with the message of OK and Status OK.
|
// NewBasicResponse returns a new basic instance of StandardMessage with the
|
||||||
|
// message of OK and Status OK.
|
||||||
func NewBasicResponse() *StandardMessage {
|
func NewBasicResponse() *StandardMessage {
|
||||||
return &StandardMessage{
|
return &StandardMessage{
|
||||||
Message: BasicMessage{
|
Message: BasicMessage{
|
||||||
|
|
@ -85,7 +92,10 @@ func setHeader(w *http.ResponseWriter, statusCode int) {
|
||||||
(*w).Header().Set("Content-Type", "application/json")
|
(*w).Header().Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
type APIFunc func(w http.ResponseWriter, r *http.Request) (*StandardMessage, error)
|
type APIFunc func(
|
||||||
|
w http.ResponseWriter,
|
||||||
|
r *http.Request,
|
||||||
|
) (*StandardMessage, error)
|
||||||
|
|
||||||
func Make(handler APIFunc) http.HandlerFunc {
|
func Make(handler APIFunc) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,10 @@ func TestSendResponseStandardMessage(t *testing.T) {
|
||||||
// Check that the content type header is set to "application/json"
|
// Check that the content type header is set to "application/json"
|
||||||
contentType := w.Header().Get("Content-Type")
|
contentType := w.Header().Get("Content-Type")
|
||||||
if contentType != "application/json" {
|
if contentType != "application/json" {
|
||||||
t.Errorf("Expected content type to be 'application/json', but got %s", contentType)
|
t.Errorf(
|
||||||
|
"Expected content type to be 'application/json', but got %s",
|
||||||
|
contentType,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the message is written to the response body correctly
|
// Check that the message is written to the response body correctly
|
||||||
|
|
@ -59,7 +62,11 @@ func TestSendResponseStandardError(t *testing.T) {
|
||||||
// Check that the content type header is set to "application/json"
|
// Check that the content type header is set to "application/json"
|
||||||
contentType := w.Header().Get("Content-Type")
|
contentType := w.Header().Get("Content-Type")
|
||||||
if contentType != "application/json" {
|
if contentType != "application/json" {
|
||||||
t.Errorf("Expected content type to be 'application/json', but got %s", contentType)
|
t.Errorf(
|
||||||
|
"Expected content type to be 'application/json',"+
|
||||||
|
" but got %s",
|
||||||
|
contentType,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the message is written to the response body correctly
|
// Check that the message is written to the response body correctly
|
||||||
|
|
@ -70,7 +77,11 @@ func TestSendResponseStandardError(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if response.Message != resp.Message {
|
if response.Message != resp.Message {
|
||||||
t.Errorf("Expected Message of %s but got %s", resp.Message, response.Message)
|
t.Errorf(
|
||||||
|
"Expected Message of %s but got %s",
|
||||||
|
resp.Message,
|
||||||
|
response.Message,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(resp, response) {
|
if !reflect.DeepEqual(resp, response) {
|
||||||
|
|
@ -78,7 +89,8 @@ func TestSendResponseStandardError(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFailureResponse returns a new instance of StandardError with the given message, status code and description.
|
// NewFailureResponse returns a new instance of StandardError with the given
|
||||||
|
// message, status code and description.
|
||||||
func TestNewFailureResponse(t *testing.T) {
|
func TestNewFailureResponse(t *testing.T) {
|
||||||
message := "An error has occured"
|
message := "An error has occured"
|
||||||
status := http.StatusInternalServerError
|
status := http.StatusInternalServerError
|
||||||
|
|
@ -86,13 +98,25 @@ func TestNewFailureResponse(t *testing.T) {
|
||||||
resp := NewFailureResponse(message, status, description)
|
resp := NewFailureResponse(message, status, description)
|
||||||
|
|
||||||
if resp.Status != status {
|
if resp.Status != status {
|
||||||
t.Errorf("Expected Status to be %d but got %d", status, resp.Status)
|
t.Errorf(
|
||||||
|
"Expected Status to be %d but got %d",
|
||||||
|
status,
|
||||||
|
resp.Status,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
if resp.Message != message {
|
if resp.Message != message {
|
||||||
t.Errorf("Expected Status to be %s but got %s", message, resp.Message)
|
t.Errorf(
|
||||||
|
"Expected Status to be %s but got %s",
|
||||||
|
message,
|
||||||
|
resp.Message,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(description, resp.Description) {
|
if !reflect.DeepEqual(description, resp.Description) {
|
||||||
t.Errorf("Expected Status to be %v but got %v", description, resp.Description)
|
t.Errorf(
|
||||||
|
"Expected Status to be %v but got %v",
|
||||||
|
description,
|
||||||
|
resp.Description,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -105,9 +129,17 @@ func TestNewMessageResponse(t *testing.T) {
|
||||||
resp := NewMessageResponse(message, http.StatusOK)
|
resp := NewMessageResponse(message, http.StatusOK)
|
||||||
|
|
||||||
if resp.Status != http.StatusOK {
|
if resp.Status != http.StatusOK {
|
||||||
t.Errorf("Expected Status to be %d but got %d", http.StatusOK, resp.Status)
|
t.Errorf(
|
||||||
|
"Expected Status to be %d but got %d",
|
||||||
|
http.StatusOK,
|
||||||
|
resp.Status,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(message, resp.Message) {
|
if !reflect.DeepEqual(message, resp.Message) {
|
||||||
t.Errorf("Expected Message to be %s but got %s", message, resp.Message)
|
t.Errorf(
|
||||||
|
"Expected Message to be %s but got %s",
|
||||||
|
message,
|
||||||
|
resp.Message,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,12 @@ func Logging(next http.Handler) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
next.ServeHTTP(wrapped, r)
|
next.ServeHTTP(wrapped, r)
|
||||||
log.Error("INFO", wrapped.statusCode, r.Method, r.URL.Path, time.Since(start))
|
log.Error(
|
||||||
|
"INFO",
|
||||||
|
wrapped.statusCode,
|
||||||
|
r.Method,
|
||||||
|
r.URL.Path,
|
||||||
|
time.Since(start),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue