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

added integration tests for subtract, multiply and add #196

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 50 additions & 35 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,61 @@
const http = require('http');
const mathM = require('./math')

const http = require("http");
const mathM = require("./math");

const PORT = 3000;

function catchError() {
res.writeHead(400).end(
JSON.stringify({
message: error,
})
);
}

const handleRequest = (req, res) => {
const {url, method } = req;
const body = [];
let result

if (url === '/calculate' && method === 'POST') {
req.on('data', (chunk) => {
body.push(chunk);
});

req.on('end', () => {
const parsedBody = Buffer.concat(body).toString()
const bodyObject = JSON.parse(parsedBody);

const { action, num1, num2 } = bodyObject

if (action === 'sum') {
result = mathM.add(num1, num2)
} else if (action === 'subtract') {
result = mathM.subtract(num1, num2)
} else if (action === 'divide') {
result = mathM.divide(num1, num2)
} else if (action === 'multiply') {
result = mathM.multiply(num1, num2)
}

res.end(JSON.stringify({ result }))
})


const { url, method } = req;
const body = [];
let result;

if (url === "/calculate" && method === "POST") {
try {
req.on("data", (chunk) => {
body.push(chunk);
});
req.on("end", () => {
const parsedBody = Buffer.concat(body).toString();
const bodyObject = JSON.parse(parsedBody);

const { action, num1, num2 } = bodyObject;
try {
if (action === "sum") {
result = mathM.add(num1, num2);
} else if (action === "subtract") {
result = mathM.subtract(num1, num2);
} else if (action === "divide") {
result = mathM.divide(num1, num2);
} else if (action === "multiply") {
result = mathM.multiply(num1, num2);
} else {
res.end("action not added!");
}

res.setHeader("Content-Type", "application/json");
res.writeHead(200);
res.end(JSON.stringify({ result }));
} catch (error) {
catchError;
}
});
} catch (error) {
catchError;
}
}
}
};

const server = http.createServer(handleRequest);

server.listen(PORT, () => {
console.log(`Server is listening on port: ${PORT}`)
})
console.log(`Server is listening on port: ${PORT}`);
});

module.exports = server;
Loading