Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,35 @@ jobs:

steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
if: github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')
if: github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))
with:
fetch-depth: 0

- name: Use Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
if: github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')
if: github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))
with:
node-version: ${{ env.NODE_VERSION }}

- if: ${{ runner.os == 'Windows' && ( github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')) }}
- if: ${{ runner.os == 'Windows' && ( github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))) }}
run: echo "ONLY_DOWNLOAD_PACT_FOR_WINDOWS=true" >> $GITHUB_ENV
- if: ${{ (matrix.docker == true && matrix.alpine == true) && (github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')) }}
- if: ${{ (matrix.docker == true && matrix.alpine == true) && (github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))) }}
name: prebuild linux ${{ matrix.arch }} musl
run: docker run -v $PWD:/home --platform linux/${{ matrix.arch }} --rm node:20-alpine bin/sh -c 'apk add bash && cd /home && bash -c "/home/script/ci/prebuild-alpine.sh" && rm -rf ffi node_modules'

- if: ${{ (matrix.docker == true && matrix.alpine != true )&& (github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')) }}
- if: ${{ (matrix.docker == true && matrix.alpine != true )&& (github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))) }}
name: prebuild linux ${{ matrix.arch }}
run: docker run -v $PWD:/home --platform linux/${{ matrix.arch }} --rm node:20 bin/bash -c 'cd /home && /home/script/ci/prebuild.sh && rm -rf ffi node_modules'

- run: sudo chown -R $(id -u):$(id -g) prebuilds
if: ${{ matrix.docker == true && (github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')) }}
if: ${{ matrix.docker == true && (github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))) }}

- run: ./script/ci/prebuild.sh
if: ${{ matrix.docker != true && (github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')) }}
if: ${{ matrix.docker != true && (github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))) }}

- name: Upload prebuild for ${{ runner.os }}-${{ runner.arch }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
if: github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')
if: github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))
with:
path: prebuilds/*.tar.gz
name: artifact-${{ matrix.docker == true && matrix.alpine == true && 'linux-musl' || matrix.docker == true && matrix.alpine == false && 'linux' || matrix.os }}-${{ matrix.arch }}
Expand Down Expand Up @@ -180,10 +180,10 @@ jobs:
fetch-depth: 0

- name: Download prebuilds
if: github.ref == 'refs/heads/master' || contains(github.event.pull_request.title, '(rebuild)')
if: github.ref == 'refs/heads/master' || (true || contains(github.event.pull_request.title, '(rebuild)'))
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
- run: FETCH_ASSETS=true REPO=pact-foundation/pact-js-core ./script/ci/check-release-libs.sh --fetch-assets
if: ${{ github.ref != 'refs/heads/master' && !contains(github.event.pull_request.title, '(rebuild)')}}
if: ${{ github.ref != 'refs/heads/master' && !(true || contains(github.event.pull_request.title, '(rebuild)'))}}
env:
GITHUB_TOKEN: ${{ github.token }}

Expand Down
2 changes: 1 addition & 1 deletion script/lib/download-ffi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require_binary mktemp

require_env_var FFI_VERSION

BASEURL=https://github.com/pact-foundation/pact-reference/releases/download
BASEURL=${PACT_FFI_BASEURL:-https://github.com/YOU54F/pact-reference/releases/download}
FFI_DIR="${LIB_DIR}/../../ffi"

if [[ $(find "${FFI_DIR}" -name "${FFI_VERSION}*") ]]; then
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type Ffi, FfiLogLevelFilter } from './types';

const bindings = require('node-gyp-build') as (dir?: string) => Ffi;

export const PACT_FFI_VERSION = '0.5.12';
export const PACT_FFI_VERSION = '0.5.13-or.1';

/**
* Returns the library path which is located inside `node_modules`
Expand Down
175 changes: 175 additions & 0 deletions test/or-matcher.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import axios from 'axios';
import { type ConsumerPact, makeConsumerPact } from '../src';
import { FfiSpecificationVersion } from '../src/ffi/types';

const HOST = '127.0.0.1';
const CONSUMER = 'or-matcher-consumer';
const PROVIDER = 'or-matcher-provider';
const OUTPUT_DIR = path.join(__dirname, '__testoutput__');
const PACT_FILE = path.join(OUTPUT_DIR, `${CONSUMER}-${PROVIDER}.json`);
const UUID = 'e2490de5-5bd3-43d5-b7c4-526e33f71304';
const UUID_REGEX =
'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$';

// Integration JSON with `pact:matcher:combine`, where `pact:matcher:type`
// holds the list of matchers to combine. Requires a version of the Pact FFI
// that supports it (pact-foundation/pact-reference#601).
const anyOf = (value: unknown, ...matchers: Record<string, unknown>[]) => ({
'pact:matcher:type': matchers,
'pact:matcher:combine': 'OR',
value,
});
const digitsOrUuid = anyOf(
'42',
{ 'pact:matcher:type': 'regex', regex: '^\\d+$' },
{ 'pact:matcher:type': 'regex', regex: UUID_REGEX },
);
const stringOrNull = anyOf(
'Fred',
{ 'pact:matcher:type': 'type' },
{ 'pact:matcher:type': 'null' },
);
const addressOrNull = anyOf(
{
street: 'Main St',
number: { 'pact:matcher:type': 'integer', value: 42 },
},
{ 'pact:matcher:type': 'type' },
{ 'pact:matcher:type': 'null' },
);

describe('FFI integration test for matchers combined with OR', () => {
let pact: ConsumerPact;
let port: number;

type RequestOptions = {
name?: unknown;
address?: unknown;
id?: string;
requestId?: string;
};

const sendRequest = ({
name = 'Fred',
address = { street: 'Main St', number: 42 },
id = '42',
requestId = '42',
}: RequestOptions = {}) =>
axios.request({
baseURL: `http://${HOST}:${port}`,
method: 'POST',
url: '/users',
params: { id },
headers: {
'content-type': 'application/json',
'x-request-id': requestId,
},
data: { name, address },
validateStatus: () => true,
});

beforeAll(() => {
if (fs.existsSync(PACT_FILE)) {
fs.unlinkSync(PACT_FILE);
}
});

beforeEach(() => {
pact = makeConsumerPact(
CONSUMER,
PROVIDER,
FfiSpecificationVersion.SPECIFICATION_VERSION_V4,
);
const interaction = pact.newInteraction('');
interaction.uponReceiving(
'a request to create a user with optional fields',
);
interaction.withRequest('POST', '/users');
interaction.withQuery('id', 0, JSON.stringify(digitsOrUuid));
interaction.withRequestHeader(
'x-request-id',
0,
JSON.stringify(digitsOrUuid),
);
interaction.withRequestBody(
JSON.stringify({ name: stringOrNull, address: addressOrNull }),
'application/json',
);
interaction.withStatus(201);
interaction.withResponseBody(
JSON.stringify({ name: stringOrNull }),
'application/json',
);
port = pact.createMockServer(HOST);
});

afterEach(() => {
pact.cleanupMockServer(port);
});

const expectMatch = async (options: RequestOptions) => {
const res = await sendRequest(options);
expect(pact.mockServerMismatches(port)).toHaveLength(0);
expect(res.status).toEqual(201);
expect(pact.mockServerMatchedSuccessfully(port)).toBe(true);
return res;
};

const expectMismatch = async (options: RequestOptions) => {
const res = await sendRequest(options);
expect(res.status).not.toEqual(201);
expect(pact.mockServerMatchedSuccessfully(port)).toBe(false);
};

it('matches the example values, and writes the rules combined with OR', async () => {
const res = await expectMatch({});
expect(res.data).toEqual({ name: 'Fred' });

pact.writePactFile(OUTPUT_DIR);
const written = JSON.parse(fs.readFileSync(PACT_FILE, 'utf8'));
const { request, response } = written.interactions[0];
const orDigitsOrUuid = {
combine: 'OR',
matchers: [
{ match: 'regex', regex: '^\\d+$' },
{ match: 'regex', regex: UUID_REGEX },
],
};
const orTypeOrNull = {
combine: 'OR',
matchers: [{ match: 'type' }, { match: 'null' }],
};

expect(request.matchingRules.query.id).toEqual(orDigitsOrUuid);
expect(request.matchingRules.header['x-request-id']).toEqual(
orDigitsOrUuid,
);
expect(request.matchingRules.body['$.name']).toEqual(orTypeOrNull);
expect(request.matchingRules.body['$.address']).toEqual(orTypeOrNull);
expect(request.matchingRules.body['$.address.number']).toEqual({
combine: 'AND',
matchers: [{ match: 'integer' }],
});
expect(response.matchingRules.body['$.name']).toEqual(orTypeOrNull);
});

it('matches the other alternatives', () =>
expectMatch({ name: null, address: null, id: UUID, requestId: UUID }));

it('applies the matchers nested in the example of an optional object', () =>
expectMatch({ address: { street: 'Other St', number: 7 } }));

it('fails when a nested matcher does not match', () =>
expectMismatch({ address: { street: 'Main St', number: '42' } }));

it('fails when the body value matches none of the alternatives', () =>
expectMismatch({ name: 123 }));

it('fails when a header matches none of the alternatives', () =>
expectMismatch({ requestId: 'abc' }));

it('fails when a query parameter matches none of the alternatives', () =>
expectMismatch({ id: 'abc' }));
});
Loading