Skip to content

Commit

Permalink
feat: added consumer for provider state injected example
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Nov 16, 2020
1 parent 722b3ce commit bdc333c
Show file tree
Hide file tree
Showing 5 changed files with 6,894 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/v3/provider-state-injected/README.md
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/

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
}
}
})
}
}
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)
})
})
})
})
Loading

0 comments on commit bdc333c

Please sign in to comment.