From 777fd1d8781f467ac2e80b301af380948fe0b86e Mon Sep 17 00:00:00 2001 From: tjons Date: Mon, 6 Nov 2023 17:14:32 +0000 Subject: [PATCH] add response test for dynamic metadata Signed-off-by: tjons --- envoyauth/response_test.go | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/envoyauth/response_test.go b/envoyauth/response_test.go index 28a8819c8..3b754e330 100644 --- a/envoyauth/response_test.go +++ b/envoyauth/response_test.go @@ -4,6 +4,9 @@ import ( "encoding/json" "reflect" "testing" + + _structpb "github.com/golang/protobuf/ptypes/struct" + "google.golang.org/protobuf/proto" ) func TestIsAllowed(t *testing.T) { @@ -341,3 +344,40 @@ func TestGetResponseHttpStatus(t *testing.T) { t.Fatalf("Expected http status code \"BadRequest\" but got %v", result.GetCode().String()) } } + +func TestGetDynamicMetadata(t *testing.T) { + input := make(map[string]interface{}) + er := EvalResult{ + Decision: input, + } + + result, err := er.GetDynamicMetadata() + if err != nil { + t.Fatalf("Expected no error but got %v", err) + } + + if result != nil { + t.Fatalf("Expected no dynamic metadata but got %v", result) + } + + input["dynamic_metadata"] = map[string]interface{}{ + "foo": "bar", + } + result, err = er.GetDynamicMetadata() + if err != nil { + t.Fatalf("Expected no error but got %v", err) + } + + expectedDynamicMetadata := &_structpb.Struct{ + Fields: map[string]*_structpb.Value{ + "foo": { + Kind: &_structpb.Value_StringValue{ + StringValue: "bar", + }, + }, + }, + } + if !proto.Equal(result, expectedDynamicMetadata) { + t.Fatalf("Expected result %v but got %v", expectedDynamicMetadata, result) + } +}