From 3573b46fd9b9b9a0047e4ce17ad0e7100a6ad106 Mon Sep 17 00:00:00 2001 From: DeveloperDurp Date: Sat, 28 Sep 2024 08:27:53 -0500 Subject: [PATCH] formating --- handlers/handlers.go | 20 ++++++++++++---- handlers/handlers_test.go | 50 ++++++++++++++++++++++++++++++++------- middleware/logging.go | 8 ++++++- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index aaf8ac6..1bed926 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -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. -func NewFailureResponse(message string, status int, description []string) StandardError { +// NewFailureResponse returns a new instance of StandardError with the given +// message, status code and description. +func NewFailureResponse( + message string, + status int, + description []string, +) StandardError { return StandardError{ Message: message, 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 { return &StandardMessage{ 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 { return &StandardMessage{ Message: BasicMessage{ @@ -85,7 +92,10 @@ func setHeader(w *http.ResponseWriter, statusCode int) { (*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 { return func(w http.ResponseWriter, r *http.Request) { diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index a56b3c4..bc636a3 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -27,7 +27,10 @@ func TestSendResponseStandardMessage(t *testing.T) { // Check that the content type header is set to "application/json" contentType := w.Header().Get("Content-Type") 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 @@ -59,7 +62,11 @@ func TestSendResponseStandardError(t *testing.T) { // Check that the content type header is set to "application/json" contentType := w.Header().Get("Content-Type") 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 @@ -70,7 +77,11 @@ func TestSendResponseStandardError(t *testing.T) { } 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) { @@ -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) { message := "An error has occured" status := http.StatusInternalServerError @@ -86,13 +98,25 @@ func TestNewFailureResponse(t *testing.T) { resp := NewFailureResponse(message, status, description) 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 { - 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) { - 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) 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) { - 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, + ) } } diff --git a/middleware/logging.go b/middleware/logging.go index b303b98..53710dd 100644 --- a/middleware/logging.go +++ b/middleware/logging.go @@ -27,6 +27,12 @@ func Logging(next http.Handler) http.Handler { } 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), + ) }) }