Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / http-proxy / test / http / routing-table-test.js
1 /*
2  * routing-table-test.js: Tests for the proxying using the ProxyTable object.
3  *
4  * (C) 2010, Charlie Robbins
5  *
6  */
7
8 var assert = require('assert'),
9     fs = require('fs'),
10     path = require('path'),
11     async = require('async'),
12     request = require('request'),
13     vows = require('vows'),
14     macros = require('../macros'),
15     helpers = require('../helpers');
16
17 var routeFile = path.join(__dirname, 'config.json');
18
19 vows.describe(helpers.describe('routing-table')).addBatch({
20   "With a routing table": {
21     "with latency": macros.http.assertProxiedToRoutes({
22       latency: 2000,
23       routes: {
24         "icanhaz.com": "127.0.0.1:{PORT}",
25         "latency.com": "127.0.0.1:{PORT}"
26       }
27     }),
28     "addHost() / removeHost()": macros.http.assertDynamicProxy({
29       hostnameOnly: true,
30       routes: {
31         "static.com":  "127.0.0.1:{PORT}",
32         "removed.com": "127.0.0.1:{PORT}"
33       }
34     }, {
35       add: [{ host: 'dynamic1.com', target: '127.0.0.1:' }],
36       drop: ['removed.com']
37     }),
38     "using RegExp": macros.http.assertProxiedToRoutes({
39       routes: {
40         "foo.com": "127.0.0.1:{PORT}",
41         "bar.com": "127.0.0.1:{PORT}",
42         "baz.com/taco": "127.0.0.1:{PORT}",
43         "pizza.com/taco/muffins": "127.0.0.1:{PORT}",
44         "blah.com/me": "127.0.0.1:{PORT}/remapped",
45         "bleh.com/remap/this": "127.0.0.1:{PORT}/remap/remapped",
46         "test.com/double/tap": "127.0.0.1:{PORT}/remap"
47       }
48     }),
49     "using hostnameOnly": macros.http.assertProxiedToRoutes({
50       hostnameOnly: true,
51       routes: {
52         "foo.com": "127.0.0.1:{PORT}",
53         "bar.com": "127.0.0.1:{PORT}"
54       }
55     }),
56     "using pathnameOnly": macros.http.assertProxiedToRoutes({
57       pathnameOnly: true,
58       routes: {
59         "/foo": "127.0.0.1:{PORT}",
60         "/bar": "127.0.0.1:{PORT}",
61         "/pizza": "127.0.0.1:{PORT}"
62       }
63     }),
64     "using a routing file": macros.http.assertProxiedToRoutes({
65       filename: routeFile,
66       routes: {
67         "foo.com": "127.0.0.1:{PORT}",
68         "bar.com": "127.0.0.1:{PORT}"
69       }
70     }, {
71       "after the file has been modified": {
72         topic: function () {
73           var config = JSON.parse(fs.readFileSync(routeFile, 'utf8')),
74               protocol = helpers.protocols.proxy,
75               port = helpers.nextPort,
76               that = this;
77
78           config.router['dynamic.com'] = "127.0.0.1:" + port;
79           fs.writeFileSync(routeFile, JSON.stringify(config));
80
81           async.parallel([
82             function waitForRoutes(next) {
83               that.proxyServer.on('routes', next);
84             },
85             async.apply(
86               helpers.http.createServer,
87               {
88                 port: port,
89                 output: 'hello from dynamic.com'
90               }
91             )
92           ], function () {
93             request({
94               uri: protocol + '://127.0.0.1:' + that.port,
95               headers: {
96                 host: 'dynamic.com'
97               }
98             }, that.callback);
99           });
100         },
101         "should receive 'hello from dynamic.com'": function (err, res, body) {
102           assert.equal(body, 'hello from dynamic.com');
103         }
104       }
105     })
106   }
107 }).export(module);