Skip to content

Commit

Permalink
Merge branch 'master' into deps/Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F authored Jul 4, 2024
2 parents 1d0359a + f3b61a7 commit 5bbeb1a
Show file tree
Hide file tree
Showing 18 changed files with 2,443 additions and 845 deletions.
1 change: 1 addition & 0 deletions consumer/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (p *httpMockProvider) ExecuteTest(t *testing.T, integrationTest func(MockSe
// Clear state between tests
func (p *httpMockProvider) reset() {
p.mockserver.CleanupMockServer(p.config.Port)
p.mockserver.CleanupPlugins()
p.config.Port = 0
err := p.configure()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions examples/consumer_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var ArrayMinLike = matchers.ArrayMinLike
type Map = matchers.MapMatcher

func TestConsumerV2(t *testing.T) {
log.SetLogLevel("TRACE")
log.SetLogLevel("INFO")

mockProvider, err := consumer.NewV2Pact(consumer.MockHTTPProviderConfig{
Consumer: "PactGoV2Consumer",
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestConsumerV2(t *testing.T) {
}

func TestConsumerV2_Match(t *testing.T) {
log.SetLogLevel("TRACE")
log.SetLogLevel("INFO")

mockProvider, err := consumer.NewV2Pact(consumer.MockHTTPProviderConfig{
Consumer: "PactGoV2ConsumerMatch",
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestConsumerV2_Match(t *testing.T) {
}

func TestConsumerV2AllInOne(t *testing.T) {
log.SetLogLevel("TRACE")
log.SetLogLevel("INFO")

mockProvider, err := consumer.NewV2Pact(consumer.MockHTTPProviderConfig{
Consumer: "PactGoV2ConsumerAllInOne",
Expand Down
4 changes: 2 additions & 2 deletions examples/consumer_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var TimeGenerated = matchers.TimeGenerated
var DateTimeGenerated = matchers.DateTimeGenerated

func TestConsumerV3(t *testing.T) {
log.SetLogLevel("TRACE")
log.SetLogLevel("INFO")

mockProvider, err := consumer.NewV3Pact(consumer.MockHTTPProviderConfig{
Consumer: "PactGoV3Consumer",
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestConsumerV3(t *testing.T) {
assert.NoError(t, err)
}
func TestMessagePact(t *testing.T) {
log.SetLogLevel("TRACE")
log.SetLogLevel("INFO")

provider, err := message.NewMessagePact(message.Config{
Consumer: "PactGoV3MessageConsumer",
Expand Down
2 changes: 1 addition & 1 deletion examples/consumer_v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func TestConsumerV4(t *testing.T) {
log.SetLogLevel("TRACE")
log.SetLogLevel("INFO")

mockProvider, err := consumer.NewV4Pact(consumer.MockHTTPProviderConfig{
Consumer: "PactGoV4Consumer",
Expand Down
2 changes: 1 addition & 1 deletion examples/grpc/grpc_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

func TestGrpcProvider(t *testing.T) {
go startProvider()
l.SetLogLevel("TRACE")
l.SetLogLevel("INFO")

verifier := provider.NewVerifier()

Expand Down
2 changes: 1 addition & 1 deletion examples/protobuf-message/protobuf_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestPluginMessageProvider(t *testing.T) {
var dir, _ = os.Getwd()
var pactDir = fmt.Sprintf("%s/../pacts", dir)

err := pactlog.SetLogLevel("TRACE")
err := pactlog.SetLogLevel("INFO")
assert.NoError(t, err)

pactversion.CheckVersion()
Expand Down
126 changes: 17 additions & 109 deletions internal/native/message_server.go
Original file line number Diff line number Diff line change
@@ -1,99 +1,7 @@
package native

/*
// Library headers
#include <stdlib.h>
#include <stdint.h>
typedef int bool;
#define true 1
#define false 0
/// Wraps a Pact model struct
typedef struct InteractionHandle InteractionHandle;
typedef struct PactMessageIterator PactMessageIterator;
typedef struct SynchronousMessage SynchronousMessage;
typedef struct Message Message;
struct InteractionHandle {
unsigned int interaction_ref;
};
/// Wraps a Pact model struct
typedef struct PactHandle PactHandle;
struct PactHandle {
unsigned int pact_ref;
};
PactHandle pactffi_new_message_pact(const char *consumer_name, const char *provider_name);
InteractionHandle pactffi_new_message(PactHandle pact, const char *description);
// Creates a new synchronous message interaction (request/response) and return a handle to it
InteractionHandle pactffi_new_sync_message_interaction(PactHandle pact, const char *description);
// Creates a new asynchronous message interaction (request/response) and return a handle to it
InteractionHandle pactffi_new_message_interaction(PactHandle pact, const char *description);
void pactffi_message_expects_to_receive(InteractionHandle message, const char *description);
void pactffi_message_given(InteractionHandle message, const char *description);
void pactffi_message_given_with_param(InteractionHandle message, const char *description, const char *name, const char *value);
void pactffi_message_with_contents(InteractionHandle message, const char *content_type, const char *body, int size);
void pactffi_message_with_metadata(InteractionHandle message, const char *key, const char *value);
int pactffi_write_message_pact_file(PactHandle pact, const char *directory, bool overwrite);
void pactffi_with_message_pact_metadata(PactHandle pact, const char *namespace, const char *name, const char *value);
int pactffi_write_pact_file(int mock_server_port, const char *directory, bool overwrite);
bool pactffi_given(InteractionHandle interaction, const char *description);
bool pactffi_given_with_param(InteractionHandle interaction, const char *description, const char *name, const char *value);
void pactffi_with_specification(PactHandle pact, int specification_version);
int pactffi_using_plugin(PactHandle pact, const char *plugin_name, const char *plugin_version);
void pactffi_cleanup_plugins(PactHandle pact);
int pactffi_interaction_contents(InteractionHandle interaction, int interaction_part, const char *content_type, const char *contents);
// Create a mock server for the provided Pact handle and transport.
int pactffi_create_mock_server_for_transport(PactHandle pact, const char *addr, int port, const char *transport, const char *transport_config);
bool pactffi_cleanup_mock_server(int mock_server_port);
char* pactffi_mock_server_mismatches(int mock_server_port);
bool pactffi_mock_server_matched(int mock_server_port);
// Functions to get message contents
// Get the length of the request contents of a `SynchronousMessage`.
size_t pactffi_sync_message_get_request_contents_length(SynchronousMessage *message);
struct PactSyncMessageIterator *pactffi_pact_handle_get_sync_message_iter(PactHandle pact);
struct SynchronousMessage *pactffi_pact_sync_message_iter_next(struct PactSyncMessageIterator *iter);
// Async
// Get the length of the contents of a `Message`.
size_t pactffi_message_get_contents_length(Message *message);
// Get the contents of a `Message` as a pointer to an array of bytes.
const unsigned char *pactffi_message_get_contents_bin(const Message *message);
struct PactMessageIterator *pactffi_pact_handle_get_message_iter(PactHandle pact);
struct Message *pactffi_pact_message_iter_next(struct PactMessageIterator *iter);
// Need the index of the body to get
const unsigned char *pactffi_sync_message_get_response_contents_bin(const struct SynchronousMessage *message, size_t index);
size_t pactffi_sync_message_get_response_contents_length(const struct SynchronousMessage *message, size_t index);
// Sync
// Get the request contents of a `SynchronousMessage` as a pointer to an array of bytes.
// The number of bytes in the buffer will be returned by `pactffi_sync_message_get_request_contents_length`.
const unsigned char *pactffi_sync_message_get_request_contents_bin(SynchronousMessage *message);
// Set Sync message request body - non binary
void pactffi_sync_message_set_request_contents(InteractionHandle *message, const char *contents, const char *content_type);
// Set Sync message request body - binary
void pactffi_sync_message_set_request_contents_bin(InteractionHandle *message, const unsigned char *contents, size_t len, const char *content_type);
// Set sync message response contents - non binary
void pactffi_sync_message_set_response_contents(InteractionHandle *message, size_t index, const char *contents, const char *content_type);
// Set sync message response contents - binary
void pactffi_sync_message_set_response_contents_bin(InteractionHandle *message, size_t index, const unsigned char *contents, size_t len, const char *content_type);
// Can be used instead of the above as a general abstraction for non-binary bodies
bool pactffi_with_body(InteractionHandle interaction, int interaction_part, const char *content_type, const char *body);
// Can be used instead of the above as a general abstraction for binary bodies
// bool pactffi_with_binary_file(InteractionHandle interaction, int interaction_part, const char *content_type, const uint8_t *body, size_t size);
bool pactffi_with_binary_file(InteractionHandle interaction, int interaction_part, const char *content_type, const char *body, int size);
#include "pact.h"
*/
import "C"

Expand Down Expand Up @@ -266,9 +174,9 @@ func (m *Message) WithRequestBinaryContents(body []byte) *Message {
defer free(cHeader)

// TODO: handle response
res := C.pactffi_with_binary_file(m.handle, C.int(INTERACTION_PART_REQUEST), cHeader, (*C.char)(unsafe.Pointer(&body[0])), C.int(len(body)))
res := C.pactffi_with_binary_file(m.handle, C.int(INTERACTION_PART_REQUEST), cHeader, (*C.uchar)(unsafe.Pointer(&body[0])), C.ulong(len(body)))

log.Println("[DEBUG] WithRequestBinaryContents - pactffi_with_binary_file returned", int(res))
log.Println("[DEBUG] WithRequestBinaryContents - pactffi_with_binary_file returned", bool(res))

return m
}
Expand All @@ -277,9 +185,9 @@ func (m *Message) WithRequestBinaryContentType(contentType string, body []byte)
defer free(cHeader)

// TODO: handle response
res := C.pactffi_with_binary_file(m.handle, C.int(INTERACTION_PART_REQUEST), cHeader, (*C.char)(unsafe.Pointer(&body[0])), C.int(len(body)))
res := C.pactffi_with_binary_file(m.handle, C.int(INTERACTION_PART_REQUEST), cHeader, (*C.uchar)(unsafe.Pointer(&body[0])), C.ulong(len(body)))

log.Println("[DEBUG] WithRequestBinaryContents - pactffi_with_binary_file returned", int(res))
log.Println("[DEBUG] WithRequestBinaryContents - pactffi_with_binary_file returned", res)

return m
}
Expand All @@ -297,7 +205,7 @@ func (m *Message) WithResponseBinaryContents(body []byte) *Message {
defer free(cHeader)

// TODO: handle response
C.pactffi_with_binary_file(m.handle, C.int(INTERACTION_PART_RESPONSE), cHeader, (*C.char)(unsafe.Pointer(&body[0])), C.int(len(body)))
C.pactffi_with_binary_file(m.handle, C.int(INTERACTION_PART_RESPONSE), cHeader, (*C.uchar)(unsafe.Pointer(&body[0])), C.ulong(len(body)))

return m
}
Expand All @@ -316,7 +224,7 @@ func (m *Message) WithContents(part interactionPart, contentType string, body []
defer free(cHeader)

res := C.pactffi_with_body(m.handle, C.int(part), cHeader, (*C.char)(unsafe.Pointer(&body[0])))
log.Println("[DEBUG] response from pactffi_interaction_contents", (int(res) == 1))
log.Println("[DEBUG] response from pactffi_interaction_contents", (bool(res)))

return m
}
Expand All @@ -330,8 +238,8 @@ func (m *MessageServer) UsingPlugin(pluginName string, pluginVersion string) err
cPluginVersion := C.CString(pluginVersion)
defer free(cPluginVersion)

InstallSignalHandlers()
r := C.pactffi_using_plugin(m.messagePact.handle, cPluginName, cPluginVersion)
InstallSignalHandlers()

// 1 - A general panic was caught.
// 2 - Failed to load the plugin.
Expand Down Expand Up @@ -535,7 +443,7 @@ func (m *MessageServer) StartTransport(transport string, address string, port in
cConfig := C.CString(configJson)
defer free(cConfig)

p := C.pactffi_create_mock_server_for_transport(m.messagePact.handle, cAddress, C.int(port), cTransport, cConfig)
p := C.pactffi_create_mock_server_for_transport(m.messagePact.handle, cAddress, C.ushort(port), cTransport, cConfig)

// | Error | Description
// |-------|-------------
Expand Down Expand Up @@ -578,7 +486,7 @@ func (m *MessageServer) CleanupMockServer(port int) bool {
log.Println("[DEBUG] mock server cleaning up port:", port)
res := C.pactffi_cleanup_mock_server(C.int(port))

return int(res) == 1
return bool(res)
}

// MockServerMismatchedRequests returns a JSON object containing any mismatches from
Expand Down Expand Up @@ -616,7 +524,7 @@ func (m *MessageServer) MockServerMatched(port int) bool {
// log.Println("MATCHED RES?")
// log.Println(int(res))

return int(res) == 1
return bool(res)
}

// WritePactFile writes the Pact to file.
Expand All @@ -625,12 +533,12 @@ func (m *MessageServer) WritePactFile(dir string, overwrite bool) error {
cDir := C.CString(dir)
defer free(cDir)

overwritePact := 0
overwritePact := false
if overwrite {
overwritePact = 1
overwritePact = true
}

res := int(C.pactffi_write_message_pact_file(m.messagePact.handle, cDir, C.int(overwritePact)))
res := int(C.pactffi_write_message_pact_file(m.messagePact.handle, cDir, C.bool(overwritePact)))

/// | Error | Description |
/// |-------|-------------|
Expand All @@ -654,12 +562,12 @@ func (m *MessageServer) WritePactFileForServer(port int, dir string, overwrite b
cDir := C.CString(dir)
defer free(cDir)

overwritePact := 0
overwritePact := false
if overwrite {
overwritePact = 1
overwritePact = true
}

res := int(C.pactffi_write_pact_file(C.int(port), cDir, C.int(overwritePact)))
res := int(C.pactffi_write_pact_file(C.int(port), cDir, C.bool(overwritePact)))

/// | Error | Description |
/// |-------|-------------|
Expand Down
9 changes: 7 additions & 2 deletions internal/native/message_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func TestGetPluginSyncMessageContentsAsBytes(t *testing.T) {
m := NewMessageServer("test-message-consumer", "test-message-provider")

// Protobuf plugin test
defer m.CleanupPlugins()
err := m.UsingPlugin("protobuf", "0.3.15")
assert.NoError(t, err)

Expand Down Expand Up @@ -268,6 +269,7 @@ func TestGetPluginSyncMessageContentsAsBytes_EmptyResponse(t *testing.T) {
m := NewMessageServer("test-message-consumer", "test-message-provider")

// Protobuf plugin test
defer m.CleanupPlugins()
err := m.UsingPlugin("protobuf", "0.3.15")
assert.NoError(t, err)

Expand Down Expand Up @@ -314,6 +316,7 @@ func TestGetPluginAsyncMessageContentsAsBytes(t *testing.T) {
m := NewMessageServer("test-message-consumer", "test-message-provider")

// Protobuf plugin test
defer m.CleanupPlugins()
_ = m.UsingPlugin("protobuf", "0.3.15")

i := m.NewAsyncMessageInteraction("grpc interaction")
Expand Down Expand Up @@ -349,11 +352,12 @@ func TestGetPluginAsyncMessageContentsAsBytes(t *testing.T) {
func TestGrpcPluginInteraction(t *testing.T) {
tmpPactFolder, err := os.MkdirTemp("", "pact-go")
assert.NoError(t, err)
_ = log.SetLogLevel("TRACE")
_ = log.SetLogLevel("INFO")

m := NewMessageServer("test-message-consumer", "test-message-provider")

// Protobuf plugin test
defer m.CleanupPlugins()
_ = m.UsingPlugin("protobuf", "0.3.15")

i := m.NewSyncMessageInteraction("grpc interaction")
Expand Down Expand Up @@ -426,11 +430,12 @@ func TestGrpcPluginInteraction(t *testing.T) {
func TestGrpcPluginInteraction_ErrorResponse(t *testing.T) {
tmpPactFolder, err := os.MkdirTemp("", "pact-go")
assert.NoError(t, err)
_ = log.SetLogLevel("TRACE")
_ = log.SetLogLevel("INFO")

m := NewMessageServer("test-message-consumer", "test-message-provider")

// Protobuf plugin test
defer m.CleanupPlugins()
_ = m.UsingPlugin("protobuf", "0.3.15")

i := m.NewSyncMessageInteraction("grpc interaction")
Expand Down
Loading

0 comments on commit 5bbeb1a

Please sign in to comment.