Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 17 additions & 1 deletion src/__tests__/fixtures/pact-formats/results.json
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
[]
[
{
"code": "interaction.type.unsupported",
"message": "Non-HTTP Interaction is not supported, OPC can only compare HTTP interactions.",
"type": "warning"
},
{
"code": "interaction.type.unsupported",
"message": "Non-HTTP Interaction is not supported, OPC can only compare HTTP interactions.",
"type": "warning"
},
{
"code": "interaction.type.unsupported",
"message": "Non-HTTP Interaction is not supported, OPC can only compare HTTP interactions.",
"type": "warning"
}
]
28 changes: 28 additions & 0 deletions src/__tests__/fixtures/pact-non-http-interactions-v3/oas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: 3.0.0
info:
title: OAS
version: 1.0.0

paths:
/login:
post:
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
email:
type: string
password:
type: string
required:
- email
- password
responses:
"201":
description: description
content:
"application/json":
schema: {}
79 changes: 79 additions & 0 deletions src/__tests__/fixtures/pact-non-http-interactions-v3/pact.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"consumer": {
"name": "consumer"
},
"messages": [
{
"_id": "e7918bd59533f7fe75b86192865f89898df6cd3f",
"contents": {
"event": "CREATED",
"id": "5cc989d0-d800-434c-b4bb-b1268499e850",
"name": "product name",
"type": "product series",
"version": "v1"
},
"description": "a product created event",
"matchingRules": {
"body": {
"$.event": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "^(CREATED|UPDATED|DELETED)$"
}
]
},
"$.id": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"$.name": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"$.type": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"$.version": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "v[a-zA-z0-9]+"
}
]
}
}
},
"metaData": {
"contentType": "application/json",
"kafka_topic": "products"
}
}
],
"metadata": {
"pact-jvm": {
"version": "4.6.17"
},
"pactSpecification": {
"version": "3.0.0"
}
},
"provider": {
"name": "provider"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"code": "interaction.type.unsupported",
"message": "Non-HTTP Interaction is not supported, OPC can only compare HTTP interactions.",
"type": "warning"
}
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"code": "interaction.type.unsupported",
"message": "Non-HTTP Interaction is not supported, OPC can only compare HTTP interactions.",
"type": "warning"
},
{
"code": "request.body.incompatible",
"message": "Request body is incompatible with the request body schema in the spec file: must be object",
Expand Down
20 changes: 18 additions & 2 deletions src/compare/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ export class Comparator {

const parsedPact = parsePact(pact);

for (const [index, interaction] of parsedPact.interactions.entries()) {
const parsedInteractions = parsedPact.interactions || [];

for (const [index, interaction] of parsedInteractions.entries()) {
if (interaction._skip) {
// non http/synchronous have been zero-ed out
yield {
code: "interaction.type.unsupported",
message: `Non-HTTP Interaction is not supported, OPC can only compare HTTP interactions.`,
type: "warning",
};
continue;
}

Expand Down Expand Up @@ -172,5 +178,15 @@ export class Comparator {
this.#config,
);
}

if (parsedPact.messages) {
for (const [_message] of parsedPact.messages.entries()) {
yield {
code: "interaction.type.unsupported",
message: `Non-HTTP Interaction is not supported, OPC can only compare HTTP interactions.`,
type: "warning",
};
}
}
}
}
54 changes: 39 additions & 15 deletions src/documents/pact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ describe("#parser", () => {
],
} as Pact;

const { request, response } = parse(json).interactions[0];
const { request, response } = parse(json).interactions![0];
expect(request.headers!["Content-Type"]).toEqual("text/json");
expect(request.headers!["Accept"]).toEqual(
"text/plain,application/json,text/json",
);
expect(response.headers!["Content-Type"]).toEqual("application/json");
});

