feat: test file for response and error module
This commit is contained in:
118
pkg/model/error.go
Normal file
118
pkg/model/error.go
Normal 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
144
pkg/model/error_test.go
Normal 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
119
pkg/model/model.go
Normal 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"`
|
||||
}
|
||||
Reference in New Issue
Block a user