diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 6618b070..2c536718 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -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 }} @@ -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 }} diff --git a/script/lib/download-ffi.sh b/script/lib/download-ffi.sh index ce2f6702..ae015eee 100755 --- a/script/lib/download-ffi.sh +++ b/script/lib/download-ffi.sh @@ -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 diff --git a/src/ffi/index.ts b/src/ffi/index.ts index 2c89fe2b..ee69331c 100644 --- a/src/ffi/index.ts +++ b/src/ffi/index.ts @@ -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` diff --git a/test/or-matcher.integration.spec.ts b/test/or-matcher.integration.spec.ts new file mode 100644 index 00000000..b70d9345 --- /dev/null +++ b/test/or-matcher.integration.spec.ts @@ -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[]) => ({ + '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' })); +});