From a82eff8fe941f1a10c6a819f85de7443f3062a69 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Fri, 25 Oct 2024 17:28:23 +0100 Subject: [PATCH] fix: ensure sync messages write metadata to both req & res --- internal/native/message_server.go | 26 ++++++++++++++++++++++++++ message/v4/synchronous_message.go | 4 ++-- message/v4/synchronous_message_test.go | 5 ++--- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/internal/native/message_server.go b/internal/native/message_server.go index 31629ba84..76d5fe19a 100644 --- a/internal/native/message_server.go +++ b/internal/native/message_server.go @@ -166,6 +166,32 @@ func (m *Message) WithMetadata(valueOrMatcher map[string]string) *Message { return m } +func (m *Message) WithRequestMetadata(valueOrMatcher map[string]string) *Message { + for k, v := range valueOrMatcher { + cName := C.CString(k) + cValue := C.CString(v) + + C.pactffi_with_metadata(m.handle, cName, cValue, 0) + + free(cValue) + free(cName) + } + + return m +} +func (m *Message) WithResponseMetadata(valueOrMatcher map[string]string) *Message { + for k, v := range valueOrMatcher { + cName := C.CString(k) + cValue := C.CString(v) + + C.pactffi_with_metadata(m.handle, cName, cValue, 1) + + free(cValue) + free(cName) + } + + return m +} func (m *Message) WithRequestBinaryContents(body []byte) *Message { cHeader := C.CString("application/octet-stream") diff --git a/message/v4/synchronous_message.go b/message/v4/synchronous_message.go index 61264721a..a2e5b675c 100644 --- a/message/v4/synchronous_message.go +++ b/message/v4/synchronous_message.go @@ -115,7 +115,7 @@ type SynchronousMessageWithRequestBuilder struct { // to go with the content // func (m *Message) WithMetadata(metadata MapMatcher) *Message { func (m *SynchronousMessageWithRequestBuilder) WithMetadata(metadata map[string]string) *SynchronousMessageWithRequestBuilder { - m.messageHandle.WithMetadata(metadata) + m.messageHandle.WithRequestMetadata(metadata) return m } @@ -164,7 +164,7 @@ type SynchronousMessageWithResponseBuilder struct { // to go with the content // func (m *Message) WithMetadata(metadata MapMatcher) *Message { func (m *SynchronousMessageWithResponseBuilder) WithMetadata(metadata map[string]string) *SynchronousMessageWithResponseBuilder { - m.messageHandle.WithMetadata(metadata) + m.messageHandle.WithResponseMetadata(metadata) return m } diff --git a/message/v4/synchronous_message_test.go b/message/v4/synchronous_message_test.go index 3beb76b7e..0c5dd147f 100644 --- a/message/v4/synchronous_message_test.go +++ b/message/v4/synchronous_message_test.go @@ -15,18 +15,17 @@ func TestSyncTypeSystem_NoPlugin(t *testing.T) { p, _ := NewSynchronousPact(Config{ Consumer: "consumer", Provider: "provider", - PactDir: "/tmp/", }) // Sync - no plugin err := p.AddSynchronousMessage("some description"). Given("some state"). WithRequest(func(r *SynchronousMessageWithRequestBuilder) { r.WithJSONContent(map[string]string{"foo": "bar"}) - r.WithMetadata(map[string]string{}) + r.WithMetadata(map[string]string{"meta_request": "meta_request_data"}) }). WithResponse(func(r *SynchronousMessageWithResponseBuilder) { r.WithJSONContent(map[string]string{"foo": "bar"}) - r.WithMetadata(map[string]string{}) + r.WithMetadata(map[string]string{"meta_response": "meta_response_data"}) }). ExecuteTest(t, func(m SynchronousMessage) error { // In this scenario, we have no real transport, so we need to mock/handle both directions