Skip to content

Commit

Permalink
feat: add a status matcher to the V4 interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Feb 27, 2024
1 parent 1c4a4b8 commit 4e0b7fb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
34 changes: 16 additions & 18 deletions src/v3/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,24 @@ export const eachValueMatches = <T>(
'pact:matcher:type': 'eachValue',
rules: Array.isArray(matchers) ? matchers : [matchers],
value: example,
// Unsure if the full object is provided, or just a template k/v pair
// value: {
// [keyTemplate]: template,
// },
});

/**
* Matches HTTP status codes by their range description, or by a list of specific codes.
*
* @param example Example status code to use
* @param range Allowed status codes
*/
export const matchStatus = (
example: number,
range: HTTPResponseStatusClass | number[]
): StatusCodeMatcher<number> => ({
value: example,
'pact:matcher:type': 'statusCode',
status: range,
});
// Waiting on https://github.com/pact-foundation/pact-reference/issues/296

// /**
// * Matches HTTP status codes by their range description, or by a list of specific codes.
// *
// * @param example Example status code to use
// * @param range Allowed status codes
// */
// export const matchStatus = (
// example: number,
// range: HTTPResponseStatusClass | number[]
// ): StatusCodeMatcher<number> => ({
// value: example,
// 'pact:matcher:type': 'statusCode',
// status: range,
// });

/**
* Array where each element must match the given template
Expand Down
10 changes: 10 additions & 0 deletions src/v3/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ export enum HTTPResponseStatusClass {
ClientError = 'clientError',
// Server errors (500–599)
ServerError = 'serverError',
// Informational responses (100–199)
'1XX' = 'information',
// Successful responses (200–299)
'2XX' = 'success',
// Redirects (300–399)
'3XX' = 'redirect',
// Client errors (400–499)
'4XX' = 'clientError',
// Server errors (500–599)
'5XX' = 'serverError',
// Non-error response(< 400)
NonError = 'nonError',
// Any error response (>= 400)
Expand Down
16 changes: 12 additions & 4 deletions src/v4/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { ConsumerInteraction, ConsumerPact } from '@pact-foundation/pact-core';
import { JsonMap } from '../../common/jsonTypes';
import { forEachObjIndexed } from 'ramda';
import { Path, TemplateHeaders, TemplateQuery, V3MockServer } from '../../v3';
import { Matcher, matcherValueOrString } from '../../v3/matchers';
import {
Matcher,
StatusCodeMatcher,
matcherValueOrString,
reify,
} from '../../v3/matchers';
import {
PactV4Options,
PluginConfig,
Expand Down Expand Up @@ -126,8 +131,11 @@ export class InteractionwithRequest implements V4InteractionwithRequest {
protected cleanupFn: () => void
) {}

willRespondWith(status: number, builder?: V4ResponseBuilderFunc) {
this.interaction.withStatus(status);
willRespondWith(
status: number | StatusCodeMatcher<number>,
builder?: V4ResponseBuilderFunc
) {
this.interaction.withStatus(reify(status) as number);

if (typeof builder === 'function') {
builder(new ResponseBuilder(this.interaction));
Expand Down Expand Up @@ -309,7 +317,7 @@ export class InteractionWithPluginRequest
status: number,
builder?: V4PluginResponseBuilderFunc
): V4InteractionWithPluginResponse {
this.interaction.withStatus(status);
this.interaction.withStatus(reify(status) as number);

if (typeof builder === 'function') {
builder(new ResponseWithPluginBuilder(this.interaction));
Expand Down
6 changes: 4 additions & 2 deletions src/v4/http/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { JsonMap } from '../../common/jsonTypes';
import {
Matcher,
Path,
SpecificationVersion,
StatusCodeMatcher,
TemplateHeaders,
TemplateQuery,
V3MockServer,
Expand Down Expand Up @@ -71,7 +73,7 @@ export interface V4InteractionWithCompleteRequest {

export interface V4InteractionwithRequest {
willRespondWith(
status: number,
status: number | StatusCodeMatcher<number>,
builder?: V4ResponseBuilderFunc
): V4InteractionWithResponse;
}
Expand Down Expand Up @@ -137,7 +139,7 @@ export interface V4InteractionWithPlugin {

export interface V4InteractionWithPluginRequest {
willRespondWith(
status: number,
status: number | StatusCodeMatcher<number>,
builder?: V4PluginResponseBuilderFunc
): V4InteractionWithPluginResponse;
}
Expand Down

0 comments on commit 4e0b7fb

Please sign in to comment.