diff --git a/src/pact.integration.spec.ts b/src/pact.integration.spec.ts index 34792e8a8..86f7a2918 100644 --- a/src/pact.integration.spec.ts +++ b/src/pact.integration.spec.ts @@ -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; + matchingRules: Record>; + }; + + 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', () => { diff --git a/src/v4/http/requestBuilder.spec.ts b/src/v4/http/requestBuilder.spec.ts index 614af14b3..054fa336a 100644 --- a/src/v4/http/requestBuilder.spec.ts +++ b/src/v4/http/requestBuilder.spec.ts @@ -1,16 +1,22 @@ 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; + let withRequestHeader: ReturnType; 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); }); @@ -18,6 +24,54 @@ describe('V4 RequestBuilder', () => { 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) => { diff --git a/src/v4/http/responseBuilder.spec.ts b/src/v4/http/responseBuilder.spec.ts index 0b931f1ee..23374eb08 100644 --- a/src/v4/http/responseBuilder.spec.ts +++ b/src/v4/http/responseBuilder.spec.ts @@ -1,16 +1,22 @@ 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; + let withResponseHeader: ReturnType; 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); }); @@ -18,6 +24,54 @@ describe('V4 ResponseBuilder', () => { 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) => { diff --git a/src/v4/http/responseBuilder.ts b/src/v4/http/responseBuilder.ts index 91ae097bd..2f79a0f8e 100644 --- a/src/v4/http/responseBuilder.ts +++ b/src/v4/http/responseBuilder.ts @@ -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; @@ -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, index: number) => { + this.interaction.withResponseHeader( + `${k}`, + index, + matcherValueOrString(header), + ); + }, + ); + } else { + this.interaction.withResponseHeader(`${k}`, 0, matcherValueOrString(v)); + } }, headers); return this;