-
Notifications
You must be signed in to change notification settings - Fork 0
/
mytest.js
128 lines (110 loc) · 3.07 KB
/
mytest.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
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
var uvhttp = require('./');
var http = require('http');
var tap = require('tap');
var exit = require('exit');
var myhost = "0.0.0.0",
myport = 8080;
var httpServer = uvhttp.newHTTPServer();
httpServer.bindAddr(myhost, myport, 111);
httpServer.staticPath("/static", 200, "Static content from /static path\n");
httpServer.pathCB("/test", function(r) {
r.res(200, 'Response from /test Javascript callback\n');
});
httpServer.pathCB("/test2", function(r) {
setTimeout(function() {
r.res(200, 'Response from /test2 Javascript callback\n');
}, 0);
});
httpServer.pathCB("/stop", function(r) {
r.res(200, '/stop callback, stopping server\n');
console.log('/stop callback, stopping server (in the next tick)');
setTimeout(function() {
console.log('/stop callback, stopping server NOW');
exit();
}, 0);
});
httpServer.staticPath("/", 200, "Static content from root path\n");
console.log("HTTP server listening to port: " + myport)
tap.test('static path test', function(t) {
// thanks: http://davidwalsh.name/nodejs-http-request
var contents='';
http.get({
host: 'localhost',
path: '/static',
port: 8080
}, function(resp) {
resp.on('data', function(data) {
console.log('got data: ' + data);
contents += data;
});
resp.on('end', function() {
console.log('got end, total contents: ' + contents);
t.equal(contents, "Static content from /static path\n", '/static contents');
t.end();
})
});
});
tap.test('root path test', function(t) {
// thanks: http://davidwalsh.name/nodejs-http-request
var contents='';
http.get({
host: 'localhost',
path: '/',
port: 8080
}, function(resp) {
resp.on('data', function(data) {
console.log('got data: ' + data);
contents += data;
});
resp.on('end', function() {
console.log('got end, total contents: ' + contents);
t.equal(contents, "Static content from root path\n", 'root contents');
t.end();
})
});
});
tap.test('Immediate Javascript callback test', function(t) {
var contents='';
http.get({
host: 'localhost',
path: '/test',
port: 8080
}, function(resp) {
resp.on('data', function(data) {
console.log('got data: ' + data);
contents += data;
});
resp.on('end', function() {
console.log('got end, total contents: ' + contents);
t.equal(contents, 'Response from /test Javascript callback\n', '/test2 contents');
t.end();
})
});
});
tap.test('Javascript callback in the next tick', function(t) {
var contents='';
http.get({
host: 'localhost',
path: '/test2',
port: 8080
}, function(resp) {
resp.on('data', function(data) {
console.log('got data: ' + data);
contents += data;
});
resp.on('end', function() {
console.log('got end, total contents: ' + contents);
t.equal(contents, 'Response from /test2 Javascript callback\n', '/test2 contents');
t.end();
})
});
});
tap.test('stop', function(t) {
http.get({
host: 'localhost',
path: '/stop',
port: 8080
});
t.assert(true);
t.end();
});