feat: test file for response and error module

This commit is contained in:
mayur.popli
2025-03-25 02:29:33 +05:30
parent eec1125fee
commit 5a3501b99d
5 changed files with 557 additions and 367 deletions

118
pkg/model/error.go Normal file
View File

@@ -0,0 +1,118 @@
package model
import (
"fmt"
"net/http"
"strings"
)
// Error represents an error response.
type Error struct {
Code string `json:"code"`
Paths string `json:"paths,omitempty"`
Message string `json:"message"`
}
// Error implements the error interface for the Error struct.
func (e *Error) Error() string {
return fmt.Sprintf("Error: Code=%s, Path=%s, Message=%s", e.Code, e.Paths, e.Message)
}
// SchemaValidationErr represents a collection of schema validation failures.
type SchemaValidationErr struct {
Errors []Error
}
// Error implements the error interface for SchemaValidationErr.
func (e *SchemaValidationErr) Error() string {
var errorMessages []string
for _, err := range e.Errors {
errorMessages = append(errorMessages, fmt.Sprintf("%s: %s", err.Paths, err.Message))
}
return strings.Join(errorMessages, "; ")
}
func (e *SchemaValidationErr) BecknError() *Error {
if len(e.Errors) == 0 {
return &Error{
Code: http.StatusText(http.StatusBadRequest),
Message: "Schema validation error.",
}
}
// Collect all error paths and messages
var paths []string
var messages []string
for _, err := range e.Errors {
if err.Paths != "" {
paths = append(paths, err.Paths)
}
messages = append(messages, err.Message)
}
return &Error{
Code: http.StatusText(http.StatusBadRequest),
Paths: strings.Join(paths, ";"),
Message: strings.Join(messages, "; "),
}
}
// SignalidationErr represents a collection of schema validation failures.
type SignValidationErr struct {
error
}
func NewSignValidationErrf(format string, a ...any) *SignValidationErr {
return &SignValidationErr{fmt.Errorf(format, a...)}
}
func NewSignValidationErr(e error) *SignValidationErr {
return &SignValidationErr{e}
}
func (e *SignValidationErr) BecknError() *Error {
return &Error{
Code: http.StatusText(http.StatusUnauthorized),
Message: "Signature Validation Error: " + e.Error(),
}
}
// SignalidationErr represents a collection of schema validation failures.
type BadReqErr struct {
error
}
func NewBadReqErr(err error) *BadReqErr {
return &BadReqErr{err}
}
func NewBadReqErrf(format string, a ...any) *BadReqErr {
return &BadReqErr{fmt.Errorf(format, a...)}
}
func (e *BadReqErr) BecknError() *Error {
return &Error{
Code: http.StatusText(http.StatusBadRequest),
Message: "BAD Request: " + e.Error(),
}
}
// SignalidationErr represents a collection of schema validation failures.
type NotFoundErr struct {
error
}
func NewNotFoundErr(err error) *NotFoundErr {
return &NotFoundErr{err}
}
func NewNotFoundErrf(format string, a ...any) *NotFoundErr {
return &NotFoundErr{fmt.Errorf(format, a...)}
}
func (e *NotFoundErr) BecknError() *Error {
return &Error{
Code: http.StatusText(http.StatusNotFound),
Message: "Endpoint not found: " + e.Error(),
}
}

144
pkg/model/error_test.go Normal file
View File

