yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 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'); |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 9 | var privateKey = fs.readFileSync('cert/private.key', 'utf8'); |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 10 | var certificate = fs.readFileSync('cert/certificate.crt', 'utf8'); |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 11 | var credentials = { |
| 12 | key: privateKey, |
| 13 | cert: certificate |
| 14 | }; |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | var parser = new ArgumentParser({ |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 18 | version: '0.0.1', |
| 19 | addHelp: true, |
| 20 | description: 'Datarouter simulator' |
| 21 | }); |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 22 | |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 23 | parser.addArgument('--tc', { |
| 24 | help: 'TC $NoOfTc' |
| 25 | }); |
| 26 | parser.addArgument('--printtc', { |
| 27 | help: 'Print complete usage help', |
| 28 | action: 'storeTrue' |
| 29 | }); |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 30 | |
| 31 | var args = parser.parseArgs(); |
| 32 | |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 33 | if (args.tc == "100") { |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 34 | console.log("TC: 100") |
| 35 | } |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 36 | if (args.tc == "101") { |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 37 | console.log("TC: 101") |
| 38 | //preparations |
| 39 | } |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 40 | if (args.tc == "102") { |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 41 | console.log("TC: 102") |
| 42 | //preparations |
| 43 | } |
| 44 | |
| 45 | if (args.printtc) { |
| 46 | console.log("TC 100: receive all incoming files"); |
| 47 | console.log("TC 101: drop/deny first 10 publishing attempt, then receive all"); |
| 48 | console.log("TC 102: drop/deny/every second publisging attempt"); |
| 49 | process.exit(0); |
| 50 | } |
| 51 | |
| 52 | var bodyParser = require('body-parser') |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 53 | app.use(bodyParser.urlencoded({ |
| 54 | extended: false |
| 55 | })) |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 56 | |
| 57 | // parse application/json |
| 58 | app.use(bodyParser.json()) |
| 59 | |
| 60 | // parse application/vnd.api+json as json |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 61 | app.use(bodyParser.json({ |
| 62 | type: 'application/vnd.api+json' |
| 63 | })) |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 64 | |
| 65 | // parse some custom thing into a Buffer |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 66 | app.use(bodyParser.raw({ |
| 67 | limit: 1024 * 1024 * 20, |
| 68 | type: 'application/octet-stream' |
| 69 | })) |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 70 | // parse an HTML body into a string |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 71 | app.use(bodyParser.text({ |
| 72 | type: 'text/html' |
| 73 | })) |
| 74 | app.get("/", function (req, res) { |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 75 | res.send("ok"); |
| 76 | }) |
| 77 | app.post('/webapi/feeds/', function (req, res) { |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 78 | res.setHeader('Content-Type', 'application/json'); |
| 79 | var feedName = req.body.feedName; |
| 80 | console.log(feedName); |
| 81 | res.end(JSON.stringify({ |
| 82 | "type": "feed", |
| 83 | "lastMod": "2019-03-21T16:00:40.489", |
| 84 | "status": "VALID", |
| 85 | "asprClassification": "unclassified", |
| 86 | "feedDescription": "generated for CSIT", |
| 87 | "feedId": "3", |
| 88 | "feedName": feedName, |
| 89 | "feedVersion": "csit", |
| 90 | "logURL": "https://dmaap-dr-prov/feedlog/3", |
| 91 | "owner": "dgl", |
| 92 | "publishURL": "https://dmaap-dr-prov/publish/3", |
| 93 | "pubs": [{ |
| 94 | "lastMod": "2019-01-24T16:00:40.484", |
| 95 | "status": "VALID", |
| 96 | "dcaeLocationName": "san-francisco", |
| 97 | "feedId": "3", |
| 98 | "pubId": "3.4gh53", |
| 99 | "username": "tmp_i63w8psh6ycnoqu", |
| 100 | "userpwd": "6jkc1uwywrc8q4w" |
| 101 | }], |
| 102 | "subs": [], |
| 103 | "subscribeURL": "https://dmaap-dr-prov/subscribe/3", |
| 104 | "suspended": false |
| 105 | })); |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 106 | }) |
| 107 | var httpServer = http.createServer(app); |
| 108 | var httpsServer = https.createServer(credentials, app); |
| 109 | |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 110 | var httpPort = 6665 |
| 111 | var httpsPort = 6666 |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 112 | httpServer.listen(httpPort); |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 113 | console.log("DR-simulator listening (http) at " + httpPort) |
yanhuanwang | db4bd69 | 2019-03-15 07:57:38 +0000 | [diff] [blame] | 114 | httpsServer.listen(httpsPort); |
yanhuanwang | 08463e5 | 2019-03-22 09:23:23 +0000 | [diff] [blame] | 115 | console.log("DR-simulator listening (https) at " + httpsPort) |