curl-server.js
2.84 KB
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
var config = {
IP: require('ip').address(),
port: 8765,
servers: 2,
dir: __dirname
}
var panic = require('panic-server');
panic.server().listen(config.port);
var clients = panic.clients;
var manager = require('panic-manager')();
manager.start({
clients: Array(config.servers).fill().map(function(u, i){
return {
type: 'node',
port: config.port + (i + 1)
}
}),
panic: 'http://' + config.IP + ':' + config.port
});
var servers = clients.filter('Node.js');
var alice = servers.pluck(1);
var bob = servers.excluding(alice).pluck(1);
describe("Server to server sync", function(){
this.timeout(5000);
it("Servers have joined!", function(){
return servers.atLeast(config.servers);
});
it("Start GUN Bob server.", function(){
return bob.run(function(test){
test.async();
var express = require('express');
var bodyParser = require('body-parser');
var Gun = require('gun');
var app = express();
app.use(Gun.serve)
app.use(bodyParser.json())
app.post('/foo', function(req, res) {
gun.get('bar').put(req.body)
res.sendStatus(200)
});
var server = app.listen(8082, function(){ test.done() })
var gun = Gun({peers: 'http://localhost:8081/gun', web: server})
gun.get('bar').on(function(data, key){
console.log('bob', data, key)
})
}, {i: 1, config: config});
});
it("Start GUN Alice server.", function(){
return alice.run(function(test){
test.async();
var express = require('express');
var bodyParser = require('body-parser');
var Gun = require('gun');
var app = express()
app.use(Gun.serve)
var server = app.listen(8081, function(){ test.done() })
var gun = Gun({peers: 'http://localhost:8082/gun', web: server})
gun.get('bar').on(function(data, key){
console.log('alice', data, key)
global.DATA = data;
})
}, {i: 1, config: config});
});
it("Curl Bob!", function(){
var reply = require('child_process').execSync("curl --request POST "
+ "--url http://localhost:8082/foo "
+ "--header 'content-type: application/json' "
+ "--data '"+JSON.stringify({bar: "FOOBAR"})+"'");
console.log("REPLY:", reply.toString());
if(reply.toString().indexOf("err") >= 0){
console.log(reply.toString());
throw new Error("Server did not like the request!");
}
return;
});
it("Did Alice get it?", function(){
return alice.run(function(test){
test.async();
setTimeout(function(){
console.log("does Alice have it?", global.DATA);
if(!global.DATA){
console.log("no data!");
return;
}
test.done();
}, 1000);
}, {i: 1, config: config});
});
it("All finished!", function(done){
console.log("Done! Cleaning things up...");
setTimeout(function(){
done();
},1000);
});
after("Everything shut down.", function(){
return servers.run(function(){
process.exit();
});
});
});