-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy path_server_test.js
More file actions
85 lines (66 loc) · 2.07 KB
/
_server_test.js
File metadata and controls
85 lines (66 loc) · 2.07 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
// Copyright (c) 2012 Titanium I.T. LLC. All rights reserved. See LICENSE.txt for details.
(function() {
"use strict";
var Server = require("./server.js");
var fs = require("fs");
var http = require("http");
var async = require("async");
var assert = require("_assert");
var io = require("socket.io-client");
var ClientPointerEvent = require("../shared/client_pointer_event.js");
var ServerPointerEvent = require("../shared/server_pointer_event.js");
var CONTENT_DIR = "generated/test/";
var NOT_FOUND_PAGE = "test404.html";
var PORT = 5020;
var INDEX_PAGE = "index.html";
var INDEX_PAGE_CONTENTS = "This is the index page.";
describe("Server", function() {
var server;
beforeEach(function(done) {
fs.writeFile(CONTENT_DIR + INDEX_PAGE, INDEX_PAGE_CONTENTS, done);
});
afterEach(function(done) {
fs.unlink(CONTENT_DIR + INDEX_PAGE, done);
});
beforeEach(function(done) {
server = new Server();
server.start(CONTENT_DIR, NOT_FOUND_PAGE, PORT, done);
});
afterEach(function(done) {
server.stop(done);
});
it("serves HTML", function(done) {
http.get("http://localhost:" + PORT, function(response) {
var receivedData = "";
response.setEncoding("utf8");
response.on("data", function(chunk) {
receivedData += chunk;
});
response.on("error", function(err) {
assert.fail(err);
});
response.on("end", function() {
assert.equal(receivedData, INDEX_PAGE_CONTENTS);
done();
});
});
});
it("services real-time events", function(done) {
var emitter = createSocket();
var receiver = createSocket();
var clientEvent = new ClientPointerEvent(100, 200);
receiver.on(ServerPointerEvent.EVENT_NAME, function(data) {
assert.deepEqual(data, clientEvent.toServerEvent(emitter.id).toSerializableObject());
setTimeout(done, 10);
});
emitter.emit(clientEvent.name(), clientEvent.toSerializableObject());
});
function createSocket() {
return io("http://localhost:" + PORT);
}
function closeSocket(socket, callback) {
socket.disconnect();
callback();
}
});
}());