feat: test file for response and error module
This commit is contained in:
@@ -17,12 +17,7 @@ const (
|
||||
InvalidRequestErrorType ErrorType = "INVALID_REQUEST"
|
||||
)
|
||||
|
||||
// type BecknRequest struct {
|
||||
// Context map[string]interface{} `json:"context,omitempty"`
|
||||
// }
|
||||
|
||||
func SendAck(w http.ResponseWriter) {
|
||||
// Create the response object
|
||||
resp := &model.Response{
|
||||
Message: model.Message{
|
||||
Ack: model.Ack{
|
||||
@@ -31,22 +26,22 @@ func SendAck(w http.ResponseWriter) {
|
||||
},
|
||||
}
|
||||
|
||||
// Marshal to JSON
|
||||
data, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to marshal response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Set headers and write response
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(data)
|
||||
_, err = w.Write(data)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to write response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// nack sends a negative acknowledgment (NACK) response with an error message.
|
||||
func nack(w http.ResponseWriter, err *model.Error, status int) {
|
||||
// Create the NACK response object
|
||||
resp := &model.Response{
|
||||
Message: model.Message{
|
||||
Ack: model.Ack{
|
||||
@@ -55,28 +50,27 @@ func nack(w http.ResponseWriter, err *model.Error, status int) {
|
||||
Error: err,
|
||||
},
|
||||
}
|
||||
|
||||
// Marshal the response to JSON
|
||||
data, jsonErr := json.Marshal(resp)
|
||||
if jsonErr != nil {
|
||||
http.Error(w, "failed to marshal response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Set headers and write response
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status) // Assuming NACK means a bad request
|
||||
w.Write(data)
|
||||
w.WriteHeader(status)
|
||||
_, er := w.Write(data)
|
||||
if er != nil {
|
||||
http.Error(w, "failed to write response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func internalServerError(ctx context.Context) *model.Error {
|
||||
return &model.Error{
|
||||
Code: http.StatusText(http.StatusInternalServerError),
|
||||
Code: http.StatusText(http.StatusInternalServerError),
|
||||
Message: fmt.Sprintf("Internal server error, MessageID: %s", ctx.Value(model.MsgIDKey)),
|
||||
}
|
||||
}
|
||||
|
||||
// SendNack sends a negative acknowledgment (NACK) response with an error message.
|
||||
func SendNack(ctx context.Context, w http.ResponseWriter, err error) {
|
||||
var schemaErr *model.SchemaValidationErr
|
||||
var signErr *model.SignValidationErr
|
||||
|
||||
@@ -13,7 +13,10 @@ import (
|
||||
)
|
||||
|
||||
func TestSendAck(t *testing.T) {
|
||||
http.NewRequest("GET", "/", nil)
|
||||
_, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err) // For tests
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
SendAck(rr)
|
||||
@@ -58,7 +61,10 @@ func TestNack(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
http.NewRequest("POST", "/", nil)
|
||||
_, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err) // For tests
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
nack(rr, tt.err, tt.status)
|
||||
@@ -123,7 +129,10 @@ func TestSendNack(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
http.NewRequest("POST", "/", nil)
|
||||
_, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err) // For tests
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
SendNack(ctx, rr, tt.err)
|
||||
@@ -133,10 +142,16 @@ func TestSendNack(t *testing.T) {
|
||||
}
|
||||
|
||||
var actual map[string]interface{}
|
||||
json.Unmarshal(rr.Body.Bytes(), &actual)
|
||||
err = json.Unmarshal(rr.Body.Bytes(), &actual)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
var expected map[string]interface{}
|
||||
json.Unmarshal([]byte(tt.expected), &expected)
|
||||
err = json.Unmarshal([]byte(tt.expected), &expected)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal expected response: %v", err)
|
||||
}
|
||||
|
||||
if !compareJSON(expected, actual) {
|
||||
t.Errorf("expected body %s, got %s", tt.expected, rr.Body.String())
|
||||
|
||||
Reference in New Issue
Block a user