-
Notifications
You must be signed in to change notification settings - Fork 24
/
http-connection.test.js
91 lines (79 loc) · 2.84 KB
/
http-connection.test.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
require('requirish')._(module);
require('should');
var HttpConnection = require('lib/net').HttpConnection;
var isNode = (typeof window === 'undefined');
describe('HttpConnection', function () {
var conn, protocol, hostname, port;
this.timeout(30000);
before(function () {
protocol = isNode ? undefined : 'https';
hostname = isNode ? 'localhost' : window.location.hostname;
port = isNode ? require('./http-server').port : window.location.port;
});
describe('#init()', function () {
it('should return an instance', function (done) {
conn = new HttpConnection();
conn.should.be.ok;
conn.should.be.a.Object;
conn.options.should.have.properties({path: 'http://localhost:80/apiw1'});
done();
})
});
describe('#read()', function () {
it('should notify error back on reading', function (done) {
conn = new HttpConnection({protocol: protocol, host: "nohost"});
conn.connect();
conn.read(function (e) {
console.log(e);
done();
});
})
});
describe('#init(host, port)', function () {
it('should return an instance with given host and port', function (done) {
conn = new HttpConnection({host: "0.0.0.0", port: port});
conn.options.should.have.properties({path: '/apiw1'});
done();
})
});
describe('#connect()', function () {
it('should call back after HttpConnection', function (done) {
conn = new HttpConnection({host: "0.0.0.0", port: port});
conn.connect(done);
})
});
describe('#write()', function () {
it('should call back after write', function (done) {
conn = new HttpConnection({host: "0.0.0.0", port: port});
conn.connect();
conn.write(new Buffer(">string<"), done);
})
});
describe('#read()', function () {
it('should call back after read', function (done) {
conn = new HttpConnection({protocol: protocol, host: hostname, port: port});
conn.connect();
conn.write(new Buffer("test post method"));
conn.read(function (ex, data) {
if (ex) {
console.err(ex);
}
data.should.be.ok;
console.log(data.toString());
done();
});
})
});
after(function () {
if (isNode) {
require('./http-server').shutdown();
} else {
conn = new HttpConnection({protocol: protocol, host: hostname, port: port});
conn.connect(function () {
conn.write(new Buffer("shutdown"));
conn.read(function (ex, data) {
});
});
}
});
});