Skip to content

Commit

Permalink
Merge pull request #46 from kikoruiz/feat/add-request-query
Browse files Browse the repository at this point in the history
feat: add query to request in the pact file
  • Loading branch information
YOU54F authored Jun 1, 2022
2 parents 0ffc6fe + 864d9f4 commit 34d7268
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 32 deletions.
3 changes: 2 additions & 1 deletion examples/react/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export class API {
.then(r => r.data);
}

async getProduct(id) {
async getProduct(id, params) {
return axios.get(this.withPath("/product/" + id), {
params,
headers: {
"Authorization": this.generateAuthToken()
}
Expand Down
61 changes: 59 additions & 2 deletions src/convertMswMatchToPact.msw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@ import { convertMswMatchToPact } from "./convertMswMatchToPact";
import { MswMatch, PactFile } from "./pactMswAdapter";
import { Headers } from 'headers-utils';

const generatedPact:PactFile = {
const generatedPact: PactFile = {
consumer: { name: "interaction.consumer.name" },
provider: { name: "interaction.provider.name" },
interactions: [
{
description: "de5eefb0-c451-4ae2-9695-e02626f00ca7",
providerState: "",
request: {
method: "GET",
path: "/products",
body: undefined,
headers: {
accept: "application/json, text/plain, */*",
authorization: "Bearer 2022-03-01T19:36:18.277Z",
}
},
response: {
status: 200,
body: [{ id: "09", type: "CREDIT_CARD", name: "Gem Visa" }],
headers: { "x-powered-by": "msw", "content-type": "application/json" },
},
},
{
description: "073d6de0-e1ac-11ec-8fea-0242ac120002",
providerState: "",
request: {
method: "GET",
path: "/products",
Expand All @@ -17,6 +35,7 @@ const generatedPact:PactFile = {
accept: "application/json, text/plain, */*",
authorization: "Bearer 2022-03-01T19:36:18.277Z",
},
query: 'sort=asc'
},
response: {
status: 200,
Expand Down Expand Up @@ -66,8 +85,47 @@ const sampleMatch: MswMatch[] = [
])
},
body: JSON.stringify([{ id: '09', type: 'CREDIT_CARD', name: 'Gem Visa' }])
},
{
request: {
id: '073d6de0-e1ac-11ec-8fea-0242ac120002',
url: new URL('http://localhost:8081/products?sort=asc'),
method: 'GET',
body: undefined,
headers: new Headers({
accept: 'application/json, text/plain, */*',
authorization: 'Bearer 2022-03-01T19:36:18.277Z',
'user-agent': 'axios/0.21.1',
host: 'localhost:8081',
'content-type': 'application/json'
}),
cookies: {},
redirect: 'manual',
referrer: '',
keepalive: false,
cache: 'default',
mode: 'cors',
referrerPolicy: 'no-referrer',
integrity: '',
destination: 'document',
bodyUsed: false,
credentials: 'same-origin'
},
response: {
status: 200,
statusText: 'OK',
headers: new Headers({
'x-powered-by': 'msw',
'content-type': 'application/json'
}),
body: JSON.stringify([
{ id: '09', type: 'CREDIT_CARD', name: 'Gem Visa' }
])
},
body: JSON.stringify([{ id: '09', type: 'CREDIT_CARD', name: 'Gem Visa' }])
}
];

describe("writes an msw req/res to a pact", () => {
it("should convert an msw server match to a pact", async () => {
expect(
Expand All @@ -79,4 +137,3 @@ describe("writes an msw req/res to a pact", () => {
).toMatchObject(generatedPact);
});
});

58 changes: 29 additions & 29 deletions src/convertMswMatchToPact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PactFile, MswMatch } from "./pactMswAdapter";
import { omit } from "lodash";
export const convertMswMatchToPact = ({
export const convertMswMatchToPact = ({
consumer,
provider,
matches,
Expand All @@ -14,39 +14,39 @@ export const convertMswMatchToPact = ({
const pactFile: PactFile = {
consumer: { name: consumer },
provider: { name: provider },
interactions: matches.map( (match) => {
return {
description: match.request.id,
providerState: '',
request: {
method: match.request.method,
path: match.request.url.pathname,
headers: headers?.excludeHeaders
? omit(match.request.headers['_headers'], headers.excludeHeaders)
: match.request.headers['_headers'],
body: match.request.body || undefined
},
response: {
status: match.response.status,
headers: headers?.excludeHeaders
? omit(
Object.fromEntries(match.response.headers.entries()),
headers.excludeHeaders
)
: Object.fromEntries(match.response.headers.entries()),
body: match.body
? match.response.headers.get('content-type')?.includes('json')
? JSON.parse(match.body)
: match.body
: undefined
}
};
}),
interactions: matches.map(match => ({
description: match.request.id,
providerState: '',
request: {
method: match.request.method,
path: match.request.url.pathname,
headers: headers?.excludeHeaders
? omit(match.request.headers['_headers'], headers.excludeHeaders)
: match.request.headers['_headers'],
body: match.request.body || undefined,
query: match.request.url.search ? match.request.url.search.split('?')[1] : undefined
},
response: {
status: match.response.status,
headers: headers?.excludeHeaders
? omit(
Object.fromEntries(match.response.headers.entries()),
headers.excludeHeaders
)
: Object.fromEntries(match.response.headers.entries()),
body: match.body
? match.response.headers.get('content-type')?.includes('json')
? JSON.parse(match.body)
: match.body
: undefined
}
})),
metadata: {
pactSpecification: {
version: "2.0.0",
},
},
};

return pactFile;
};
23 changes: 23 additions & 0 deletions src/pactFromMswServer.msw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@ describe("API - With MSW mock generating a pact", () => {
expect(respProduct).toEqual(product);
});

test("get product ID 10 with visibility hidden", async () => {
const product = {
id: "10",
type: "CREDIT_CARD",
name: "28 Degrees"
};
const hiddenVisibilityProduct = {
...product,
visibility: "hidden"
};
server.use(
rest.get(API.url + "/product/10", (req, res, ctx) => {
const visibility = req.url.searchParams.get('visibility');
const response = visibility === 'hidden' ? hiddenVisibilityProduct : product;

return res(ctx.status(200), ctx.json(response));
})
);

const respProduct = await API.getProduct("10", {visibility: "hidden"});
expect(respProduct).toEqual(hiddenVisibilityProduct);
});

test("unhandled route", async () => {
await expect(API.getProduct("11")).rejects.toThrow(
/^connect ECONNREFUSED (127.0.0.1|::1):8081$/
Expand Down
1 change: 1 addition & 0 deletions src/pactMswAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ export interface PactInteraction {
path: string;
headers: any;
body: DefaultRequestBody;
query?: string;
};
response: {
status: number;
Expand Down

0 comments on commit 34d7268

Please sign in to comment.