Skip to content

Commit

Permalink
feat: add plugin to consumer interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Mar 20, 2022
1 parent 76baab8 commit ea0f536
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 16 deletions.
62 changes: 61 additions & 1 deletion src/consumer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export const makeConsumerPact = (
}

return {
addPlugin: (name: string, version: string) => {
ffi.pactffiUsingPlugin(pactPtr, name, version);
},
cleanupPlugins: () => {
ffi.pactffiCleanupPlugins(pactPtr);
},
createMockServer: (
address: string,
requestedPort?: number,
Expand Down Expand Up @@ -80,7 +86,6 @@ export const makeConsumerPact = (
}
return port;
},

mockServerMatchedSuccessfully: (port: number) => {
return ffi.pactffiMockServerMatched(port);
},
Expand Down Expand Up @@ -219,6 +224,31 @@ export const makeConsumerPact = (
withStatus: (status: number) => {
return ffi.pactffiResponseStatus(interactionPtr, status);
},
withPluginRequestInteractionContents: (
contentType: string,
contents: string
) => {
ffi.pactffiPluginInteractionContents(
interactionPtr,
INTERACTION_PART_REQUEST,
contentType,
contents
);

return true;
},
withPluginResponseInteractionContents: (
contentType: string,
contents: string
) => {
ffi.pactffiPluginInteractionContents(
interactionPtr,
INTERACTION_PART_RESPONSE,
contentType,
contents
);
return true;
},
});
},
};
Expand All @@ -243,6 +273,12 @@ export const makeConsumerAsyncMessagePact = (
}

return {
addPlugin: (name: string, version: string) => {
ffi.pactffiUsingPlugin(pactPtr, name, version);
},
cleanupPlugins: () => {
ffi.pactffiCleanupPlugins(pactPtr);
},
writePactFile: (dir: string, merge = true) =>
writePact(ffi, pactPtr, dir, merge),
addMetadata: (namespace: string, name: string, value: string): boolean => {
Expand All @@ -252,6 +288,30 @@ export const makeConsumerAsyncMessagePact = (
const interactionPtr = ffi.pactffiNewAsyncMessage(pactPtr, description);

return {
withPluginRequestInteractionContents: (
contentType: string,
contents: string
) => {
ffi.pactffiPluginInteractionContents(
interactionPtr,
INTERACTION_PART_REQUEST,
contentType,
contents
);
return true;
},
withPluginResponseInteractionContents: (
contentType: string,
contents: string
) => {
ffi.pactffiPluginInteractionContents(
interactionPtr,
INTERACTION_PART_RESPONSE,
contentType,
contents
);
return true;
},
expectsToReceive: (description: string) => {
return ffi.pactffiMessageExpectsToReceive(
interactionPtr,
Expand Down
32 changes: 28 additions & 4 deletions src/consumer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,23 @@ export type RequestMismatch = {
body?: string;
};

export type ConsumerInteraction = {
export type PluginInteraction = {
withPluginRequestInteractionContents: (
contentType: string,
contents: string
) => boolean;
withPluginResponseInteractionContents: (
contentType: string,
contents: string
) => boolean;
};

export type PluginPact = {
addPlugin: (plugin: string, version: string) => void;
cleanupPlugins: () => void;
};

export type ConsumerInteraction = PluginInteraction & {
uponReceiving: (description: string) => boolean;
given: (state: string) => boolean;
givenWithParam: (state: string, name: string, value: string) => boolean;
Expand All @@ -133,9 +149,17 @@ export type ConsumerInteraction = {
filename: string,
mimePartName: string
) => boolean;
withPluginRequestInteractionContents: (
contentType: string,
contents: string
) => void;
withPluginResponseInteractionContents: (
contentType: string,
contents: string
) => void;
};

export type ConsumerPact = {
export type ConsumerPact = PluginPact & {
newInteraction: (description: string) => ConsumerInteraction;
createMockServer: (address: string, port?: number, tls?: boolean) => number;
mockServerMismatches: (port: number) => MatchingResult[];
Expand All @@ -161,7 +185,7 @@ export type ConsumerPact = {
addMetadata: (namespace: string, name: string, value: string) => boolean;
};

export type ConsumerMessage = {
export type ConsumerMessage = PluginInteraction & {
given: (state: string) => void;
givenWithParam: (state: string, name: string, value: string) => void;
expectsToReceive: (description: string) => void;
Expand All @@ -170,7 +194,7 @@ export type ConsumerMessage = {
reifyMessage: () => string;
};

export type ConsumerMessagePact = {
export type ConsumerMessagePact = PluginPact & {
newMessage: (description: string) => ConsumerMessage;
/**
* This function writes the pact file, regardless of whether or not the test was successful.
Expand Down
32 changes: 21 additions & 11 deletions src/ffi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,28 @@ export type Ffi = {
pactffiLogToStdout(level: FfiLogLevelFilter): number;
pactffiLogToFile(fileName: string, level: FfiLogLevelFilter): number;
pactffiFetchLogBuffer(logId: number): string;
pactffiUsingPlugin(handle: FfiPactHandle): FfiConfigurePluginResponse;
pactffiUsingPlugin(
handle: FfiPactHandle,
name: string,
version: string
): FfiConfigurePluginResponse;
pactffiCleanupPlugins(handle: FfiPactHandle): void;
pactffiPluginInteractionContents(): FfiPluginInteractionResponse;
pactffiPluginInteractionContents(
handle: FfiInteractionHandle,
part: FfiInteractionPart,
contentType: string,
contents: string
): void;
pactffiNewAsyncMessage(
handle: FfiPactHandle,
description: string
): FfiMessageHandle;
// TODO: not sure how to use this given the return type, commenting out for now
// pactffiNewSyncMessage(
// handle: FfiPactHandle,
// description: string
// ): FfiInteractionHandle;
// TODO: removing the old "FfiMessagePactHandle" based methods
// pactffiNewMessagePact(
// consumer: string,
// provider: string
Expand All @@ -221,15 +240,6 @@ export type Ffi = {
// dir: string,
// overwrite: boolean
// ): FfiWriteMessagePactResponse;
pactffiNewAsyncMessage(
handle: FfiPactHandle,
description: string
): FfiMessageHandle;
pactffiNewSyncMessage(
handle: FfiPactHandle,
description: string
): FfiInteractionHandle;
// This uses the MessagePactHandle
// pactffiNewMessage(
// handle: FfiMessagePactHandle,
// description: string
Expand Down

0 comments on commit ea0f536

Please sign in to comment.