diff --git a/consumer/package-lock.json b/consumer/package-lock.json index a33c005..b080b19 100644 --- a/consumer/package-lock.json +++ b/consumer/package-lock.json @@ -14,8 +14,8 @@ "@pact-foundation/pact": "^12.1.0", "axios-mock-adapter": "^1.20.0", "chai": "4.3.4", - "mocha": "^10.0.0", - "prettier": "^2.4.1" + "chai-as-promised": "^7.1.1", + "mocha": "^10.0.0" } }, "node_modules/@pact-foundation/pact": { @@ -458,6 +458,18 @@ "node": ">=4" } }, + "node_modules/chai-as-promised": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/chai-as-promised/-/chai-as-promised-7.1.1.tgz", + "integrity": "sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA==", + "dev": true, + "dependencies": { + "check-error": "^1.0.2" + }, + "peerDependencies": { + "chai": ">= 2.1.2 < 5" + } + }, "node_modules/chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", @@ -1061,10 +1073,13 @@ } }, "node_modules/function-bind": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", - "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", - "dev": true + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "dev": true, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } }, "node_modules/get-caller-file": { "version": "2.0.5", @@ -1789,14 +1804,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/mocha/node_modules/path-exists": { - "version": "4.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=8" - } - }, "node_modules/mocha/node_modules/supports-color": { "version": "8.1.1", "dev": true, @@ -1958,6 +1965,15 @@ "node": ">= 0.8" } }, + "node_modules/path-exists": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", + "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/path-is-absolute": { "version": "1.0.1", "dev": true, @@ -2073,21 +2089,6 @@ "node": ">= 0.4.0" } }, - "node_modules/prettier": { - "version": "2.7.1", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.7.1.tgz", - "integrity": "sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==", - "dev": true, - "bin": { - "prettier": "bin-prettier.js" - }, - "engines": { - "node": ">=10.13.0" - }, - "funding": { - "url": "https://github.com/prettier/prettier?sponsor=1" - } - }, "node_modules/process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", @@ -3130,6 +3131,15 @@ "type-detect": "^4.0.5" } }, + "chai-as-promised": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/chai-as-promised/-/chai-as-promised-7.1.1.tgz", + "integrity": "sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA==", + "dev": true, + "requires": { + "check-error": "^1.0.2" + } + }, "chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", @@ -3580,9 +3590,9 @@ "optional": true }, "function-bind": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", - "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", "dev": true }, "get-caller-file": { @@ -4070,10 +4080,6 @@ "p-limit": "^3.0.2" } }, - "path-exists": { - "version": "4.0.0", - "dev": true - }, "supports-color": { "version": "8.1.1", "dev": true, @@ -4190,6 +4196,12 @@ "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==", "dev": true }, + "path-exists": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", + "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", + "dev": true + }, "path-is-absolute": { "version": "1.0.1", "dev": true @@ -4279,12 +4291,6 @@ "version": "0.4.1", "dev": true }, - "prettier": { - "version": "2.7.1", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.7.1.tgz", - "integrity": "sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==", - "dev": true - }, "process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", diff --git a/consumer/package.json b/consumer/package.json index 26eea25..cdefcef 100644 --- a/consumer/package.json +++ b/consumer/package.json @@ -7,13 +7,12 @@ "scripts": { "test:consumer": "mocha --config test/contract/.mocharc.js", "pact:publish": "pact-broker publish pacts/ --consumer-app-version=1.0.0 --broker-base-url=http://localhost:9292/" - }, "devDependencies": { "@pact-foundation/pact": "^12.1.0", "axios-mock-adapter": "^1.20.0", "chai": "4.3.4", - "mocha": "^10.0.0", - "prettier": "^2.4.1" + "chai-as-promised": "^7.1.1", + "mocha": "^10.0.0" } } diff --git a/consumer/src/consumer.js b/consumer/src/consumer.js index e6d5d31..1c57ca5 100644 --- a/consumer/src/consumer.js +++ b/consumer/src/consumer.js @@ -2,10 +2,6 @@ const axios = require('axios') const baseUrl = `http://127.0.0.1:8001`; -const getPerson = async (id) => { - return await axios.get(`${baseUrl}/person/${id}`); -} - const sum = async (num1, num2) => { const response = await axios.post(`${baseUrl}/sum`, { number1: num1, @@ -20,6 +16,5 @@ const sum = async (num1, num2) => { } module.exports = { - getPerson, - sum + sum }; \ No newline at end of file diff --git a/consumer/test/contract/getPerson.test.js b/consumer/test/contract/getPerson.test.js deleted file mode 100644 index da19cc1..0000000 --- a/consumer/test/contract/getPerson.test.js +++ /dev/null @@ -1,37 +0,0 @@ -"use strict" - -const { expect } = require('chai') -const { Matchers } = require("@pact-foundation/pact") - -const { getPerson } = require('../../src/consumer') - -describe('API Pact test - Integration between \'backend\' and \'frontend\'', () => { - describe("GET /person/:id", () => { - const expectedBody = { - first_name: 'Angelo', - last_name: 'Machado', - alias: 'gelin', - age: 19, - } - - before (async () => { - await mockProvider.addInteraction({ - uponReceiving: "a request for person with id 1", - withRequest: { - method: "GET", - path: "/person/1" - }, - willRespondWith: { - status: 200, - body: Matchers.like(expectedBody), - }, - }) - }) - - it("returns correct body, header and statusCode", async () => { - const response = await getPerson(1) - expect(response.data).to.deep.equal(expectedBody) - expect(response.status).to.equal(200) - }) - }) -}) \ No newline at end of file diff --git a/consumer/test/contract/postSum.test.js b/consumer/test/contract/postSum.test.js index c601a8e..89648f5 100644 --- a/consumer/test/contract/postSum.test.js +++ b/consumer/test/contract/postSum.test.js @@ -1,26 +1,28 @@ -"use strict" +"use strict"; -const { expect } = require('chai') -const { Matchers } = require("@pact-foundation/pact") +const { expect } = require("chai"); +const { Matchers } = require("@pact-foundation/pact"); -const { sum } = require('../../src/consumer') +const { sum } = require("../../src/consumer"); -describe('API Pact test', () => { +describe("API Pact test", () => { describe("POST /sum", () => { const expectedBody = { result: 30, }; + const requestPayload = { + number1: Matchers.somethingLike(10), + number2: Matchers.somethingLike(20), + }; + before(async () => { await mockProvider.addInteraction({ uponReceiving: "a request to calculate the sum of two numbers", withRequest: { method: "POST", path: "/sum", - body: { - number1: 10, - number2: 20, - }, + body: requestPayload, }, willRespondWith: { status: 200, diff --git a/provider/index.php b/provider/index.php index af40afb..26205f6 100644 --- a/provider/index.php +++ b/provider/index.php @@ -8,31 +8,35 @@ $app = AppFactory::create(); $app->addErrorMiddleware(false, true, false); -$app->get("/person/{id}", function (Request $request, Response $response, array $args) { - $body = [ - "first_name" => "Angelo", - "last_name" => "Machado", - "alias" => "gelin", - "age" => 19, - ]; - $response = $response->withHeader("Content-Type", "application/json"); - $response->getBody()->write(json_encode($body)); +$app->options('/{routes:.+}', function ($request, $response, $args) { return $response; }); +$app->add(function ($request, $handler) { + $response = $handler->handle($request); + return $response + ->withHeader('Access-Control-Allow-Origin', '*') + ->withHeader('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Origin'); +}); + $app->post("/sum", function (Request $request, Response $response, array $args) { $data = $request->getParsedBody(); if (isset($data['number1']) && isset($data['number2'])) { - $number1 = (int) $data['number1']; - $number2 = (int) $data['number2']; - $sum = $number1 + $number2; - $response->getBody()->write(json_encode(['result' => $sum])); - return $response->withStatus(200)->withHeader('Content-Type', 'application/json'); + $number1 = $data['number1']; + $number2 = $data['number2']; + + if (is_int($number1) && is_int($number2)) { + $sum = $number1 + $number2; + $response->getBody()->write(json_encode(['result' => $sum])); + return $response->withStatus(200)->withHeader('Content-Type', 'application/json'); + } + + $response->getBody()->write(json_encode(['error' => 'Please provide the numbers "number1" and "number2" as integers'])); + return $response->withStatus(400)->withHeader('Content-Type', 'application/json'); } - $errorResponse = ['error' => 'Please provide the numbers "number1" and "number2"']; - $response->getBody()->write(json_encode($errorResponse)); + $response->getBody()->write(json_encode(['error' => 'Please provide the numbers "number1" and "number2"'])); return $response->withStatus(400)->withHeader('Content-Type', 'application/json'); });