Skip to content

Commit c9c0f78

Browse files
committed
Update: add 8-api folder
1 parent ba56787 commit c9c0f78

File tree

9 files changed

+274
-0
lines changed

9 files changed

+274
-0
lines changed

0x06-unittests_in_js/10-api/api.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const express = require('express');
2+
3+
const app = express();
4+
const PORT = 7865;
5+
6+
app.use(express.json());
7+
8+
app.get('/', (_req, res) => {
9+
res.send('Welcome to the payment system');
10+
});
11+
12+
app.get('/cart/:id(\\d+)', (req, res) => {
13+
const id = req.params.id;
14+
15+
res.send(`Payment methods for cart ${id}`);
16+
});
17+
18+
app.get('/available_payments', (_req, res) => {
19+
res.json({ payment_methods: { credit_cards: true, paypal: false } });
20+
});
21+
22+
app.post('/login', (req, res) => {
23+
let username = '';
24+
25+
if (req.body) {
26+
username = req.body.userName;
27+
}
28+
29+
res.send(`Welcome ${username}`);
30+
});
31+
32+
app.listen(PORT, () => {
33+
console.log(`API available on localhost port ${PORT}`);
34+
});
35+
36+
module.exports = app;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const request = require('request');
2+
const { expect } = require('chai');
3+
4+
describe('API integration test', () => {
5+
const API_URL = 'http://localhost:7865';
6+
7+
it('GET / returns correct response', (done) => {
8+
request.get(`${API_URL}/`, (_err, res, body) => {
9+
expect(res.statusCode).to.be.equal(200);
10+
expect(body).to.be.equal('Welcome to the payment system');
11+
done();
12+
});
13+
});
14+
15+
it('GET /cart/:id returns correct response for valid :id', (done) => {
16+
request.get(`${API_URL}/cart/47`, (_err, res, body) => {
17+
expect(res.statusCode).to.be.equal(200);
18+
expect(body).to.be.equal('Payment methods for cart 47');
19+
done();
20+
});
21+
});
22+
23+
it('GET /cart/:id returns 404 response for negative number values in :id', (done) => {
24+
request.get(`${API_URL}/cart/-47`, (_err, res, _body) => {
25+
expect(res.statusCode).to.be.equal(404);
26+
done();
27+
});
28+
});
29+
30+
it('GET /cart/:id returns 404 response for non-numeric values in :id', (done) => {
31+
request.get(`${API_URL}/cart/d200-44a5-9de6`, (_err, res, _body) => {
32+
expect(res.statusCode).to.be.equal(404);
33+
done();
34+
});
35+
});
36+
37+
it('POST /login returns valid response', (done) => {
38+
request.post(`${API_URL}/login`, {json: {userName: 'Pinkbrook'}}, (_err, res, body) => {
39+
expect(res.statusCode).to.be.equal(200);
40+
expect(body).to.be.equal('Welcome Pinkbrook');
41+
done();
42+
});
43+
});
44+
45+
it('GET /available_payments returns valid response', (done) => {
46+
request.get(`${API_URL}/available_payments`, (_err, res, body) => {
47+
expect(res.statusCode).to.be.equal(200);
48+
expect(JSON.parse(body))
49+
.to.be.deep.equal({payment_methods: {credit_cards: true, paypal: false}});
50+
done();
51+
});
52+
});
53+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "10-api",
3+
"version": "1.0.0",
4+
"description": "A simple API for learning basic integration testing in NodeJS.",
5+
"main": "api.js",
6+
"author": "Bezaleel Olakunori <bezaleeloci@gmail.com>",
7+
"private": true,
8+
"license": "MIT",
9+
"scripts": {
10+
"test": "node ./node_modules/mocha/bin/mocha"
11+
},
12+
"dependencies": {
13+
"express": "^4.17.1"
14+
},
15+
"devDependencies": {
16+
"chai": "^4.2.0",
17+
"mocha": "^6.2.2",
18+
"request": "^2.88.0",
19+
"sinon": "^7.5.0"
20+
},
21+
"repository": {
22+
"type": "git",
23+
"url": "https://github.com/B3zaleel/alx-backend-javascript"
24+
},
25+
"bugs": {
26+
"url": "https://github.com/B3zaleel/alx-backend-javascript/issues"
27+
},
28+
"homepage": "https://github.com/B3zaleel/alx-backend-javascript/tree/main/0x06-unittests_in_js/10-api",
29+
"engines": {
30+
"node": "16.x",
31+
"npm": "8.x",
32+
"yarn": "1.x"
33+
}
34+
}

0x06-unittests_in_js/8-api/api.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const express = require('express');
2+
3+
const app = express();
4+
const PORT = 7865;
5+
6+
app.get('/', (_, res) => {
7+
res.send('Welcome to the payment system');
8+
});
9+
10+
app.listen(PORT, () => {
11+
console.log(`API available on localhost port ${PORT}`);
12+
});
13+
14+
module.exports = app;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const request = require('request');
2+
const { expect } = require('chai');
3+
4+
describe('API integration test', () => {
5+
const API_URL = 'http://localhost:7865';
6+
7+
it('GET / returns correct response', (done) => {
8+
request.get(`${API_URL}/`, (_err, response, body) => {
9+
expect(response.statusCode).to.be.equal(200);
10+
expect(body).to.be.equal('Welcome to the payment system');
11+
done();
12+
});
13+
});
14+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "8-api",
3+
"version": "1.0.0",
4+
"description": "A simple API for learning basic integration testing in NodeJS.",
5+
"main": "api.js",
6+
"author": "Bezaleel Olakunori <bezaleeloci@gmail.com>",
7+
"private": true,
8+
"license": "MIT",
9+
"scripts": {
10+
"test": "node ./node_modules/mocha/bin/mocha"
11+
},
12+
"dependencies": {
13+
"express": "^4.17.1"
14+
},
15+
"devDependencies": {
16+
"chai": "^4.2.0",
17+
"mocha": "^6.2.2",
18+
"request": "^2.88.0",
19+
"sinon": "^7.5.0"
20+
},
21+
"repository": {
22+
"type": "git",
23+
"url": "https://github.com/B3zaleel/alx-backend-javascript"
24+
},
25+
"bugs": {
26+
"url": "https://github.com/B3zaleel/alx-backend-javascript/issues"
27+
},
28+
"homepage": "https://github.com/B3zaleel/alx-backend-javascript/tree/main/0x06-unittests_in_js/8-api",
29+
"engines": {
30+
"node": "16.x",
31+
"npm": "8.x",
32+
"yarn": "1.x"
33+
}
34+
}

0x06-unittests_in_js/9-api/api.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const express = require('express');
2+
const app = express();
3+
const PORT = 7865;
4+
5+
app.get('/', (_, res) => {
6+
res.send('Welcome to the payment system');
7+
});
8+
9+
app.get('/cart/:id(\\d+)', (req, res) => {
10+
const id = req.params.id;
11+
12+
res.send(`Payment methods for cart ${id}`);
13+
});
14+
15+
app.listen(PORT, () => {
16+
console.log(`API available on localhost port ${PORT}`);
17+
});
18+
19+
module.exports = app;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const request = require('request');
2+
const { expect } = require('chai');
3+
4+
describe('API integration test', () => {
5+
const API_URL = 'http://localhost:7865';
6+
7+
it('GET / returns correct response', (done) => {
8+
request.get(`${API_URL}/`, (_err, res, body) => {
9+
expect(res.statusCode).to.be.equal(200);
10+
expect(body).to.be.equal('Welcome to the payment system');
11+
done();
12+
});
13+
});
14+
15+
it('GET /cart/:id returns correct response for valid :id', (done) => {
16+
request.get(`${API_URL}/cart/47`, (_err, res, body) => {
17+
expect(res.statusCode).to.be.equal(200);
18+
expect(body).to.be.equal('Payment methods for cart 47');
19+
done();
20+
});
21+
});
22+
23+
it('GET /cart/:id returns 404 response for negative number values in :id', (done) => {
24+
request.get(`${API_URL}/cart/-47`, (_err, res, _body) => {
25+
expect(res.statusCode).to.be.equal(404);
26+
done();
27+
});
28+
});
29+
30+
it('GET /cart/:id returns 404 response for non-numeric values in :id', (done) => {
31+
request.get(`${API_URL}/cart/d200-44a5-9de6`, (_err, res, _body) => {
32+
expect(res.statusCode).to.be.equal(404);
33+
done();
34+
});
35+
});
36+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "9-api",
3+
"version": "1.0.0",
4+
"description": "A simple API for learning basic integration testing in NodeJS.",
5+
"main": "api.js",
6+
"author": "Bezaleel Olakunori <bezaleeloci@gmail.com>",
7+
"private": true,
8+
"license": "MIT",
9+
"scripts": {
10+
"test": "node ./node_modules/mocha/bin/mocha"
11+
},
12+
"dependencies": {
13+
"express": "^4.17.1"
14+
},
15+
"devDependencies": {
16+
"chai": "^4.2.0",
17+
"mocha": "^6.2.2",
18+
"request": "^2.88.0",
19+
"sinon": "^7.5.0"
20+
},
21+
"repository": {
22+
"type": "git",
23+
"url": "https://github.com/B3zaleel/alx-backend-javascript"
24+
},
25+
"bugs": {
26+
"url": "https://github.com/B3zaleel/alx-backend-javascript/issues"
27+
},
28+
"homepage": "https://github.com/B3zaleel/alx-backend-javascript/tree/main/0x06-unittests_in_js/9-api",
29+
"engines": {
30+
"node": "16.x",
31+
"npm": "8.x",
32+
"yarn": "1.x"
33+
}
34+
}

0 commit comments

Comments
 (0)