Skip to content

Commit

Permalink
refactor: change request
Browse files Browse the repository at this point in the history
  • Loading branch information
acmachado14 committed Dec 3, 2023
1 parent 263446f commit 024384d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 113 deletions.
90 changes: 48 additions & 42 deletions consumer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions consumer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
7 changes: 1 addition & 6 deletions consumer/src/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,6 +16,5 @@ const sum = async (num1, num2) => {
}

module.exports = {
getPerson,
sum
sum
};
37 changes: 0 additions & 37 deletions consumer/test/contract/getPerson.test.js

This file was deleted.

20 changes: 11 additions & 9 deletions consumer/test/contract/postSum.test.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
36 changes: 20 additions & 16 deletions provider/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

Expand Down

0 comments on commit 024384d

Please sign in to comment.