it("filters out non-HTTP interactions", () => {
it("marks out non-HTTP interactions from v4 pact as skip `true`", () => {
const request = {
method: "GET",
path: "/path",
Expand Down Expand Up @@ -66,11 +66,35 @@ describe("#parser", () => {
};

const pact = parse(json as Pact);
expect(pact.interactions.length).toBe(4);
expect(pact.interactions[0].description).toBe("no-type");
expect(pact.interactions[1].description).toBe("http");
expect(pact.interactions[2]._skip).toBe(true);
expect(pact.interactions[3]._skip).toBe(true);
expect(pact.interactions!.length).toBe(4);
expect(pact.interactions![0].description).toBe("no-type");
expect(pact.interactions![1].description).toBe("http");
expect(pact.interactions![2]._skip).toBe(true);
expect(pact.interactions![3]._skip).toBe(true);
});

it("if messages are present it parses it to be dealt with later", () => {
const request = {
method: "GET",
path: "/path",
};
const response = {
status: 200,
};
const json = {
interactions: [{ description: "no-type", request, response }],
messages: [
{
content: "some-message-content",
},
],
};

const pact = parse(json as Pact);
expect(pact.interactions!.length).toBe(1);
expect(pact.interactions![0].description).toBe("no-type");
expect(pact.messages!.length).toBe(1);
expect(pact.messages![0]).toEqual({ content: "some-message-content" });
});

it("should parse V4 body types", () => {
Expand Down Expand Up @@ -139,13 +163,13 @@ describe("#parser", () => {

const pact = parse(json as Pact);

expect(pact.interactions[0].response.body).toEqual({ hello: "world" });
expect(pact.interactions[1].response.body).toEqual({ hello: "world" });
expect(pact.interactions[2].response.body).toEqual("hello world");
expect(pact.interactions[3].request.body).toEqual({ hello: "world" });
expect(pact.interactions[4].request.body).toEqual({ hello: "world" });
expect(pact.interactions[5].request.body).toEqual("hello world");
expect(pact.interactions[6].request.body).toEqual("{ not: json }");
expect(pact.interactions[7].request.body).toEqual("abcdef");
expect(pact.interactions![0].response.body).toEqual({ hello: "world" });
expect(pact.interactions![1].response.body).toEqual({ hello: "world" });
expect(pact.interactions![2].response.body).toEqual("hello world");
expect(pact.interactions![3].request.body).toEqual({ hello: "world" });
expect(pact.interactions![4].request.body).toEqual({ hello: "world" });
expect(pact.interactions![5].request.body).toEqual("hello world");
expect(pact.interactions![6].request.body).toEqual("{ not: json }");
expect(pact.interactions![7].request.body).toEqual("abcdef");
});
});
11 changes: 7 additions & 4 deletions src/documents/pact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export const Pact = Type.Object({
),
}),
),
interactions: Type.Array(Interaction),
interactions: Type.Optional(Type.Array(Interaction)),
messages: Type.Optional(Type.Array(Type.Unknown())),
});

export type Pact = Static<typeof Pact>;
Expand Down Expand Up @@ -154,11 +155,12 @@ const ajv = new Ajv();
const validate = ajv.compile(Pact);

export const parse = (pact: Pact): Pact => {
const { metadata, interactions } = pact;
const { metadata, interactions, messages } = pact;

const isValid = validate({
metadata,
interactions: interactions.filter(supportedInteractions),
interactions: interactions?.filter(supportedInteractions),
messages,
});
if (!isValid) {
throw new ParserError(validate.errors!);
Expand All @@ -175,11 +177,12 @@ export const parse = (pact: Pact): Pact => {

return {
metadata,
interactions: interactions.map((i: Interaction) =>
interactions: interactions?.map((i: Interaction) =>
supportedInteractions(i)
? interactionParser(i)
: ({ _skip: true } as Interaction),
),
messages,
};
};

Expand Down
3 changes: 2 additions & 1 deletion src/results/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type WarningCode =
| "request.query.unknown"
| "response.content-type.unknown"
| "response.header.undefined"
| "response.status.default";
| "response.status.default"
| "interaction.type.unsupported";

export interface Result {
code: ErrorCode | WarningCode;
Expand Down