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
31 changes: 31 additions & 0 deletions src/pact.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,37 @@ describe('V4 Pact', () => {
foo: 'bar',
}),
));

it('records each value of a repeated response header', async () => {
const description = 'a response with a repeated header';

await pact
.addInteraction()
.uponReceiving(description)
.withRequest('GET', '/repeated-header')
.willRespondWith(200, (builder) => {
builder.headers({
'X-Repeated': ['first', MatchersV3.regex(/^second$/, 'second')],
});
})
.executeTest(async (server) => {
const response = await axios.get(`${server.url}/repeated-header`);
expect(response.headers['x-repeated']).toContain('first');
expect(response.headers['x-repeated']).toContain('second');
});

const interaction = interactionByDescription(description);
const response = interaction.response as {
headers: Record<string, string[]>;
matchingRules: Record<string, Record<string, unknown>>;
};

expect(response.headers['X-Repeated']).toEqual(['first', 'second']);
expect(response.matchingRules.header['X-Repeated']).toEqual({
combine: 'AND',
matchers: [{ match: 'regex', regex: '^second$' }],
});
});
});

describe('Asynchronous message contract', () => {
Expand Down
56 changes: 55 additions & 1 deletion src/v4/http/requestBuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,77 @@
import type { ConsumerInteraction } from '@pact-foundation/pact-core';
import { vi } from 'vitest';
import { regex } from '../../v3/matchers';
import { XmlBuilder } from '../../v4';
import { RequestBuilder } from './requestBuilder';

describe('V4 RequestBuilder', () => {
let withRequestBody: ReturnType<typeof vi.fn>;
let withRequestHeader: ReturnType<typeof vi.fn>;
let interaction: ConsumerInteraction;
let builder: RequestBuilder;

beforeEach(() => {
withRequestBody = vi.fn();
interaction = { withRequestBody } as unknown as ConsumerInteraction;
withRequestHeader = vi.fn();
interaction = {
withRequestBody,
withRequestHeader,
} as unknown as ConsumerInteraction;
builder = new RequestBuilder(interaction);
});

afterEach(() => {
vi.restoreAllMocks();
});

describe('#headers', () => {
it('registers a single header value at index 0', () => {
builder.headers({ 'Content-Type': 'application/json' });

expect(withRequestHeader).toHaveBeenCalledOnce();
expect(withRequestHeader).toHaveBeenCalledWith(
'Content-Type',
0,
'application/json',
);
});

it('registers each value of a repeated header at its own index', () => {
builder.headers({ 'Set-Cookie': ['a=1', 'b=2'] });

expect(withRequestHeader).toHaveBeenCalledTimes(2);
expect(withRequestHeader).toHaveBeenNthCalledWith(
1,
'Set-Cookie',
0,
'a=1',
);
expect(withRequestHeader).toHaveBeenNthCalledWith(
2,
'Set-Cookie',
1,
'b=2',
);
});

it('serialises matchers within a repeated header', () => {
const matcher = regex('^b=\\d+$', 'b=2');

builder.headers({ 'Set-Cookie': ['a=1', matcher] });

expect(withRequestHeader).toHaveBeenNthCalledWith(
2,
'Set-Cookie',
1,
JSON.stringify(matcher),
);
});

it('returns the builder for chaining', () => {
expect(builder.headers({ 'X-Test': 'value' })).toBe(builder);
});
});

describe('#xmlBody', () => {
it('calls withRequestBody with application/xml content type', () => {
const body = new XmlBuilder('1.0', 'UTF-8', 'root').build((el) => {
Expand Down
56 changes: 55 additions & 1 deletion src/v4/http/responseBuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,77 @@
import type { ConsumerInteraction } from '@pact-foundation/pact-core';
import { vi } from 'vitest';
import { regex } from '../../v3/matchers';
import { XmlBuilder } from '../../v4';
import { ResponseBuilder } from './responseBuilder';

describe('V4 ResponseBuilder', () => {
let withResponseBody: ReturnType<typeof vi.fn>;
let withResponseHeader: ReturnType<typeof vi.fn>;
let interaction: ConsumerInteraction;
let builder: ResponseBuilder;

beforeEach(() => {
withResponseBody = vi.fn();
interaction = { withResponseBody } as unknown as ConsumerInteraction;
withResponseHeader = vi.fn();
interaction = {
withResponseBody,
withResponseHeader,
} as unknown as ConsumerInteraction;
builder = new ResponseBuilder(interaction);
});

afterEach(() => {
vi.restoreAllMocks();
});

describe('#headers', () => {
it('registers a single header value at index 0', () => {
builder.headers({ 'Content-Type': 'application/json' });

expect(withResponseHeader).toHaveBeenCalledOnce();
expect(withResponseHeader).toHaveBeenCalledWith(
'Content-Type',
0,
'application/json',
);
});

it('registers each value of a repeated header at its own index', () => {
builder.headers({ 'Set-Cookie': ['a=1', 'b=2'] });

expect(withResponseHeader).toHaveBeenCalledTimes(2);
expect(withResponseHeader).toHaveBeenNthCalledWith(
1,
'Set-Cookie',
0,
'a=1',
);
expect(withResponseHeader).toHaveBeenNthCalledWith(
2,
'Set-Cookie',
1,
'b=2',
);
});

it('serialises matchers within a repeated header', () => {
const matcher = regex('^b=\\d+$', 'b=2');

builder.headers({ 'Set-Cookie': ['a=1', matcher] });

expect(withResponseHeader).toHaveBeenNthCalledWith(
2,
'Set-Cookie',
1,
JSON.stringify(matcher),
);
});

it('returns the builder for chaining', () => {
expect(builder.headers({ 'X-Test': 'value' })).toBe(builder);
});
});

describe('#xmlBody', () => {
it('calls withResponseBody with application/xml content type', () => {
const body = new XmlBuilder('1.0', 'UTF-8', 'root').build((el) => {
Expand Down
18 changes: 15 additions & 3 deletions src/v4/http/responseBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ConsumerInteraction } from '@pact-foundation/pact-core';
import { forEachObjIndexed } from 'ramda';
import { convertRulesToFFI, validateRules } from '../../common/matchingRules';
import type { Rules, TemplateHeaders } from '../../v3';
import type { Matcher, Rules, TemplateHeaders } from '../../v3';
import { matcherValueOrString } from '../../v3/matchers';
import { readBinaryData } from '.';
import type { V4ResponseBuilder } from './types';
import type { TemplateHeaderArrayValue, V4ResponseBuilder } from './types';

export class ResponseBuilder implements V4ResponseBuilder {
protected interaction: ConsumerInteraction;
Expand All @@ -15,7 +15,19 @@ export class ResponseBuilder implements V4ResponseBuilder {

headers(headers: TemplateHeaders): V4ResponseBuilder {
forEachObjIndexed((v, k) => {
this.interaction.withResponseHeader(`${k}`, 0, matcherValueOrString(v));
if (Array.isArray(v)) {
(v as TemplateHeaderArrayValue).forEach(
(header: string | Matcher<string>, index: number) => {
this.interaction.withResponseHeader(
`${k}`,
index,
matcherValueOrString(header),
);
},
);
} else {
this.interaction.withResponseHeader(`${k}`, 0, matcherValueOrString(v));
}
}, headers);

return this;
Expand Down
Loading