Skip to content

Commit

Permalink
Specific response assertion error messages
Browse files Browse the repository at this point in the history
Signed-off-by: Pieter van der Werk <pieter.vdwerk@gmail.com>
  • Loading branch information
PietervdWerk committed May 14, 2024
1 parent 98997dd commit 66197cd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
16 changes: 8 additions & 8 deletions envoyauth/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (result *EvalResult) IsAllowed() (bool, error) {
}

if allowed, ok = val.(bool); !ok {
return false, fmt.Errorf("type assertion error")
return false, fmt.Errorf("type assertion error, expected allowed to be of type 'boolean' but got '%T'", val)
}

return allowed, nil
Expand Down Expand Up @@ -137,14 +137,14 @@ func (result *EvalResult) GetRequestHTTPHeadersToRemove() ([]string, error) {
for _, vval := range val {
header, ok := vval.(string)
if !ok {
return nil, fmt.Errorf("type assertion error")
return nil, fmt.Errorf("type assertion error, expected request_headers_to_remove value to be of type 'string' but got '%T'", vval)
}

headersToRemove = append(headersToRemove, header)
}
return headersToRemove, nil
default:
return nil, fmt.Errorf("type assertion error")
return nil, fmt.Errorf("type assertion error, expected request_headers_to_remove to be of type '[]string' but got '%T'", val)
}
}

Expand Down Expand Up @@ -244,7 +244,7 @@ func (result *EvalResult) GetResponseBody() (string, error) {
}

if body, ok = val.(string); !ok {
return "", fmt.Errorf("type assertion error")
return "", fmt.Errorf("type assertion error, expected body to be of type 'string' but got '%T'", val)
}

return body, nil
Expand All @@ -271,7 +271,7 @@ func (result *EvalResult) GetResponseHTTPStatus() (int, error) {
}

if statusCode, ok = val.(json.Number); !ok {
return status, fmt.Errorf("type assertion error")
return status, fmt.Errorf("type assertion error, expected http_status to be of type 'number' but got '%T'", val)
}

httpStatusCode, err := statusCode.Int64()
Expand Down Expand Up @@ -307,7 +307,7 @@ func (result *EvalResult) GetDynamicMetadata() (*_structpb.Struct, error) {

metadata, ok := val.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("type assertion error")
return nil, fmt.Errorf("type assertion error, expected dynamic_metadata to be of type 'object' but got '%T'", val)
}

return structpb.NewStruct(metadata)
Expand Down Expand Up @@ -369,7 +369,7 @@ func transformToHTTPHeaderFormat(input interface{}, result *http.Header) error {
for _, val := range input {
headers, ok := val.(map[string]interface{})
if !ok {
return fmt.Errorf("type assertion error")
return fmt.Errorf("type assertion error, expected headers to be of type 'object' but got '%T'", val)
}

err := takeResponseHeaders(headers, result)
Expand All @@ -385,7 +385,7 @@ func transformToHTTPHeaderFormat(input interface{}, result *http.Header) error {
}

default:
return fmt.Errorf("type assertion error")
return fmt.Errorf("type assertion error, expected headers to be of type 'object' but got '%T'", input)
}

return nil
Expand Down
46 changes: 42 additions & 4 deletions envoyauth/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package envoyauth
import (
"encoding/json"
"reflect"
"strings"
"testing"

_structpb "github.com/golang/protobuf/ptypes/struct"
Expand All @@ -28,6 +29,10 @@ func TestIsAllowed(t *testing.T) {
t.Fatal("Expected error but got nil")
}

if !strings.Contains(err.Error(), "but got 'int'") {
t.Fatal("Assertion error type reflection failed")
}

input["allowed"] = true
var result bool
result, err = er.IsAllowed()
Expand Down Expand Up @@ -107,6 +112,21 @@ func TestGetRequestHTTPHeadersToRemove(t *testing.T) {
}
})
}

t.Run("type_assertion_error", func(t *testing.T) {
er := EvalResult{
Decision: map[string]interface{}{"request_headers_to_remove": 1},
}

_, err := er.GetRequestHTTPHeadersToRemove()
if err == nil {
t.Fatal("Expected error but got nil")
}

if !strings.Contains(err.Error(), "but got 'int'") {
t.Fatalf("Assertion error type reflection failed")
}
})
}

func TestGetResponseHTTPHeadersToAdd(t *testing.T) {
Expand Down Expand Up @@ -295,10 +315,14 @@ func TestGetResponseBody(t *testing.T) {
}

input["body"] = 123
result, err = er.GetResponseBody()
_, err = er.GetResponseBody()
if err == nil {
t.Fatal("Expected error but got nil", err)
}

if !strings.Contains(err.Error(), "but got 'int'") {
t.Fatalf("Assertion error type reflection failed")
}
}

func TestGetResponseHttpStatus(t *testing.T) {
Expand All @@ -317,19 +341,23 @@ func TestGetResponseHttpStatus(t *testing.T) {
}

input["http_status"] = true
result, err = er.GetResponseEnvoyHTTPStatus()
_, err = er.GetResponseEnvoyHTTPStatus()
if err == nil {
t.Fatal("Expected error but got nil")
}

if !strings.Contains(err.Error(), "but got 'bool'") {
t.Fatalf("Assertion error type reflection failed")
}

input["http_status"] = json.Number("1")
result, err = er.GetResponseEnvoyHTTPStatus()
_, err = er.GetResponseEnvoyHTTPStatus()
if err == nil {
t.Fatal("Expected error but got nil")
}

input["http_status"] = json.Number("9999")
result, err = er.GetResponseEnvoyHTTPStatus()
_, err = er.GetResponseEnvoyHTTPStatus()
if err == nil {
t.Fatal("Expected error but got nil")
}
Expand Down Expand Up @@ -380,6 +408,16 @@ func TestGetDynamicMetadata(t *testing.T) {
if !proto.Equal(result, expectedDynamicMetadata) {
t.Fatalf("Expected result %v but got %v", expectedDynamicMetadata, result)
}

input["dynamic_metadata"] = 123
_, err = er.GetDynamicMetadata()
if err == nil {
t.Fatal("Expected error but got nil")
}

if !strings.Contains(err.Error(), "but got 'int'") {
t.Fatalf("Assertion error type reflection failed")
}
}

func TestGetDynamicMetadataWithBooleanDecision(t *testing.T) {
Expand Down

0 comments on commit 66197cd

Please sign in to comment.