1 var http = require('http');
2 var https = require('https');
3 var ArgumentParser = require('argparse').ArgumentParser;
4 var express = require('express');
5 const stream = require('stream');
7 var fs = require('fs');
8 var path = require('path');
9 var privateKey = fs.readFileSync('cert/private.key', 'utf8');
10 var certificate = fs.readFileSync('cert/certificate.crt', 'utf8');
11 var credentials = {key: privateKey, cert: certificate};
12 const allPublished = "allPublished";
13 const nonePublished = "nonePublished";
15 var parser = new ArgumentParser({
18 description: 'Datarouter simulator'
21 parser.addArgument('--tc' , { help: 'TC $NoOfTc' } );
22 parser.addArgument('--printtc' ,
24 help: 'Print complete usage help',
29 var args = parser.parseArgs();
31 if (args.tc=="nonePublished") {
32 console.log("TC: nonePublished")
34 if (args.tc=="allPublished") {
35 console.log("TC: allPublished")
40 console.log("TC nonePublished: no file has already been published.");
41 console.log("TC allPublished: whatever is the request, this file is considered as published.");
42 console.log("No argument passed: normal behaviour, that is publish if not already published");
46 var bodyParser = require('body-parser')
47 app.use(bodyParser.urlencoded({ extended: false }))
49 // parse application/json
50 app.use(bodyParser.json())
52 // parse application/vnd.api+json as json
53 app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
55 // parse some custom thing into a Buffer
56 app.use(bodyParser.raw({limit:1024*1024*20, type: 'application/octet-stream' }))
57 // parse an HTML body into a string
58 app.use(bodyParser.text({ type: 'text/html' }))
59 app.get("/",function(req, res){
65 app.get('/feedlog/1/',function(req, res){
66 var filename = req.query.filename;
67 if(args.tc == allPublished){
68 res.send("[" + filename + "]");
69 } else if(args.tc == nonePublished){
72 if (published.includes(filename)) {
73 res.send("[" + filename + "]");
81 app.put('/publish/1/', function (req, res) {
82 var filename = req.query.filename;
83 var type = req.query.type;
84 if(typeof(filename) == 'undefined'){
85 res.status(400).send({error: 'No filename provided.'});
86 } else if(typeof(type) == 'undefined'){
87 res.status(400).send({error: 'No type provided.'});
89 if(args.tc == allPublished){
90 res.send("[" + filename + "]");
91 } else if(args.tc == nonePublished){
92 res.redirect(301, 'http://127.0.0.1:3908/publish/1/'+filename);
94 if (!published.includes(filename)) {
95 published.push(filename);
96 res.redirect(301, 'http://127.0.0.1:3908/publish/1/'+filename);
105 var httpServer = http.createServer(app);
106 var httpsServer = https.createServer(credentials, app);
110 httpServer.listen(httpPort);
111 console.log("DR-simulator listening (http) at "+httpPort);
112 httpsServer.listen(httpsPort);
113 console.log("DR-simulator listening (https) at "+httpsPort);