aboutsummaryrefslogtreecommitdiffstats
path: root/test/tests/TestServer.hx
blob: b02bb90fa648f49e2d24dc2a65ffb5961d3ff286 (plain) (blame)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package test.tests;

import Types.WsEvent;
import Types.WsEventType;
import haxe.Json;
import haxe.Timer;
import js.lib.Promise;
import js.node.Http;
import js.npm.ws.WebSocket.WebSocket;
import server.Main;
import utest.Assert;
import utest.Async;
import utest.Test;

using StringTools;

@:access(server)
class TestServer extends Test {
	@:timeout(500)
	function testBadRequests(async:Async) {
		final server = new Main({loadState: false});
		server.onServerInited = () -> {
			final url = 'http://${server.localIp}:${server.port}';
			request('$url/你好,世界!@$^&*)_+-=', data -> {
				Assert.equals("File 你好,世界!@$^&*)_ -= not found.", data);
			});
			request('$url/Привет%00мир!', data -> {
				Assert.equals("File Приветмир! not found.", data);
			});
			request('$url/Ы%ы%00ы!', data -> {
				Assert.equals("<!DOCTYPE html>", splitLines(data)[0]);
			});
			request('$url/video/skins/default.php?dir_inc=/etc/passwd%00', data -> {
				var line = "File video/skins/default.php not found.";
				if (Sys.systemName() == "Windows") line = line.replace("/", "\\");
				Assert.equals(line, data);
			});
			request('$url/%20', data -> {
				Assert.equals("<!DOCTYPE html>", splitLines(data)[0]);
			});
			request('$url/build/../../server.js', data -> {
				Assert.equals("File server.js not found.", data);
			});
			request('$url/?meh', data -> {
				Assert.equals("<!DOCTYPE html>", splitLines(data)[0]);
				async.done();
			});
		}
	}

	function splitLines(text:String):Array<String> {
		return ~/\r?\n/g.split(text);
	}

	function request(url:String, onComplete:(data:String) -> Void):Void {
		Http.get(url, r -> {
			r.setEncoding("utf8");
			final data = new StringBuf();
			r.on("data", chunk -> data.add(chunk));
			r.on("end", _ -> onComplete(data.toString()));
		}).on("error", e -> trace(e));
	}

	@:timeout(2000)
	function testDoubleSkip(async:Async) {
		final server = new Main({loadState: false});
		server.onServerInited = () -> {
			final client = new FakeClient(server.localIp, server.port);
			var client2:FakeClient = null;

			client.message().then((data) -> {
				Assert.equals(Connected, data.type);
				Assert.equals(1, server.clients.length);
				client.name = data.connected.clientName;

				client2 = new FakeClient(server.localIp, server.port);
				client2.message().then(data -> {
					Assert.equals(Connected, data.type);
					client2.name = data.connected.clientName;
				});

				client.message();
			}).then(data -> {
				Assert.equals(UpdateClients, data.type);
				Assert.equals(2, server.clients.length);
				client.send({
					type: AddVideo,
					addVideo: {
						item: {
							url: "url1",
							title: "1",
							author: "",
							duration: 30,
							isTemp: true,
							playerType: RawType,
							doCache: false
						},
						atEnd: true
					}
				});
			}).then(data -> {
				Assert.equals(AddVideo, data.type);
				client2.send({type: VideoLoaded});
				client.send({type: VideoLoaded});
			}).then(data -> {
				Assert.equals(VideoLoaded, data.type);
				client.send({
					type: SetLeader,
					setLeader: {
						clientName: client.name
					},
				});
			}).then(data -> {
				Assert.equals(SetLeader, data.type);
				client.send({
					type: SetTime,
					setTime: {
						time: 30
					},
				});
				// SetTime will not answer to leader request
				// wait for second client data
				client2.messages(2);
			}).then(arr -> {
				Assert.equals(SetLeader, arr[0].type);
				Assert.equals(SetTime, arr[1].type);
				client.send({
					type: SetLeader,
					setLeader: {
						clientName: ""
					}
				});
			}).then(data -> {
				Assert.equals(SetLeader, data.type);
				Timer.delay(() -> {
					client2.send({type: GetTime});
					client2.send({
						type: AddVideo,
						addVideo: {
							item: {
								url: "url2",
								title: "2",
								author: "",
								duration: 30,
								isTemp: true,
								playerType: RawType,
								doCache: false
							},
							atEnd: true
						}
					});
				}, 50);
				client.send({type: GetTime});
			}).then(data -> {
				Assert.equals(AddVideo, data.type);
				client.message(); // single skip after 1s
			}).then(data -> {
				Assert.equals(SkipVideo, data.type);
				Assert.equals(1, server.videoList.length);
				Assert.equals("url2", server.videoList.getItem(0).url);
				Timer.delay(() -> {
					client.send({
						type: Message,
						message: {
							clientName: "",
							text: "ok"
						}
					});
				}, 50);
				client.message();
			}).then(data -> {
				Assert.equals(Message, data.type);
				async.done();
			});
		}
	}
}

class FakeClient {
	public final ws:WebSocket;
	public var name = "Unknown";

	public function new(ip:String, port:Int) {
		ws = new WebSocket('ws://$ip:$port');
	}

	public function open() {
		final promise = new Promise((resolve, reject) -> {
			ws.once("open", _ -> resolve(_));
			ws.once("error", err -> reject(err));
		});
		return promise;
	}

	// Warning: promise chain skips several fast sent messages
	public function message() {
		final promise = new Promise((resolve, reject) -> {
			ws.once("message", data -> {
				final data:WsEvent = Json.parse(data);
				resolve(data);
			});
		});
		return promise;
	}

	public function messages(count:Int) {
		var arr:Array<WsEvent> = [];
		final promise = new Promise((resolve, reject) -> {
			function onMessage(data:String):Void {
				final data:WsEvent = Json.parse(data);
				arr.push(data);
				count--;
				if (count == 0) {
					ws.off("message", onMessage);
					resolve(arr);
				}
			}
			ws.on("message", onMessage);
		});
		return promise;
	}

	public function send(data:WsEvent) {
		ws.send(Json.stringify(data), null);
		return message();
	}

	public function wait(ms:Int) {
		final promise = new Promise((resolve, reject) -> {
			Timer.delay(() -> resolve(null), ms);
		});
		return promise;
	}
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage