Skip to content

Commit

Permalink
Merge pull request #461 from pact-foundation/fix/sync_message_metadata
Browse files Browse the repository at this point in the history
fix: ensure sync messages write metadata to both req & res
  • Loading branch information
mefellows authored Nov 14, 2024
2 parents 65ce078 + a82eff8 commit bbc852f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
26 changes: 26 additions & 0 deletions internal/native/message_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions message/v4/synchronous_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions message/v4/synchronous_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bbc852f

Please sign in to comment.