Skip to content

Commit f0a2c74

Browse files
committed
generate pact contract on consumer side (nextjs)
1 parent 49f67bf commit f0a2c74

File tree

6 files changed

+1313
-0
lines changed

6 files changed

+1313
-0
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devDependencies": {
3+
"@pact-foundation/pact": "13.2.0"
4+
}
5+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"consumer": {
3+
"name": "NextJS-ShortenerApp"
4+
},
5+
"interactions": [
6+
{
7+
"description": "a POST request to shorten a url",
8+
"providerState": "provider can shorten urls",
9+
"request": {
10+
"body": {
11+
"url": "https://example.com/page1/page2/page3"
12+
},
13+
"headers": {
14+
"Content-Type": "application/json"
15+
},
16+
"matchingRules": {
17+
"$.body.url": {
18+
"match": "type"
19+
}
20+
},
21+
"method": "POST",
22+
"path": "/shorten-url"
23+
},
24+
"response": {
25+
"body": {
26+
"shortenedUrl": "http://localhost:1234/abc123"
27+
},
28+
"headers": {
29+
"Content-Type": "application/json"
30+
},
31+
"matchingRules": {
32+
"$.body.shortenedUrl": {
33+
"match": "type"
34+
}
35+
},
36+
"status": 200
37+
}
38+
}
39+
],
40+
"metadata": {
41+
"pact-js": {
42+
"version": "13.2.0"
43+
},
44+
"pactRust": {
45+
"ffi": "0.4.22",
46+
"models": "1.2.3"
47+
},
48+
"pactSpecification": {
49+
"version": "2.0.0"
50+
}
51+
},
52+
"provider": {
53+
"name": "NestJS-ShortenerAPI"
54+
}
55+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import path from 'path';
2+
import { Pact, Matchers } from '@pact-foundation/pact';
3+
import fetch from 'node-fetch';
4+
5+
const { like } = Matchers;
6+
7+
const provider = new Pact({
8+
consumer: 'NextJS-ShortenerApp',
9+
provider: 'NestJS-ShortenerAPI',
10+
port: 1234,
11+
log: path.resolve(__dirname, '../pact/logs', 'pact.log'),
12+
dir: path.resolve(__dirname, '../pact/pacts'),
13+
logLevel: 'info',
14+
});
15+
16+
describe('Pact with NestJS shorten-url provider', () => {
17+
beforeAll(async () => await provider.setup());
18+
afterAll(async () => await provider.finalize());
19+
afterEach(async () => await provider.verify());
20+
21+
describe('POST /shorten-url', () => {
22+
beforeEach(() => {
23+
return provider.addInteraction({
24+
state: 'provider can shorten urls',
25+
uponReceiving: 'a POST request to shorten a url',
26+
withRequest: {
27+
method: 'POST',
28+
path: '/shorten-url',
29+
headers: {
30+
'Content-Type': 'application/json',
31+
},
32+
body: {
33+
url: like('https://example.com/page1/page2/page3'),
34+
},
35+
},
36+
willRespondWith: {
37+
status: 200,
38+
headers: {
39+
'Content-Type': 'application/json',
40+
},
41+
body: {
42+
shortenedUrl: like('http://localhost:1234/abc123'),
43+
},
44+
},
45+
});
46+
});
47+
48+
it('sends POST /shorten-url and receives shortened URL', async () => {
49+
const baseUrl = provider.mockService.baseUrl;
50+
const response = await fetch(`${baseUrl}/shorten-url`, {
51+
method: 'POST',
52+
headers: {
53+
'Content-Type': 'application/json',
54+
},
55+
body: JSON.stringify({ url: 'https://example.com/page1/page2/page3' }),
56+
});
57+
58+
const body = await response.json();
59+
60+
expect(response.status).toBe(200);
61+
expect(body).toEqual({
62+
shortenedUrl: 'http://localhost:1234/abc123',
63+
});
64+
});
65+
});
66+
});

webapp/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@testing-library/react": "14.1.2",
2727
"@types/jest": "29.5.11",
2828
"@types/node": "20",
29+
"@types/node-fetch": "2",
2930
"@types/react": "19",
3031
"@types/react-dom": "19",
3132
"@typescript-eslint/eslint-plugin": "8.18.0",
@@ -37,6 +38,7 @@
3738
"eslint-plugin-react": "7.37.2",
3839
"jest": "29.7.0",
3940
"jest-environment-jsdom": "29.7.0",
41+
"node-fetch": "2",
4042
"prettier": "3.4.2",
4143
"ts-jest": "29.2.5",
4244
"ts-node": "10.9.2",

webapp/yarn.lock

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,14 @@
12311231
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
12321232
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==
12331233

1234+
"@types/node-fetch@2":
1235+
version "2.6.12"
1236+
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.12.tgz#8ab5c3ef8330f13100a7479e2cd56d3386830a03"
1237+
integrity sha512-8nneRWKCg3rMtF69nLQJnOYUcbafYeFSjqkw3jCRLsqkWFlHaoQrr5mXmofFGOx3DKn7UfmBMyov8ySvLRVldA==
1238+
dependencies:
1239+
"@types/node" "*"
1240+
form-data "^4.0.0"
1241+
12341242
"@types/node@*":
12351243
version "22.10.2"
12361244
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.10.2.tgz#a485426e6d1fdafc7b0d4c7b24e2c78182ddabb9"
@@ -4024,6 +4032,13 @@ [email protected]:
40244032
"@next/swc-win32-x64-msvc" "15.1.0"
40254033
sharp "^0.33.5"
40264034

4035+
node-fetch@2:
4036+
version "2.7.0"
4037+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
4038+
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
4039+
dependencies:
4040+
whatwg-url "^5.0.0"
4041+
40274042
node-int64@^0.4.0:
40284043
version "0.4.0"
40294044
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
@@ -4925,6 +4940,11 @@ tr46@^3.0.0:
49254940
dependencies:
49264941
punycode "^2.1.1"
49274942

4943+
tr46@~0.0.3:
4944+
version "0.0.3"
4945+
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
4946+
integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==
4947+
49284948
ts-api-utils@^1.3.0:
49294949
version "1.4.3"
49304950
resolved "https://registry.yarnpkg.com/ts-api-utils/-/ts-api-utils-1.4.3.tgz#bfc2215fe6528fecab2b0fba570a2e8a4263b064"
@@ -5122,6 +5142,11 @@ walker@^1.0.8:
51225142
dependencies:
51235143
makeerror "1.0.12"
51245144

5145+
webidl-conversions@^3.0.0:
5146+
version "3.0.1"
5147+
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
5148+
integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==
5149+
51255150
webidl-conversions@^7.0.0:
51265151
version "7.0.0"
51275152
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-7.0.0.tgz#256b4e1882be7debbf01d05f0aa2039778ea080a"
@@ -5147,6 +5172,14 @@ whatwg-url@^11.0.0:
51475172
tr46 "^3.0.0"
51485173
webidl-conversions "^7.0.0"
51495174

5175+
whatwg-url@^5.0.0:
5176+
version "5.0.0"
5177+
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
5178+
integrity sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==
5179+
dependencies:
5180+
tr46 "~0.0.3"
5181+
webidl-conversions "^3.0.0"
5182+
51505183
which-boxed-primitive@^1.0.2, which-boxed-primitive@^1.1.0:
51515184
version "1.1.0"
51525185
resolved "https://registry.yarnpkg.com/which-boxed-primitive/-/which-boxed-primitive-1.1.0.tgz#2d850d6c4ac37b95441a67890e19f3fda8b6c6d9"

0 commit comments

Comments
 (0)