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