platform/test/server.dart

118 lines
3.4 KiB
Dart
Raw Normal View History

2016-09-18 01:35:16 +00:00
import 'dart:async';
import 'dart:io';
2016-07-06 13:33:40 +00:00
import 'package:angel_framework/angel_framework.dart' as server;
2016-09-03 12:34:01 +00:00
import 'package:angel_websocket/cli.dart' as client;
import 'package:angel_websocket/server.dart';
import 'package:json_god/json_god.dart' as god;
import 'package:test/test.dart';
import 'common.dart';
main() {
2016-07-06 13:33:40 +00:00
server.Angel app;
client.WebSocketClient clientApp;
client.WebSocketService clientTodos;
2016-09-18 01:35:16 +00:00
Stream<Map> customEventStream;
2016-09-18 02:53:58 +00:00
Stream<Map> customEventStream2;
WebSocket socket;
2016-09-18 01:35:16 +00:00
AngelWebSocket webSocket = new AngelWebSocket("/ws");
setUp(() async {
2016-07-06 13:33:40 +00:00
app = new server.Angel();
app.use("/real", new FakeService(), hooked: false);
2016-07-06 13:33:40 +00:00
app.use("/api/todos", new server.MemoryService<Todo>());
await app
.service("api/todos")
.create(new Todo(text: "Clean your room", when: "now"));
2016-09-18 01:35:16 +00:00
await app.configure(webSocket);
await app.configure((server.Angel app) async {
AngelWebSocket ws = app.container.make(AngelWebSocket);
ws.onConnection.listen((WebSocketContext socket) {
socket.onData.listen((data) {
print("Data: $data");
});
customEventStream = socket.on["custom"];
});
});
2016-09-18 02:53:58 +00:00
await app.configure(new Custom2Controller());
await app.configure(startTestServer);
socket = await WebSocket.connect(app.properties["ws_url"]);
2016-07-06 13:33:40 +00:00
clientApp = new client.WebSocketClient(app.properties["ws_url"]);
await clientApp.connect();
2016-09-18 02:53:58 +00:00
customEventStream2 = clientApp.on["custom2"];
2016-07-06 13:33:40 +00:00
clientTodos = clientApp.service("api/todos", type: Todo);
});
tearDown(() async {
await app.httpServer.close(force: true);
});
test("find all real-time services", () {
2016-09-18 01:35:16 +00:00
print(webSocket.servicesAlreadyWired);
expect(webSocket.servicesAlreadyWired, equals(["api/todos"]));
});
test("index", () async {
var action = new WebSocketAction(eventName: "api/todos::index");
socket.add(god.serialize(action));
2016-07-06 13:33:40 +00:00
String json = await socket.first;
print(json);
2016-09-18 02:53:58 +00:00
WebSocketEvent e = god.deserialize(json, outputType: WebSocketEvent);
2016-07-06 13:33:40 +00:00
expect(e.eventName, equals("api/todos::indexed"));
expect(e.data[0]["when"], equals("now"));
});
test("create", () async {
var todo = new Todo(text: "Finish the Angel framework", when: "2016");
clientTodos.create(todo);
var all = await clientTodos.onAllEvents.first;
var e = await clientTodos.onCreated.first;
print(god.serialize(e));
expect(all, equals(e));
expect(e.eventName, equals("created"));
expect(e.data is Todo, equals(true));
expect(e.data.text, equals(todo.text));
expect(e.data.when, equals(todo.when));
});
2016-09-18 01:35:16 +00:00
test("custom event via controller", () async {
clientApp.send("custom", {"hello": "world"});
var data = await customEventStream.first;
2016-09-18 02:53:58 +00:00
expect(data["hello"], equals("world"));
});
test("custom event via ws controller", () async {
clientApp.send("custom2", {"hello": "world"});
var data = customEventStream2.first;
print("Received data from server: $data");
2016-09-18 01:35:16 +00:00
});
}
2016-07-06 13:33:40 +00:00
class FakeService extends server.Service {}
2016-09-18 02:53:58 +00:00
@server.Expose("/custom2")
class Custom2Controller extends WebSocketController {
@override
Future onConnect(WebSocketContext socket) async {
print(
"Got a WS connection from session #${socket.requestContext.session.id}!");
}
@ExposeWs("custom2")
void sayFoo(WebSocketContext socket, server.RequestContext req, AngelWebSocket ws) {
socket.send("custom2", {"franken": "stein"});
}
}