-
-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added consumer for provider state injected example
- Loading branch information
Ronald Holshausen
committed
Nov 16, 2020
1 parent
722b3ce
commit bdc333c
Showing
5 changed files
with
6,894 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Provider state injected example | ||
|
||
This example follows what is described in the blog post https://pactflow.io/blog/injecting-values-from-provider-states/ | ||
|
30 changes: 30 additions & 0 deletions
30
examples/v3/provider-state-injected/consumer/transaction-service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const axios = require("axios") | ||
|
||
let accountServiceUrl = "" | ||
|
||
module.exports = { | ||
setAccountServiceUrl: url => { | ||
accountServiceUrl = url | ||
}, | ||
|
||
createTransaction: (accountId, amountInCents) => { | ||
return axios.get(accountServiceUrl + "/accounts/search/findOneByAccountNumberId", { | ||
params: { | ||
accountNumber: accountId | ||
} | ||
}).then(({ data }) => { | ||
// This is the point where a real transaction service would create the transaction, but for the purpose | ||
// of this example we'll assume this has happened here | ||
let id = Math.floor(Math.random() * Math.floor(100000)) | ||
return { | ||
account: { | ||
accountNumber: data.accountNumber.id, | ||
accountReference: data.accountRef | ||
}, | ||
transaction: { | ||
id, amount: amountInCents | ||
} | ||
} | ||
}) | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
examples/v3/provider-state-injected/consumer/transaction-service.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const path = require("path") | ||
const transactionService = require("./transaction-service") | ||
const { PactV3, MatchersV3 } = require("@pact-foundation/pact/v3") | ||
const { expect } = require("chai") | ||
const { | ||
string, | ||
integer, | ||
url, | ||
regex, | ||
datetime | ||
} = MatchersV3 | ||
|
||
describe("Transaction service - create a new transaction for an account", () => { | ||
|
||
let provider | ||
beforeAll(() => { | ||
provider = new PactV3({ | ||
consumer: "TransactionService", | ||
provider: "AccountService", | ||
dir: path.resolve(process.cwd(), "pacts"), | ||
}) | ||
}) | ||
|
||
it("queries the account service for the account details", () => { | ||
|
||
provider | ||
.given("Account Test001 exists", { accountRef: "Test001" }) | ||
.uponReceiving("a request to get the account details") | ||
.withRequest({ | ||
method: "GET", | ||
path: "/accounts/search/findOneByAccountNumberId", | ||
query: { accountNumber: "100" }, | ||
headers: { Accept: "application/json" }, | ||
}) | ||
.willRespondWith({ | ||
status: 200, | ||
headers: { "Content-Type": "application/hal+json" }, | ||
body: { | ||
id: integer(1), | ||
version: integer(0), | ||
name: string("Test"), | ||
accountRef: string("Test001"), | ||
createdDate: datetime("yyyy-MM-dd HH:mm:ss"), | ||
lastModifiedDate: datetime("yyyy-MM-dd HH:mm:ss"), | ||
accountNumber: { | ||
id: integer(100) | ||
}, | ||
_links: { | ||
self: { | ||
href: url("http://localhost:8080", [ "accounts", regex("\\d+", "100") ]) | ||
}, | ||
account: { | ||
href: url("http://localhost:8080", [ "accounts", regex("\\d+", "100") ]) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
return provider.executeTest(async (mockserver) => { | ||
transactionService.setAccountServiceUrl(mockserver.url) | ||
return transactionService.createTransaction(100, 100000).then(result => { | ||
expect(result.account.accountNumber).to.equal(100) | ||
expect(result.transaction.amount).to.equal(100000) | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.