github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/protocol/v2/request_test.go (about)

     1  // Copyright (c) 2021-2022, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package v2
     6  
     7  import (
     8  	"time"
     9  
    10  	"github.com/choria-io/go-choria/protocol"
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Request", func() {
    16  	Describe("Filter", func() {
    17  		It("Should report the correct filter", func() {
    18  			r := Request{}
    19  			f, filtered := r.Filter()
    20  			Expect(filtered).To(BeFalse())
    21  			Expect(f.Empty()).To(BeTrue())
    22  
    23  			f = protocol.NewFilter()
    24  			f.AddClassFilter("x")
    25  			r.SetFilter(f)
    26  			f, filtered = r.Filter()
    27  			Expect(filtered).To(BeTrue())
    28  			Expect(f.Empty()).To(BeFalse())
    29  			Expect(f.ClassFilters()).To(Equal([]string{"x"}))
    30  		})
    31  	})
    32  
    33  	Describe("SetMessage", func() {
    34  		It("Should correctly set the message", func() {
    35  			r := Request{}
    36  			Expect(r.MessageBody).To(BeNil())
    37  			r.SetMessage([]byte("hello world"))
    38  			Expect(r.MessageBody).To(Equal([]byte("hello world")))
    39  		})
    40  	})
    41  
    42  	Describe("NewRequestFromSecureRequest", func() {
    43  		It("Should ensure the secure request is the compatible version", func() {
    44  			r, err := NewRequestFromSecureRequest(&SecureRequest{Protocol: protocol.SecureRequestV1})
    45  			Expect(err).To(MatchError("cannot create a version 2 SecureRequest from a choria:secure:request:1 SecureRequest"))
    46  			Expect(r).To(BeNil())
    47  		})
    48  
    49  		It("Should validate the secure request and fail for invalid ones", func() {
    50  			r, err := NewRequestFromSecureRequest(&SecureRequest{Protocol: protocol.SecureRequestV2, MessageBody: []byte("{}")})
    51  			Expect(err).To(MatchError("the JSON body from the SecureRequest is not a valid Request message: supplied JSON document does not pass schema validation: missing properties: 'protocol', 'message', 'id', 'sender', 'caller', 'collective', 'agent', 'ttl', 'time'"))
    52  			Expect(r).To(BeNil())
    53  		})
    54  
    55  		It("Should create a valid request", func() {
    56  			req, err := NewRequest("ginkgo", "go.tests", "choria=test", 120, "a2f0ca717c694f2086cfa81b6c494648", "choria")
    57  			req.SetMessage([]byte("hello world"))
    58  			Expect(err).ToNot(HaveOccurred())
    59  			j, err := req.JSON()
    60  			Expect(err).ToNot(HaveOccurred())
    61  
    62  			r, err := NewRequestFromSecureRequest(&SecureRequest{Protocol: protocol.SecureRequestV2, MessageBody: j, CallerJWT: "caller.jwt"})
    63  			Expect(err).ToNot(HaveOccurred())
    64  			Expect(r.RequestID()).To(Equal("a2f0ca717c694f2086cfa81b6c494648"))
    65  			Expect(r.CallerPublicData()).To(Equal("caller.jwt"))
    66  		})
    67  	})
    68  
    69  	Describe("RecordNetworkHop", func() {
    70  		It("Should record the hop correctly", func() {
    71  			r := &Request{}
    72  			Expect(r.ReqEnvelope.seenBy).To(BeEmpty())
    73  			r.RecordNetworkHop("ginkgo.in", "server1", "ginkgo.out")
    74  			Expect(r.ReqEnvelope.seenBy).To(HaveLen(1))
    75  			r.RecordNetworkHop("ginkgo.in", "server2", "ginkgo.out")
    76  			Expect(r.ReqEnvelope.seenBy).To(HaveLen(2))
    77  			Expect(r.ReqEnvelope.seenBy[0]).To(Equal([3]string{"ginkgo.in", "server1", "ginkgo.out"}))
    78  		})
    79  	})
    80  
    81  	Describe("NetworkHops", func() {
    82  		It("Should report the correct hops", func() {
    83  			r := &Request{}
    84  			r.RecordNetworkHop("ginkgo.in", "server1", "ginkgo.out")
    85  			r.RecordNetworkHop("ginkgo.in", "server2", "ginkgo.out")
    86  			Expect(r.ReqEnvelope.seenBy).To(HaveLen(2))
    87  			hops := r.NetworkHops()
    88  			Expect(hops).To(HaveLen(2))
    89  			Expect(hops[0]).To(Equal([3]string{"ginkgo.in", "server1", "ginkgo.out"}))
    90  		})
    91  	})
    92  
    93  	Describe("Federation", func() {
    94  		Describe("SetFederationRequestID", func() {
    95  			It("Should set the id correctly", func() {
    96  				r := Request{}
    97  				id, federated := r.FederationRequestID()
    98  				Expect(federated).To(BeFalse())
    99  				Expect(id).To(Equal(""))
   100  
   101  				r.SetFederationRequestID("123")
   102  				id, federated = r.FederationRequestID()
   103  				Expect(federated).To(BeTrue())
   104  				Expect(id).To(Equal("123"))
   105  			})
   106  		})
   107  
   108  		Describe("SetUnfederated", func() {
   109  			It("Should correctly unfederate the message", func() {
   110  				r := Request{}
   111  				r.SetFederationRequestID("123")
   112  				Expect(r.IsFederated()).To(BeTrue())
   113  				r.SetUnfederated()
   114  				Expect(r.IsFederated()).To(BeFalse())
   115  			})
   116  		})
   117  
   118  		Describe("SetFederationTargets", func() {
   119  			It("Should set the federation targets correctly", func() {
   120  				r := &Request{}
   121  				t, federated := r.FederationTargets()
   122  				Expect(federated).To(BeFalse())
   123  				Expect(t).To(BeEmpty())
   124  				r.SetFederationTargets([]string{"1", "2"})
   125  				t, federated = r.FederationTargets()
   126  				Expect(federated).To(BeTrue())
   127  				Expect(t).To(Equal([]string{"1", "2"}))
   128  			})
   129  		})
   130  
   131  		Describe("SetFederationReplyTo", func() {
   132  			It("Should set the federation reply correctly", func() {
   133  				r := Request{}
   134  				rt, federated := r.FederationReplyTo()
   135  				Expect(federated).To(BeFalse())
   136  				Expect(rt).To(Equal(""))
   137  
   138  				r.SetFederationReplyTo("reply.to")
   139  				rt, federated = r.FederationReplyTo()
   140  				Expect(federated).To(BeTrue())
   141  				Expect(rt).To(Equal("reply.to"))
   142  			})
   143  		})
   144  
   145  		Describe("IsFederated", func() {
   146  			It("Should report correctly", func() {
   147  				r := Request{}
   148  				Expect(r.IsFederated()).To(BeFalse())
   149  				r.SetFederationReplyTo("reply.to")
   150  				Expect(r.IsFederated()).To(BeTrue())
   151  			})
   152  		})
   153  	})
   154  
   155  	Describe("NewRequest", func() {
   156  		It("Should validate requests against the schema", func() {
   157  			req, err := NewRequest("", "go.tests", "choria=test", 120, "a2f0ca717c694f2086cfa81b6c494648", "mcollective")
   158  			Expect(err).ToNot(HaveOccurred())
   159  			_, err = req.JSON()
   160  			Expect(err).To(MatchError(ErrInvalidJSON))
   161  		})
   162  
   163  		It("Should construct the correct request", func() {
   164  			request, err := NewRequest("test", "go.tests", "choria=test", 120, "a2f0ca717c694f2086cfa81b6c494648", "mcollective")
   165  			Expect(err).ToNot(HaveOccurred())
   166  			filter, filtered := request.Filter()
   167  
   168  			request.SetMessage([]byte("hello world"))
   169  
   170  			j, err := request.JSON()
   171  			Expect(err).ToNot(HaveOccurred())
   172  
   173  			Expect(protocol.VersionFromJSON(j)).To(Equal(protocol.RequestV2))
   174  			Expect(request.Version()).To(Equal(protocol.RequestV2))
   175  			Expect(request.Message()).To(Equal([]byte("hello world")))
   176  			Expect(request.RequestID()).To(HaveLen(32))
   177  			Expect(request.SenderID()).To(Equal("go.tests"))
   178  			Expect(request.CallerID()).To(Equal("choria=test"))
   179  			Expect(request.Collective()).To(Equal("mcollective"))
   180  			Expect(request.Agent()).To(Equal("test"))
   181  			Expect(request.TTL()).To(Equal(120))
   182  			Expect(request.Time()).To(BeTemporally("~", time.Now(), time.Second))
   183  			Expect(filtered).To(BeFalse())
   184  			Expect(filter.Empty()).To(BeTrue())
   185  
   186  			filter.AddAgentFilter("rpcutil")
   187  			filter, filtered = request.Filter()
   188  			Expect(filtered).To(BeTrue())
   189  			Expect(filter).ToNot(BeNil())
   190  
   191  			filter.AddAgentFilter("other")
   192  			filter, filtered = request.Filter()
   193  			Expect(filtered).To(BeTrue())
   194  			Expect(filter).ToNot(BeNil())
   195  		})
   196  	})
   197  })