@@ -0,0 +1,144 @@
package model
import (
"errors"
"net/http"
"testing"
)
func TestError_Error(t *testing.T) {
err := &Error{
Code: "400",
Paths: "/path/to/field",
Message: "Invalid value",
}
expected := "Error: Code=400, Path=/path/to/field, Message=Invalid value"
if err.Error() != expected {
t.Errorf("Expected %s, got %s", expected, err.Error())
}
}
func TestSchemaValidationErr_Error(t *testing.T) {
errs := SchemaValidationErr{
Errors: []Error{
{Paths: "/field1", Message: "Field is required"},
{Paths: "/field2", Message: "Invalid format"},
},
}
expected := "/field1: Field is required; /field2: Invalid format"
if errs.Error() != expected {
t.Errorf("Expected %s, got %s", expected, errs.Error())
}
}
func TestSchemaValidationErr_BecknError(t *testing.T) {
errs := SchemaValidationErr{
Errors: []Error{
{Paths: "/field1", Message: "Field is required"},
{Paths: "/field2", Message: "Invalid format"},
},
}
result := errs.BecknError()
if result.Code != http.StatusText(http.StatusBadRequest) {
t.Errorf("Expected %s, got %s", http.StatusText(http.StatusBadRequest), result.Code)
}
expectedPaths := "/field1;/field2"
expectedMessage := "Field is required; Invalid format"
if result.Paths != expectedPaths {
t.Errorf("Expected paths %s, got %s", expectedPaths, result.Paths)
}
if result.Message != expectedMessage {
t.Errorf("Expected message %s, got %s", expectedMessage, result.Message)
}
}
func TestNewSignValidationErrf(t *testing.T) {
err := NewSignValidationErrf("signature %s", "invalid")
expected := "signature invalid"
if err.Error() != expected {
t.Errorf("Expected %s, got %s", expected, err.Error())
}
}
func TestNewSignValidationErr(t *testing.T) {
baseErr := errors.New("invalid signature")
err := NewSignValidationErr(baseErr)
if err.Error() != "invalid signature" {
t.Errorf("Expected %s, got %s", "invalid signature", err.Error())
}
}
func TestSignValidationErr_BecknError(t *testing.T) {
err := NewSignValidationErr(errors.New("invalid signature"))
result := err.BecknError()
expected := "Signature Validation Error: invalid signature"
if result.Code != http.StatusText(http.StatusUnauthorized) {
t.Errorf("Expected %s, got %s", http.StatusText(http.StatusUnauthorized), result.Code)
}
if result.Message != expected {
t.Errorf("Expected %s, got %s", expected, result.Message)
}
}
func TestNewBadReqErr(t *testing.T) {
baseErr := errors.New("bad request error")
err := NewBadReqErr(baseErr)
if err.Error() != "bad request error" {
t.Errorf("Expected %s, got %s", "bad request error", err.Error())
}
}
func TestNewBadReqErrf(t *testing.T) {
err := NewBadReqErrf("missing %s", "field")
expected := "missing field"
if err.Error() != expected {
t.Errorf("Expected %s, got %s", expected, err.Error())
}
}
func TestBadReqErr_BecknError(t *testing.T) {
err := NewBadReqErr(errors.New("invalid payload"))
result := err.BecknError()
expected := "BAD Request: invalid payload"
if result.Code != http.StatusText(http.StatusBadRequest) {
t.Errorf("Expected %s, got %s", http.StatusText(http.StatusBadRequest), result.Code)
}
if result.Message != expected {
t.Errorf("Expected %s, got %s", expected, result.Message)
}
}
func TestNewNotFoundErr(t *testing.T) {
baseErr := errors.New("resource not found")
err := NewNotFoundErr(baseErr)
if err.Error() != "resource not found" {
t.Errorf("Expected %s, got %s", "resource not found", err.Error())
}
}
func TestNewNotFoundErrf(t *testing.T) {
err := NewNotFoundErrf("route %s not found", "/api/data")
expected := "route /api/data not found"
if err.Error() != expected {
t.Errorf("Expected %s, got %s", expected, err.Error())
}
}
func TestNotFoundErr_BecknError(t *testing.T) {
err := NewNotFoundErr(errors.New("endpoint not available"))
result := err.BecknError()
expected := "Endpoint not found: endpoint not available"
if result.Code != http.StatusText(http.StatusNotFound) {
t.Errorf("Expected %s, got %s", http.StatusText(http.StatusNotFound), result.Code)
}
if result.Message != expected {
t.Errorf("Expected %s, got %s", expected, result.Message)
}
}

