Skip to content

Commit

Permalink
feat: add support for binary payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed May 12, 2020
1 parent 6feacbe commit 658ffa0
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 149 deletions.
9 changes: 9 additions & 0 deletions examples/v3/todo-consumer/src/todo.js
Expand Up @@ -2,6 +2,7 @@ const axios = require("axios")
const parser = require("xml2json")
const eyes = require("eyes")
const R = require("ramda")
const fs = require('fs')

let serverUrl = "http://localhost:2203"

Expand Down Expand Up @@ -32,4 +33,12 @@ module.exports = {
serverUrl = url
return this
},
postImage: (id, image) => {
const data = fs.readFileSync(image)
return axios.post(serverUrl + "/projects/" + id + "/images", data, {
headers: {
'Content-Type': 'application/octet-stream'
}
})
},
}
61 changes: 46 additions & 15 deletions examples/v3/todo-consumer/test/consumer.spec.js
Expand Up @@ -21,14 +21,13 @@ describe("Pact V3", () => {
context("when there are a list of projects", () => {
describe("and there is a valid user session", () => {
describe("with JSON request", () => {
const provider = new PactV3({
consumer: "TodoApp",
provider: "TodoServiceV3",
dir: path.resolve(process.cwd(), "pacts"),
logLevel: "INFO",
})

before(() => {
provider = new PactV3({
consumer: "TodoApp",
provider: "TodoServiceV3",
dir: path.resolve(process.cwd(), "pacts"),
logLevel: "INFO",
})
provider
.given("i have a list of projects")
.uponReceiving("a request for projects")
Expand Down Expand Up @@ -81,15 +80,14 @@ describe("Pact V3", () => {
})
})

describe.only("with XML requests", () => {
const provider = new PactV3({
consumer: "TodoApp",
provider: "TodoServiceV3",
dir: path.resolve(process.cwd(), "pacts"),
logLevel: "INFO",
})

describe("with XML requests", () => {
before(() => {
provider = new PactV3({
consumer: "TodoApp",
provider: "TodoServiceV3",
dir: path.resolve(process.cwd(), "pacts"),
logLevel: "INFO",
})
provider
.given("i have a list of projects")
.uponReceiving("a request for projects in XML")
Expand Down Expand Up @@ -171,5 +169,38 @@ describe("Pact V3", () => {
return result
})
})

describe("with image uploads", () => {
before(() => {
provider = new PactV3({
consumer: "TodoApp",
provider: "TodoServiceV3",
dir: path.resolve(process.cwd(), "pacts"),
logLevel: "INFO",
})
provider
.given("i have a project", { id: "1001", name: "Home Chores" })
.uponReceiving("a request to store an image against the project")
.withRequestBinaryFile(
{ method: "POST", path: "/projects/1001/images" },
"image/jpeg",
path.resolve(__dirname, "example.jpg")
)
.willRespondWith({ status: 201 })
})

it("stores the image against the project", async () => {
let result = await provider.executeTest(mockserver => {
console.log("In Test Function", mockserver)
return TodoApp.setUrl(mockserver.url).postImage(
1001,
path.resolve(__dirname, "example.jpg")
)
})
console.log("result from runTest", result.status)
expect(result.status).to.be.eq(201)
return result
})
})
})
})
Binary file added examples/v3/todo-consumer/test/example.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 658ffa0

Please sign in to comment.