Skip to content

Commit a8b16cf

Browse files
author
Yoan THIRION
committed
Add js consumer
1 parent 1402741 commit a8b16cf

File tree

8 files changed

+136
-6
lines changed

8 files changed

+136
-6
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,6 @@ xcuserdata
112112

113113
# # Node
114114
node_modules
115-
pacts/
115+
pacts/
116+
package-lock.json
117+
node_modules

AgilePartner.Pact.Client2/AgilePartner.Pact.Client2.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
@@ -13,11 +13,11 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16-
<ProjectReference Include="..\AgilePartner.Pact.Lib\AgilePartner.Pact.Lib.csproj" />
16+
<Folder Include="pacts\" />
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<Folder Include="pacts\" />
20+
<ProjectReference Include="..\AgilePartner.Pact.Lib\AgilePartner.Pact.Lib.csproj" />
2121
</ItemGroup>
2222

2323
</Project>

AgilePartner.Pact.Client5/AgilePartner.Pact.Client5.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16-
<ProjectReference Include="..\AgilePartner.Pact.Lib\AgilePartner.Pact.Lib.csproj" />
16+
<Folder Include="pacts\" />
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<Folder Include="pacts\" />
20+
<ProjectReference Include="..\AgilePartner.Pact.Lib\AgilePartner.Pact.Lib.csproj" />
2121
</ItemGroup>
2222

2323
</Project>

AgilePartner.Pact.Providers.Tests/helloworld_api_should.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,11 @@ public void respect_its_contract_with_client5()
6161
{
6262
VerifyContractWithConsumer("Client5");
6363
}
64+
65+
[Fact]
66+
public void respect_its_contract_with_js_consumer()
67+
{
68+
VerifyContractWithConsumer("consumer-js");
69+
}
6470
}
6571
}

consumer-js/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict"
2+
3+
const axios = require("axios")
4+
5+
exports.helloworldv2 = endpoint => {
6+
const url = endpoint.url
7+
const port = endpoint.port
8+
9+
return axios.request({
10+
method: "GET",
11+
baseURL: `${url}:${port}`,
12+
url: "/api/v2.0/helloworld",
13+
headers: { Accept: "application/json; charset=utf-8" },
14+
})
15+
}

consumer-js/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "js-consumer",
3+
"version": "1.0.0",
4+
"description": "Mocha Pact example",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "rimraf pacts && mocha"
8+
},
9+
"license": "MIT",
10+
"devDependencies": {
11+
"@pact-foundation/pact": "^8.2.6",
12+
"axios": "^0.14.0",
13+
"chai": "^3.5.0",
14+
"mocha": "^5.1.1",
15+
"rimraf": "^2.6.2"
16+
}
17+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"use strict"
2+
3+
const expect = require("chai").expect;
4+
const path = require("path");
5+
const { Pact } = require('@pact-foundation/pact');
6+
const { helloworldv2 } = require("../index");
7+
8+
describe("The Helloworld API", () => {
9+
let url = "http://localhost"
10+
const port = 8992
11+
const contractPath = path.resolve(process.cwd(), "pacts");
12+
13+
const provider = new Pact({
14+
port: port,
15+
log: path.resolve(process.cwd(), "logs", "mockserver-integration.log"),
16+
dir: contractPath,
17+
spec: 2,
18+
consumer: "consumer-js",
19+
provider: "HelloWorld",
20+
pactfileWriteMode: "merge",
21+
publishPacts: true,
22+
publishVerificationResult: true
23+
})
24+
25+
const EXPECTED_BODY = {
26+
message: "Hello world v2"
27+
}
28+
29+
// Setup the provider
30+
before(() => provider.setup())
31+
32+
// Write Pact when all tests done
33+
after(() => provider.finalize())
34+
35+
// verify with Pact, and reset expectations
36+
afterEach(() => provider.verify())
37+
38+
describe("get /api/v2.0/helloworld", () => {
39+
before(done => {
40+
const interaction = {
41+
state: "consumer-js get at helloworld",
42+
uponReceiving: "A message",
43+
withRequest: {
44+
method: "GET",
45+
path: "/api/v2.0/helloworld"
46+
},
47+
willRespondWith: {
48+
status: 200,
49+
headers: {
50+
"Content-Type": "application/json; charset=utf-8",
51+
},
52+
body: EXPECTED_BODY,
53+
},
54+
}
55+
provider.addInteraction(interaction).then(() => {
56+
done()
57+
})
58+
})
59+
60+
it("returns the correct response", done => {
61+
const urlAndPort = {
62+
url: url,
63+
port: port,
64+
}
65+
helloworldv2(urlAndPort).then(response => {
66+
expect(response.data).to.eql(EXPECTED_BODY)
67+
publish(contractPath)
68+
69+
done()
70+
}, done
71+
)
72+
})
73+
})
74+
})
75+
76+
let publish = contractDirectory => {
77+
let pact = require('@pact-foundation/pact-node');
78+
let opts = {
79+
pactBroker: "http://localhost/",
80+
pactFilesOrDirs: [contractDirectory],
81+
consumerVersion: "0.0.1",
82+
publishVerificationResult: true
83+
};
84+
pact.publishPacts(opts);
85+
}

consumer-js/test/mocha.opts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--bail
2+
--reporter spec
3+
--colors
4+
--timeout 10000
5+
--exit

0 commit comments

Comments
 (0)