119
pkg/model/model.go Normal file
View File

@@ -0,0 +1,119 @@
package model
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
)
// Subscriber represents a unique operational configuration of a trusted platform on a network.
type Subscriber struct {
SubscriberID string `json:"subscriber_id"`
URL string `json:"url" format:"uri"`
Type string `json:"type" enum:"BAP,BPP,BG"`
Domain string `json:"domain"`
}
// SubscriptionDetails represents subscription details of a Network Participant.
type Subscription struct {
Subscriber `json:",inline"`
KeyID string `json:"key_id" format:"uuid"`
SigningPublicKey string `json:"signing_public_key"`
EncrPublicKey string `json:"encr_public_key"`
ValidFrom time.Time `json:"valid_from" format:"date-time"`
ValidUntil time.Time `json:"valid_until" format:"date-time"`
Status string `json:"status" enum:"INITIATED,UNDER_SUBSCRIPTION,SUBSCRIBED,EXPIRED,UNSUBSCRIBED,INVALID_SSL"`
Created time.Time `json:"created" format:"date-time"`
Updated time.Time `json:"updated" format:"date-time"`
Nonce string
}
const (
AuthHeaderSubscriber string = "Authorization"
AuthHeaderGateway string = "X-Gateway-Authorization"
UnaAuthorizedHeaderSubscriber string = "WWW-Authenticate"
UnaAuthorizedHeaderGateway string = "Proxy-Authenticate"
)
type contextKey string
// Correctly define MsgIDKey with a variable, not a const
var MsgIDKey = contextKey("message_id")
type Role string
const (
RoleBAP Role = "bap"
RoleBPP Role = "bpp"
RoleGateway Role = "gateway"
RoleRegistery Role = "registery"
)
// validRoles ensures only allowed values are accepted
var validRoles = map[Role]bool{
RoleBAP: true,
RoleBPP: true,
RoleGateway: true,
RoleRegistery: true,
}
// Custom YAML unmarshalling to validate Role names
func (r *Role) UnmarshalYAML(unmarshal func(interface{}) error) error {
var roleName string
if err := unmarshal(&roleName); err != nil {
return err
}
role := Role(roleName)
if !validRoles[role] {
return fmt.Errorf("invalid Role: %s", roleName)
}
*r = role
return nil
}
type Route struct {
Type string
URL *url.URL
Publisher string
}
type StepContext struct {
context.Context
Request *http.Request
Body []byte
Route *Route
SubID string
Role Role
RespHeader http.Header
}
func (ctx *StepContext) WithContext(newCtx context.Context) {
ctx.Context = newCtx // Update the existing context, keeping all other fields unchanged.
}
// Status represents the status of an acknowledgment.
type Status string
const (
StatusACK Status = "ACK"
StatusNACK Status = "NACK"
)
// Ack represents an acknowledgment response.
type Ack struct {
Status Status `json:"status"` // ACK or NACK
}
// Message represents the message object in the response.
type Message struct {
Ack Ack `json:"ack"`
Error *Error `json:"error,omitempty"`
}
// Response represents the main response structure.
type Response struct {
Message Message `json:"message"`
}

View File

