-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceptions.js
179 lines (163 loc) · 5.84 KB
/
receptions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const express = require('express');
const mongoose = require('mongoose');
const fs = require('fs');
const envUtils = require('../helpers/envutils');
const multer = require('multer');
const upload = multer({ dest: envUtils.getTempDir() })
const router = express.Router();
//import models
require('../models/HttpReception');
require('../models/HttpDump');
const HttpReception = mongoose.model('httpreception');
const HttpDump = mongoose.model('httdump');
//receptions index page
router.get('/', (req, res) => {
HttpDump.find({ httpReceptionId: { $ne: null } })
.sort({ date: 'desc' })
.then(httpDumps => {
//retrieve http receptions
HttpReception.find({})
.sort({ date: 'desc' })
.then(httpReceptions => {
res.render('receptions/index', {
httpDumps: httpDumps,
httpReceptions: httpReceptions,
indexView: true
});
})
.catch(err => {
res.send("Error occured while retrieving receptions");
});
})
.catch(err => {
res.send("Error occured while retrieving http dumps");
});
});
//display http dumps for selected reception
router.get('/:id', (req, res) => {
HttpDump.find({ httpReceptionId: req.params.id })
.sort({ date: 'desc' })
.then(httpDumps => {
//retrieve http receptions
HttpReception.find({})
.sort({ date: 'desc' })
.then(httpReceptions => {
res.render('receptions/index', {
receptionUrl: envUtils.getHostName() + "/reception/" + req.params.id,
selectedReceptionId: req.params.id,
httpDumps: httpDumps,
httpReceptions: httpReceptions
});
})
.catch(err => {
res.send("Error occured while retrieving receptions");
});
})
.catch(err => {
res.send("Error occured while retrieving http dumps")
});
});
//it will create a new httpreception when accessing this route
router.get('/add/new', (req, res) => {
const defaultResponse = "HTTP Duck Says Hello!";
//create new http reception
const buffer = new Buffer.from(defaultResponse, "UTF-8");
const newHttpReception = new HttpReception({
body: buffer
});
newHttpReception.save()
.then(httpReception => {
res.render('receptions/add', {
url: envUtils.getHostName() + "/reception/" + httpReception._id,
receptionId: httpReception._id,
responseBody: defaultResponse
});
})
.catch(err => {
console.log(err);
});
});
//update route for httpreception
router.post('/:id', upload.single('responsePayload'), (req, res) => {
console.log("");
HttpReception.findOne({
_id: req.params.id
})
.then(httpReception => {
const newHttpReceptionBody = Buffer.from(req.body.responseBody, "UTF-8");
httpReception.body = newHttpReceptionBody;
httpReception.name = req.body.name;
httpReception.responseStatus = req.body.status;
httpReception.reasonPhrase = req.body.reasonPhrase;
//check file uploaded
if (typeof req.file != "undefined") {
const path = req.file.path;
//process file
const fileBuff = fs.readFileSync(path);
//delete file
fs.unlinkSync(path);
httpReception.body = fileBuff;
}
var idx = 0;
const headerMap = [];
//add user defined headers to http reception
for (; idx < req.body.header.length; idx++) {
const headerName = req.body.header[idx];
const headerValue = req.body.headerVal[idx];
// don't accept header values with empty strings
if (headerName.length > 0) {
headerMap.push({
key: headerName,
value: headerValue
})
}
}
httpReception.responseHeaders = headerMap;
httpReception.save()
.then(updatedHttpReception => {
console.log(updatedHttpReception.body);
//TODO success message here
res.redirect('/receptions')
})
.catch(err => {
res.send(err)
});
}).catch(err => {
res.send(err)
});
});
//delete route for a selected http dump
router.get('/dump/:id/delete', (req, res) => {
HttpDump.deleteOne({ _id: req.params.id })
.then(() => {
req.flash('warning_msg', 'HttpDump Removed Successfully!');
res.redirect('/receptions')
})
.catch(err => {
console.log(err);
res.send("Unable to delete")
})
});
//delete route for receptions
router.get('/:id/delete', (req, res) => {
//first find the reception to delete
HttpDump.deleteMany({
httpReceptionId: req.params.id
})
.then(() => {
HttpReception.deleteOne({
_id: req.params.id
})
.then(() => {
req.flash('warning_msg', 'HttpReception and Associated HttpDumps Removed Successfully!');
res.redirect('/receptions');
})
.catch(err => {
res.send("Error when deleting HttpReception")
});
})
.catch(err => {
res.send("Error when deleting HttpDump")
});
});
module.exports = router;