Simulator scripts for datafile-collector
[integration.git] / test / mocks / datafilecollector-testharness / dr-sim / dmaapDR.js
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');
6 var app = express();
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
13
14 var parser = new ArgumentParser({
15           version: '0.0.1',
16           addHelp:true,
17           description: 'Datarouter simulator'
18         });
19
20 parser.addArgument('--tc' , { help: 'TC $NoOfTc' } );
21 parser.addArgument('--printtc' ,
22                 {
23                         help: 'Print complete usage help',
24                         action: 'storeTrue'
25                 }
26         );
27
28 var args = parser.parseArgs();
29
30 if (args.tc=="100") {
31         console.log("TC: 100")
32 }
33 if (args.tc=="101") {
34         console.log("TC: 101")
35         //preparations
36 }
37 if (args.tc=="102") {
38         console.log("TC: 102")
39         //preparations
40 }
41
42 if (args.printtc) {
43         console.log("TC 100: receive all incoming files");
44         console.log("TC 101: drop/deny first 10 publishing attempt, then receive all");
45         console.log("TC 102: drop/deny/every second publisging attempt");
46         process.exit(0);
47 }
48
49 var bodyParser = require('body-parser')
50 app.use(bodyParser.urlencoded({ extended: false }))
51
52 // parse application/json
53 app.use(bodyParser.json())
54
55 // parse application/vnd.api+json as json
56 app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
57
58 // parse some custom thing into a Buffer
59 app.use(bodyParser.raw({limit:1024*1024*20, type: 'application/octet-stream' }))
60 // parse an HTML body into a string
61 app.use(bodyParser.text({ type: 'text/html' }))
62 app.get("/",function(req, res){
63         res.send("ok");
64 })
65 app.put('/publish/1/:filename', function (req, res) {
66         console.log(req.files);
67         console.log(req.body);
68         console.log(req.headers);
69         var filename = path.basename(req.params.filename);
70          res.redirect(301, 'http://127.0.0.1:3908/publish/1/'+filename)
71 })
72 var httpServer = http.createServer(app);
73 var httpsServer = https.createServer(credentials, app);
74
75 var httpPort=3906
76 var httpsPort=3907
77 httpServer.listen(httpPort);
78 console.log("DR-simulator listening (http) at "+httpPort)
79 httpsServer.listen(httpsPort);
80 console.log("DR-simulator listening (https) at "+httpsPort)