-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_server.js
90 lines (78 loc) · 2.77 KB
/
test_server.js
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
var _ = require('underscore');
var Percolator = require('./index').Percolator;
var BasicAuthenticateHelper = require('./index').BasicAuthenticateHelper;
var app = {
protocol : 'http',
resourcePath : '/api',
staticDir : __dirname + '/test/test_fixtures/static',
port : 8080
};
app.artists = {
1 : {"name" : "Neil Young", created : new Date()},
2 : {"name" : "Joe Strummer", created : new Date()}
};
app.teas = [{name : "Earl Grey"}, {name : "Orange Pekoe"}, {name : "Lemon Zinger"}, {name : "English Breakfast"}];
app.teas = _.map(app.teas, function(tea){ tea.created = new Date(); return tea; });
var server = new Percolator(app);
server.before(function(req, res, handler, cb){
req.started = new Date();
BasicAuthenticateHelper(req, res, handler, function(){
cb();
});
});
server.after(function(req, res, handler){
console.log(' <-- ', req.method, ' ', req.url, ' | duration: ' + (new Date() - req.started) + ' ms');
});
var resourceDir = __dirname + '/test/test_fixtures/resources';
server.routeDirectory(resourceDir, app.resourcePath, function(err){
console.log("routed resources in " + resourceDir);
server.connectMiddleware(function(req, res, done){
console.log("hello from connect. URL: ", req.url);
done();
});
server.route('/inside',
{ GET : function(req, res){
res.end("muahahah!");
}
});
server.route('/someProtectedPath', {
basicAuthenticate : function(username, password, req, res, cb){
// try to get the user here, based on cookie, Authentication header, etc
if (username === 'Pierre' && password === 'Collateur'){
return cb(null, {username : "Pierre", twitter_handle : "@Percolator"});
// user object will be available in req.authenticated in all methods
} else {
return cb(true); // Percolator will 401 for you
}
},
GET : function(req, res){
res.object({youAre : req.authenticated}).send();
}
});
server.route('/trypost', {
POST : function(request, response){
console.log('calling onJson');
request.onJson(function(error, input){
console.log('onJson callback invoked', arguments);
return response.object({onJson: arguments}).send();
});
}
});
if (err) {
console.log("Routing error");
console.log(err);
return;
}
server.on("response", function(data){
console.log("response");
console.log(data);
});
server.on("errorResponse", function(data){
console.log("error response");
console.log(data.req.method, data.req.url, data.type, data.message, data.detail);
});
server.listen(function(err){
if (err) {console.log(err);throw err;}
console.log('Percolator running on ' + server.port);
});
});