Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug for matchers in namespaced xml elements #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,20 @@ module.exports = {
}
})
},

getXml: (id) => {
return axios
.get(accountServiceUrl + "/data/xml/" + id)
.then((data) => {
return data
})
},

getText: (id) => {
return axios
.get(accountServiceUrl + "/data/" + id)
.then((data) => {
return data
})
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require("path")
const transactionService = require("./transaction-service")
const { PactV3, MatchersV3 } = require("@pact-foundation/pact/v3")
const { PactV3, MatchersV3, XmlBuilder } = require("@pact-foundation/pact/v3")
const { expect } = require("chai")
const { string, integer, url2, regex, datetime, fromProviderState } = MatchersV3

Expand Down Expand Up @@ -64,4 +64,62 @@ describe("Transaction service - create a new transaction for an account", () =>
})
})
})

// MatchersV3.string doesn't work within XmlBuilder with namespaced xml
it("test xml data", () => {
provider
.given("set id", {id: "52"})
.uponReceiving("a request to get the plain data")
.withRequest({
method: "GET",
path: MatchersV3.fromProviderState("/data/xml/${id}", "/data/42"),
})
.willRespondWith({
status: 200,
headers: { "Content-Type": 'application/xml; charset=utf-8' },
body: new XmlBuilder('1.0', '', 'root').build(root => {
root.setAttributes({"xmlns:h":"http://www.w3.org/TR/html4/"})
root.appendElement('data', '', data => {
data.appendElement("h:data", "", MatchersV3.string("random"))
.appendElement('id', '', MatchersV3.fromProviderState("${id}", "42"))
})
})
})

return provider.executeTest(async (mockserver) => {
transactionService.setAccountServiceUrl(mockserver.url)
return transactionService
.getXml(42)
.then((result) => {
console.log(result.data)
expect(result.data).to.equal(`<?xml version='1.0'?><root xmlns:h='http://www.w3.org/TR/html4/'><data><h:data>random</h:data><id>42</id></data></root>`)
})
})
})

// MatchersV3.fromProviderState on body doesn't work properly
it("test text data", () => {
provider
.given("set id", {id: "42"})
.uponReceiving("a request to get the plain data")
.withRequest({
method: "GET",
path: MatchersV3.fromProviderState("/data/${id}", "/data/42"),
})
.willRespondWith({
status: 200,
headers: { "Content-Type": 'text/plain; charset=utf-8' },
body: MatchersV3.fromProviderState("data: testData, id: ${id}", "data: testData, id: 42")
})

return provider.executeTest(async (mockserver) => {
transactionService.setAccountServiceUrl(mockserver.url)
return transactionService
.getText(42)
.then((result) => {
console.log(result.data)
expect(result.data).to.equal("data: testData, id: 42")
})
})
})
})
18 changes: 18 additions & 0 deletions examples/v3/provider-state-injected/provider/account-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ server.get("/accounts/search/findOneByAccountNumberId", (req, res) => {
})
})

server.get("/data/xml/:id", (req, res) => {
res.header('Content-Type', 'application/xml; charset=utf-8')
res.send(`<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:h="http://www.w3.org/TR/html4/">
<data>
<h:data>testData</h:data>
<id>${req.params.id}</id>
</data>
</root>`);
})

server.get("/data/:id", (req, res) => {
res.header('Content-Type', 'text/plain; charset=utf-8')
res.send("data: testData, id: " + req.params.id);
})

//server.listen(8081)

module.exports = {
accountService: server,
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ describe("Account Service", () => {
return null
}
},
"set id": (setup, params) => {
if (setup) {
return { id : params.id }
}
},
"set path": (setup, params) => {
if (setup) {
return { id : params.id, path: params.path}
}
},
},

pactUrls: [
Expand Down