@@ -3,7 +3,11 @@ package response
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/http"
"github.com/beckn/beckn-onix/pkg/model"
) )
type ErrorType string type ErrorType string
@@ -13,131 +17,87 @@ const (
InvalidRequestErrorType ErrorType = "INVALID_REQUEST" InvalidRequestErrorType ErrorType = "INVALID_REQUEST"
) )
type BecknRequest struct { // type BecknRequest struct {
Context map[string]interface{} `json:"context,omitempty"` // Context map[string]interface{} `json:"context,omitempty"`
} // }
type Error struct { func SendAck(w http.ResponseWriter) {
Code string `json:"code,omitempty"` // Create the response object
Message string `json:"message,omitempty"` resp := &model.Response{
Paths string `json:"paths,omitempty"` Message: model.Message{
} Ack: model.Ack{
Status: model.StatusACK,
type Message struct {
Ack struct {
Status string `json:"status,omitempty"`
} `json:"ack,omitempty"`
Error *Error `json:"error,omitempty"`
}
type BecknResponse struct {
Context map[string]interface{} `json:"context,omitempty"`
Message Message `json:"message,omitempty"`
}
type ClientFailureBecknResponse struct {
Context map[string]interface{} `json:"context,omitempty"`
Error *Error `json:"error,omitempty"`
}
var errorMap = map[ErrorType]Error{
SchemaValidationErrorType: {
Code: "400",
Message: "Schema validation failed",
},
InvalidRequestErrorType: {
Code: "401",
Message: "Invalid request format",
},
}
var DefaultError = Error{
Code: "500",
Message: "Internal server error",
}
func Nack(ctx context.Context, tp ErrorType, paths string, body []byte) ([]byte, error) {
var req BecknRequest
if err := json.Unmarshal(body, &req); err != nil {
return nil, fmt.Errorf("failed to parse request: %w", err)
}
errorObj, ok := errorMap[tp]
if paths != "" {
errorObj.Paths = paths
}
var response BecknResponse
if !ok {
response = BecknResponse{
Context: req.Context,
Message: Message{
Ack: struct {
Status string `json:"status,omitempty"`
}{
Status: "NACK",
},
Error: &DefaultError,
},
}
} else {
response = BecknResponse{
Context: req.Context,
Message: Message{
Ack: struct {
Status string `json:"status,omitempty"`
}{
Status: "NACK",
},
Error: &errorObj,
},
}
}
return json.Marshal(response)
}
func Ack(ctx context.Context, body []byte) ([]byte, error) {
var req BecknRequest
if err := json.Unmarshal(body, &req); err != nil {
return nil, fmt.Errorf("failed to parse request: %w", err)
}
response := BecknResponse{
Context: req.Context,
Message: Message{
Ack: struct {
Status string `json:"status,omitempty"`
}{
Status: "ACK",
}, },
}, },
} }
return json.Marshal(response) // Marshal to JSON
} data, err := json.Marshal(resp)
if err != nil {
func HandleClientFailure(ctx context.Context, tp ErrorType, body []byte) ([]byte, error) { http.Error(w, "failed to marshal response", http.StatusInternalServerError)
var req BecknRequest return
if err := json.Unmarshal(body, &req); err != nil {
return nil, fmt.Errorf("failed to parse request: %w", err)
} }
errorObj, ok := errorMap[tp] // Set headers and write response
var response ClientFailureBecknResponse w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
if !ok { // nack sends a negative acknowledgment (NACK) response with an error message.
response = ClientFailureBecknResponse{ func nack(w http.ResponseWriter, err *model.Error, status int) {
Context: req.Context, // Create the NACK response object
Error: &DefaultError, resp := &model.Response{
} Message: model.Message{
} else { Ack: model.Ack{
response = ClientFailureBecknResponse{ Status: model.StatusNACK,
Context: req.Context, },
Error: &errorObj, Error: err,
} },
} }
return json.Marshal(response) // 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)
}
func internalServerError(ctx context.Context) *model.Error {
return &model.Error{
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
var badReqErr *model.BadReqErr
var notFoundErr *model.NotFoundErr
switch {
case errors.As(err, &schemaErr):
nack(w, schemaErr.BecknError(), http.StatusBadRequest)
return
case errors.As(err, &signErr):
nack(w, signErr.BecknError(), http.StatusUnauthorized)
return
case errors.As(err, &badReqErr):
nack(w, badReqErr.BecknError(), http.StatusBadRequest)
return
case errors.As(err, &notFoundErr):
nack(w, notFoundErr.BecknError(), http.StatusNotFound)
return
default:
nack(w, internalServerError(ctx), http.StatusInternalServerError)
return
}
} }

View File

@@ -1,303 +1,152 @@
package response package response
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"reflect" "errors"
"net/http"
"net/http/httptest"
"testing" "testing"
"github.com/beckn/beckn-onix/pkg/model"
) )
func TestSendAck(t *testing.T) {
http.NewRequest("GET", "/", nil)
rr := httptest.NewRecorder()
SendAck(rr)
if rr.Code != http.StatusOK {
t.Errorf("expected status code %d, got %d", http.StatusOK, rr.Code)
}
expected := `{"message":{"ack":{"status":"ACK"}}}`
if rr.Body.String() != expected {
t.Errorf("expected body %s, got %s", expected, rr.Body.String())
}
}
func TestNack(t *testing.T) { func TestNack(t *testing.T) {
ctx := context.Background()
tests := []struct { tests := []struct {
name string name string
errorType ErrorType err *model.Error
requestBody string status int
wantStatus string expected string
wantErrCode string
wantErrMsg string
wantErr bool
path string
}{ }{
{ {
name: "Schema validation error", name: "Schema Validation Error",
errorType: SchemaValidationErrorType, err: &model.Error{
requestBody: `{"context": {"domain": "test-domain", "location": "test-location"}}`, Code: "BAD_REQUEST",
wantStatus: "NACK", Paths: "/test/path",
wantErrCode: "400", Message: "Invalid schema",
wantErrMsg: "Schema validation failed", },
wantErr: false, status: http.StatusBadRequest,
path: "test", expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"BAD_REQUEST","paths":"/test/path","message":"Invalid schema"}}}`,
}, },
{ {
name: "Invalid request error", name: "Internal Server Error",
errorType: InvalidRequestErrorType, err: &model.Error{
requestBody: `{"context": {"domain": "test-domain"}}`, Code: "INTERNAL_SERVER_ERROR",
wantStatus: "NACK", Message: "Something went wrong",
wantErrCode: "401", },
wantErrMsg: "Invalid request format", status: http.StatusInternalServerError,
wantErr: false, expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"INTERNAL_SERVER_ERROR","message":"Something went wrong"}}}`,
path: "test",
},
{
name: "Unknown error type",
errorType: "UNKNOWN_ERROR",
requestBody: `{"context": {"domain": "test-domain"}}`,
wantStatus: "NACK",
wantErrCode: "500",
wantErrMsg: "Internal server error",
wantErr: false,
path: "test",
},
{
name: "Empty request body",
errorType: SchemaValidationErrorType,
requestBody: `{}`,
wantStatus: "NACK",
wantErrCode: "400",
wantErrMsg: "Schema validation failed",
wantErr: false,
path: "test",
},
{
name: "Invalid JSON",
errorType: SchemaValidationErrorType,
requestBody: `{invalid json}`,
wantErr: true,
path: "test",
},
{
name: "Complex nested context",
errorType: SchemaValidationErrorType,
requestBody: `{"context": {"domain": "test-domain", "nested": {"key1": "value1", "key2": 123}}}`,
wantStatus: "NACK",
wantErrCode: "400",
wantErrMsg: "Schema validation failed",
wantErr: false,
path: "test",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
resp, err := Nack(ctx, tt.errorType, tt.path, []byte(tt.requestBody)) http.NewRequest("POST", "/", nil)
rr := httptest.NewRecorder()
if (err != nil) != tt.wantErr { nack(rr, tt.err, tt.status)
t.Errorf("Nack() error = %v, wantErr %v", err, tt.wantErr)
return if rr.Code != tt.status {
t.Errorf("expected status code %d, got %d", tt.status, rr.Code)
} }
if tt.wantErr && err != nil { body := rr.Body.String()
return if body != tt.expected {
} t.Errorf("expected body %s, got %s", tt.expected, body)
var becknResp BecknResponse
if err := json.Unmarshal(resp, &becknResp); err != nil {
t.Errorf("Failed to unmarshal response: %v", err)
return
}
if becknResp.Message.Ack.Status != tt.wantStatus {
t.Errorf("Nack() status = %v, want %v", becknResp.Message.Ack.Status, tt.wantStatus)
}
if becknResp.Message.Error.Code != tt.wantErrCode {
t.Errorf("Nack() error code = %v, want %v", becknResp.Message.Error.Code, tt.wantErrCode)
}
if becknResp.Message.Error.Message != tt.wantErrMsg {
t.Errorf("Nack() error message = %v, want %v", becknResp.Message.Error.Message, tt.wantErrMsg)
}
var origReq BecknRequest
if err := json.Unmarshal([]byte(tt.requestBody), &origReq); err == nil {
if !compareContexts(becknResp.Context, origReq.Context) {
t.Errorf("Nack() context not preserved, got = %v, want %v", becknResp.Context, origReq.Context)
}
} }
}) })
} }
} }
func TestAck(t *testing.T) { func TestSendNack(t *testing.T) {
ctx := context.Background() ctx := context.WithValue(context.Background(), model.MsgIDKey, "123456")
tests := []struct { tests := []struct {
name string name string
requestBody string err error
wantStatus string expected string
wantErr bool status int
}{ }{
{ {
name: "Valid request", name: "SchemaValidationErr",
requestBody: `{"context": {"domain": "test-domain", "location": "test-location"}}`, err: &model.SchemaValidationErr{
wantStatus: "ACK", Errors: []model.Error{
wantErr: false, {Paths: "/path1", Message: "Error 1"},
{Paths: "/path2", Message: "Error 2"},
},
},
status: http.StatusBadRequest,
expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Bad Request","paths":"/path1;/path2","message":"Error 1; Error 2"}}}`,
}, },
{ {
name: "Empty context", name: "SignValidationErr",
requestBody: `{"context": {}}`, err: model.NewSignValidationErr(errors.New("signature invalid")),
wantStatus: "ACK", status: http.StatusUnauthorized,
wantErr: false, expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Unauthorized","message":"Signature Validation Error: signature invalid"}}}`,
}, },
{ {
name: "Invalid JSON", name: "BadReqErr",
requestBody: `{invalid json}`, err: model.NewBadReqErr(errors.New("bad request error")),
wantErr: true, status: http.StatusBadRequest,
expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Bad Request","message":"BAD Request: bad request error"}}}`,
}, },
{ {
name: "Complex nested context", name: "NotFoundErr",
requestBody: `{"context": {"domain": "test-domain", "nested": {"key1": "value1", "key2": 123, "array": [1,2,3]}}}`, err: model.NewNotFoundErr(errors.New("endpoint not found")),
wantStatus: "ACK", status: http.StatusNotFound,
wantErr: false, expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Not Found","message":"Endpoint not found: endpoint not found"}}}`,
},
{
name: "InternalServerError",
err: errors.New("unexpected error"),
status: http.StatusInternalServerError,
expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Internal Server Error","message":"Internal server error, MessageID: 123456"}}}`,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
resp, err := Ack(ctx, []byte(tt.requestBody)) http.NewRequest("POST", "/", nil)
rr := httptest.NewRecorder()
if (err != nil) != tt.wantErr { SendNack(ctx, rr, tt.err)
t.Errorf("Ack() error = %v, wantErr %v", err, tt.wantErr)
return if rr.Code != tt.status {
t.Errorf("expected status code %d, got %d", tt.status, rr.Code)
} }
if tt.wantErr && err != nil { var actual map[string]interface{}
return json.Unmarshal(rr.Body.Bytes(), &actual)
}
var becknResp BecknResponse var expected map[string]interface{}
if err := json.Unmarshal(resp, &becknResp); err != nil { json.Unmarshal([]byte(tt.expected), &expected)
t.Errorf("Failed to unmarshal response: %v", err)
return
}
if becknResp.Message.Ack.Status != tt.wantStatus { if !compareJSON(expected, actual) {
t.Errorf("Ack() status = %v, want %v", becknResp.Message.Ack.Status, tt.wantStatus) t.Errorf("expected body %s, got %s", tt.expected, rr.Body.String())
}
if becknResp.Message.Error != nil {
t.Errorf("Ack() should not have error, got %v", becknResp.Message.Error)
}
var origReq BecknRequest
if err := json.Unmarshal([]byte(tt.requestBody), &origReq); err == nil {
if !compareContexts(becknResp.Context, origReq.Context) {
t.Errorf("Ack() context not preserved, got = %v, want %v", becknResp.Context, origReq.Context)
}
} }
}) })
} }
} }
func TestHandleClientFailure(t *testing.T) { func compareJSON(expected, actual map[string]interface{}) bool {
ctx := context.Background() expectedBytes, _ := json.Marshal(expected)
actualBytes, _ := json.Marshal(actual)
tests := []struct { return bytes.Equal(expectedBytes, actualBytes)
name string
errorType ErrorType
requestBody string
wantErrCode string
wantErrMsg string
wantErr bool
}{
{
name: "Schema validation error",
errorType: SchemaValidationErrorType,
requestBody: `{"context": {"domain": "test-domain", "location": "test-location"}}`,
wantErrCode: "400",
wantErrMsg: "Schema validation failed",
wantErr: false,
},
{
name: "Invalid request error",
errorType: InvalidRequestErrorType,
requestBody: `{"context": {"domain": "test-domain"}}`,
wantErrCode: "401",
wantErrMsg: "Invalid request format",
wantErr: false,
},
{
name: "Unknown error type",
errorType: "UNKNOWN_ERROR",
requestBody: `{"context": {"domain": "test-domain"}}`,
wantErrCode: "500",
wantErrMsg: "Internal server error",
wantErr: false,
},
{
name: "Invalid JSON",
errorType: SchemaValidationErrorType,
requestBody: `{invalid json}`,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := HandleClientFailure(ctx, tt.errorType, []byte(tt.requestBody))
if (err != nil) != tt.wantErr {
t.Errorf("HandleClientFailure() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && err != nil {
return
}
var failureResp ClientFailureBecknResponse
if err := json.Unmarshal(resp, &failureResp); err != nil {
t.Errorf("Failed to unmarshal response: %v", err)
return
}
if failureResp.Error.Code != tt.wantErrCode {
t.Errorf("HandleClientFailure() error code = %v, want %v", failureResp.Error.Code, tt.wantErrCode)
}
if failureResp.Error.Message != tt.wantErrMsg {
t.Errorf("HandleClientFailure() error message = %v, want %v", failureResp.Error.Message, tt.wantErrMsg)
}
var origReq BecknRequest
if err := json.Unmarshal([]byte(tt.requestBody), &origReq); err == nil {
if !compareContexts(failureResp.Context, origReq.Context) {
t.Errorf("HandleClientFailure() context not preserved, got = %v, want %v", failureResp.Context, origReq.Context)
}
}
})
}
}
func TestErrorMap(t *testing.T) {
expectedTypes := []ErrorType{
SchemaValidationErrorType,
InvalidRequestErrorType,
}
for _, tp := range expectedTypes {
if _, exists := errorMap[tp]; !exists {
t.Errorf("ErrorType %v not found in errorMap", tp)
}
}
if DefaultError.Code != "500" || DefaultError.Message != "Internal server error" {
t.Errorf("DefaultError not set correctly, got code=%v, message=%v", DefaultError.Code, DefaultError.Message)
}
}
func compareContexts(c1, c2 map[string]interface{}) bool {
if c1 == nil && c2 == nil {
return true
}
if c1 == nil && len(c2) == 0 || c2 == nil && len(c1) == 0 {
return true
}
return reflect.DeepEqual(c1, c2)
} }