2024-09-02 13:38:46 -05:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestSendResponseStandardMessage(t *testing.T) {
|
|
|
|
|
message := &BasicMessage{
|
|
|
|
|
Message: "Hello World!",
|
|
|
|
|
}
|
|
|
|
|
resp := &StandardMessage{
|
|
|
|
|
Status: http.StatusAccepted,
|
|
|
|
|
Message: message,
|
|
|
|
|
}
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
resp.SendReponse(w)
|
|
|
|
|
|
|
|
|
|
// Check the status code is set correctly
|
|
|
|
|
if w.Code != 202 {
|
|
|
|
|
t.Errorf("Expected status code to be 202, but got %d", w.Code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that the content type header is set to "application/json"
|
|
|
|
|
contentType := w.Header().Get("Content-Type")
|
|
|
|
|
if contentType != "application/json" {
|
2024-09-28 08:27:53 -05:00
|
|
|
t.Errorf(
|
|
|
|
|
"Expected content type to be 'application/json', but got %s",
|
|
|
|
|
contentType,
|
|
|
|
|
)
|
2024-09-02 13:38:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that the message is written to the response body correctly
|
|
|
|
|
response := &BasicMessage{}
|
|
|
|
|
err := json.NewDecoder(w.Body).Decode(response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if !reflect.DeepEqual(message, response) {
|
|
|
|
|
t.Errorf("Expected body to be %s but got %s", message, response)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSendResponseStandardError(t *testing.T) {
|
|
|
|
|
resp := &StandardError{
|
|
|
|
|
Status: http.StatusInternalServerError,
|
|
|
|
|
Message: "An error has occured",
|
|
|
|
|
Description: []string{"An Error"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
resp.SendReponse(w)
|
|
|
|
|
|
|
|
|
|
// Check the status code is set correctly
|
|
|
|
|
if w.Code != 500 {
|
|
|
|
|
t.Errorf("Expected status code to be 500, but got %d", w.Code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that the content type header is set to "application/json"
|
|
|
|
|
contentType := w.Header().Get("Content-Type")
|
|
|
|
|
if contentType != "application/json" {
|
2024-09-28 08:27:53 -05:00
|
|
|
t.Errorf(
|
|
|
|
|
"Expected content type to be 'application/json',"+
|
|
|
|
|
" but got %s",
|
|
|
|
|
contentType,
|
|
|
|
|
)
|
2024-09-02 13:38:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that the message is written to the response body correctly
|
|
|
|
|
response := &StandardError{}
|
|
|
|
|
err := json.NewDecoder(w.Body).Decode(response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if response.Message != resp.Message {
|
2024-09-28 08:27:53 -05:00
|
|
|
t.Errorf(
|
|
|
|
|
"Expected Message of %s but got %s",
|
|
|
|
|
resp.Message,
|
|
|
|
|
response.Message,
|
|
|
|
|
)
|
2024-09-02 13:38:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(resp, response) {
|
|
|
|
|
t.Errorf("Expected Message of %v but got %v", resp, response)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-28 08:27:53 -05:00
|
|
|
// NewFailureResponse returns a new instance of StandardError with the given
|
|
|
|
|
// message, status code and description.
|
2024-09-02 13:38:46 -05:00
|
|
|
func TestNewFailureResponse(t *testing.T) {
|
|
|
|
|
message := "An error has occured"
|
|
|
|
|
status := http.StatusInternalServerError
|
|
|
|
|
description := []string{"An Error"}
|
|
|
|
|
resp := NewFailureResponse(message, status, description)
|
|
|
|
|
|
|
|
|
|
if resp.Status != status {
|
2024-09-28 08:27:53 -05:00
|
|
|
t.Errorf(
|
|
|
|
|
"Expected Status to be %d but got %d",
|
|
|
|
|
status,
|
|
|
|
|
resp.Status,
|
|
|
|
|
)
|
2024-09-02 13:38:46 -05:00
|
|
|
}
|
|
|
|
|
if resp.Message != message {
|
2024-09-28 08:27:53 -05:00
|
|
|
t.Errorf(
|
|
|
|
|
"Expected Status to be %s but got %s",
|
|
|
|
|
message,
|
|
|
|
|
resp.Message,
|
|
|
|
|
)
|
2024-09-02 13:38:46 -05:00
|
|
|
}
|
|
|
|
|
if !reflect.DeepEqual(description, resp.Description) {
|
2024-09-28 08:27:53 -05:00
|
|
|
t.Errorf(
|
|
|
|
|
"Expected Status to be %v but got %v",
|
|
|
|
|
description,
|
|
|
|
|
resp.Description,
|
|
|
|
|
)
|
2024-09-02 13:38:46 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewMessageResponse(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
message := &BasicMessage{
|
|
|
|
|
Message: "Hello World!",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp := NewMessageResponse(message, http.StatusOK)
|
|
|
|
|
|
|
|
|
|
if resp.Status != http.StatusOK {
|
2024-09-28 08:27:53 -05:00
|
|
|
t.Errorf(
|
|
|
|
|
"Expected Status to be %d but got %d",
|
|
|
|
|
http.StatusOK,
|
|
|
|
|
resp.Status,
|
|
|
|
|
)
|
2024-09-02 13:38:46 -05:00
|
|
|
}
|
|
|
|
|
if !reflect.DeepEqual(message, resp.Message) {
|
2024-09-28 08:27:53 -05:00
|
|
|
t.Errorf(
|
|
|
|
|
"Expected Message to be %s but got %s",
|
|
|
|
|
message,
|
|
|
|
|
resp.Message,
|
|
|
|
|
)
|
2024-09-02 13:38:46 -05:00
|
|
|
}
|
|
|
|
|
}
|