Skip to content

Commit

Permalink
wip: provide repro shell for x.avidexchange bug report
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Oct 10, 2023
1 parent 9bb0d10 commit 07da292
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 53 deletions.
25 changes: 6 additions & 19 deletions examples/mocha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,15 @@ exports.getMeDogs = (endpoint) => {
const port = endpoint.port;

return axios.request({
method: 'GET',
method: 'POST',
baseURL: `${url}:${port}`,
url: '/dogs',
headers: {
Accept: [
'application/problem+json',
'application/json',
'text/plain',
'*/*',
],
Accept: 'application/x.avidxchange.accounting+json;version=1.0.0',
'Content-Type': 'application/x.avidxchange.accounting+json;version=1.0.0',
},
data: {
foo: 'bar',
},
});
};

exports.getMeDog = (endpoint) => {
const url = endpoint.url;
const port = endpoint.port;

return axios.request({
method: 'GET',
baseURL: `${url}:${port}`,
url: '/dogs/1',
headers: { Accept: 'application/json' },
});
};
61 changes: 27 additions & 34 deletions examples/mocha/test/get-dogs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@

const expect = require('chai').expect;
const path = require('path');
const { Pact, Matchers } = require('@pact-foundation/pact');
const { PactV3, Matchers } = require('@pact-foundation/pact');
const { getMeDogs, getMeDog } = require('../index');
const LOG_LEVEL = process.env.LOG_LEVEL || 'TRACE';

describe('The Dog API', () => {
let url = 'http://127.0.0.1';
const port = 8992;

const provider = new Pact({
port: port,
log: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),
const provider = new PactV3({
dir: path.resolve(process.cwd(), 'pacts'),
spec: 2,
consumer: 'MyConsumer',
provider: 'MyProvider',
logLevel: LOG_LEVEL,
host: '127.0.0.1',
});

const EXPECTED_BODY = [
Expand All @@ -29,51 +24,49 @@ describe('The Dog API', () => {
},
];

// Setup the provider
before(() => provider.setup());

// Write Pact when all tests done
after(() => provider.finalize());

// verify with Pact, and reset expectations
afterEach(() => provider.verify());

describe('get /dogs', () => {
before(() => {
const responseHeaders = {
'Content-Type': Matchers.regex({
generate:
'application/x.avidxchange.accounting+json;charset=utf-8;version=1.0.0',
matcher:
'application\\/x\\.avidxchange\\.accounting\\+json;charset=utf-8;version=([0-9]\\.?){3}',
}),
};
const interaction = {
state: 'i have a list of dogs',
uponReceiving: 'a request for all dogs',
withRequest: {
method: 'GET',
method: 'POST',
path: '/dogs',
headers: {
// Accept: 'application/problem+json, application/json, text/plain, */*', // <- fails, must use array syntax ❌
Accept: [
'application/problem+json',
'application/json',
'text/plain',
'*/*',
],
'Content-Type':
'application/x.avidxchange.accounting+json;version=1.0.0',
Accept: 'application/x.avidxchange.accounting+json;version=1.0.0',
},
body: {
foo: 'bar',
},
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json',
},
headers: responseHeaders,
body: EXPECTED_BODY,
},
};
return provider.addInteraction(interaction);
});

it('returns the correct response', async () => {
const urlAndPort = {
url: url,
port: port,
};
const response = await getMeDogs(urlAndPort);
expect(response.data).to.eql(EXPECTED_BODY);
return provider.executeTest(async (mockserver) => {
const urlAndPort = {
url: 'http://127.0.0.1',
port: mockserver.port,
};
const response = await getMeDogs(urlAndPort);
expect(response.data).to.eql(EXPECTED_BODY);
});
});
});
});

0 comments on commit 07da292

Please sign in to comment.