Skip to content

Commit b38dc72

Browse files
committed
add new module + test
1 parent 5db2560 commit b38dc72

File tree

5 files changed

+118
-13
lines changed

5 files changed

+118
-13
lines changed

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module.exports = {
33
"browser": true,
44
"es6": true,
55
"node": true,
6+
"jest": true,
7+
68
},
79
"extends": "eslint:recommended",
810
"globals": {

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
[![](https://img.shields.io/github/issues/algo7/express-suite)]()
99
[![](https://img.shields.io/github/forks/algo7/express-suite)]()
1010
[![](https://img.shields.io/github/stars/algo7/express-suite)]()
11+
[![npm version](https://badge.fury.io/js/express-suite.svg)](https://badge.fury.io/js/express-suite)
12+
[![](https://img.shields.io/npm/dt/express-suite)]()
1113

1214
## Installation
1315

app.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,11 @@ const routeCheck = (app, opt) => {
7878

7979
//Route Validation Function
8080
/**
81-
*
82-
* @param {Object} [opt] - The options
83-
* @param {Boolean} [opt.checkGet]
8481
* @returns {function():void}
8582
*/
86-
const inputValidation = (opt) => {
87-
83+
const inputValidation = ({
84+
checkGet = true,
85+
}) => {
8886

8987
return (req, res, next) => {
9088

@@ -94,11 +92,15 @@ const inputValidation = (opt) => {
9492
return res.sendStatus(200);
9593
}
9694

97-
//Let the get method pass
98-
if (req.method === 'GET') {
99-
return next();
95+
//Wether to check the body of GET requests as well
96+
if (!checkGet) {
97+
//Let the get method pass
98+
if (req.method === 'GET') {
99+
return next();
100+
}
100101
}
101102

103+
102104
//Deconstruct the request to get the body out
103105
const { body, } = req;
104106

@@ -149,7 +151,6 @@ const inputValidation = (opt) => {
149151
};
150152

151153

152-
153154
module.exports = {
154155
routeCheck,
155156
inputValidation,

test/main.test.js

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
/**
22
* @jest-environment node
33
*/
4+
45
const { server, } = require('./server');
56
const axios = require('axios').default;
67

7-
88
describe('Testing the Check Route Module', () => {
99

1010
test('Sending Request to Existing Route', () => {
1111

1212
expect.assertions(1);
13-
return axios.get('http://localhost:5003/')
13+
return axios.get('http://localhost:5003/', {
14+
data: {
15+
testField: 'test_data',
16+
},
17+
})
1418
.then(data => {
1519
const redirectCount = data.request._redirectable._redirectCount;
1620
expect(redirectCount).toBe(0);
@@ -34,6 +38,82 @@ describe('Testing the Check Route Module', () => {
3438

3539
});
3640

41+
describe('Testing the Input Validation Module', () => {
42+
43+
const request = {
44+
method: '',
45+
url: 'http://localhost:5003/',
46+
data: {
47+
testField: 'test_data',
48+
},
49+
};
50+
51+
const optRequest = {
52+
method: 'OPTIONS',
53+
url: 'http://localhost:5003/',
54+
};
55+
56+
test('Sending OPTIONS Request', () => {
57+
58+
expect.assertions(1);
59+
return axios(optRequest)
60+
.then(data => {
61+
const stCode = data.status;
62+
expect(stCode).toBe(200);
63+
});
64+
65+
});
66+
67+
test('Sending GET Request', () => {
68+
69+
request.method = 'GET';
70+
expect.assertions(1);
71+
return axios(request)
72+
.then(data => {
73+
const resBd = data.data.msg;
74+
expect(resBd).toBe('test_data');
75+
});
76+
});
77+
78+
test('Sending Empty GET Request', () => {
79+
80+
request.method = 'GET';
81+
request.data = '';
82+
expect.assertions(1);
83+
return axios(request)
84+
.catch(err => {
85+
const errMsg = err.response.data.msg;
86+
expect(errMsg).toBe('Some fields are missing!');
87+
});
88+
});
89+
90+
test('Sending POST Request', () => {
91+
92+
request.method = 'POST';
93+
request.data = {
94+
testField: 'test_data',
95+
};
96+
expect.assertions(1);
97+
return axios(request)
98+
.then(data => {
99+
const resBd = data.data.msg;
100+
expect(resBd).toBe('test_data');
101+
});
102+
});
103+
104+
test('Sending Empty POST Request', () => {
105+
request.method = 'POST';
106+
request.data = '';
107+
expect.assertions(1);
108+
return axios(request)
109+
.catch(err => {
110+
const errMsg = err.response.data.msg;
111+
expect(errMsg).toBe('Some fields are missing!');
112+
});
113+
});
114+
115+
});
116+
37117

38118
afterAll(() => {
39119
server.close();

test/server.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
//Dependencies
22
const express = require('express');
3-
const routeCheck = require('../app').routeCheck;
3+
const bodyParser = require('body-parser');
4+
const { routeCheck, inputValidation, } = require('../app');
45
//Global Constant
56
const PORT = 5003;
67

78
//Initialize the App
89
const app = express();
910

11+
// bodyParser Middleware
12+
app.use(bodyParser.json({
13+
limit: '5mb',
14+
extended: true,
15+
}));
1016

17+
//Headers Settings
1118
app.all('*', (req, res, next) => {
1219
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:5003');
1320
next();
1421
});
1522

23+
//The inputValidation middleware
24+
app.use(inputValidation({ checkGet: true, }));
25+
1626
//Exists
1727
app.get('/', (req, res) => {
18-
res.sendStatus(200);
28+
res.status(200).json({
29+
msg: req.body.testField,
30+
});
1931
});
2032

2133
//Exists but redirect to a non-existing route
@@ -28,9 +40,17 @@ app.get('/PnF', (req, res) => {
2840
res.sendStatus(404);
2941
});
3042

43+
//POST Route
44+
app.post('/', (req, res) => {
45+
res.status(200).json({
46+
msg: req.body.testField,
47+
});
48+
});
49+
3150

3251
app.use(routeCheck(app, { path: '/PnF', }));
3352

53+
3454
const server = app.listen(PORT);
3555

3656
module.exports = { server, };

0 commit comments

Comments
 (0)