2016-12-10 17:05:31 +00:00
|
|
|
import 'dart:async';
|
2016-12-10 18:11:27 +00:00
|
|
|
import 'dart:convert';
|
2016-12-10 17:05:31 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_client/io.dart' as client;
|
2016-12-10 18:11:27 +00:00
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2017-03-25 04:12:21 +00:00
|
|
|
import 'package:mock_request/mock_request.dart';
|
2016-12-10 18:11:27 +00:00
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
|
|
|
final Uuid _uuid = new Uuid();
|
|
|
|
|
|
|
|
Future<TestClient> connectTo(Angel app,
|
2016-12-19 04:51:21 +00:00
|
|
|
{Map initialSession, bool saveSession: false}) async {
|
2016-12-10 18:11:27 +00:00
|
|
|
TestClient client;
|
|
|
|
var path = '/${_uuid.v1()}/${_uuid.v1()}/${_uuid.v1()}';
|
|
|
|
|
|
|
|
if (saveSession) {
|
|
|
|
app
|
|
|
|
..get(path, (RequestContext req, res) async {
|
|
|
|
client._session = req.session;
|
|
|
|
|
|
|
|
if (initialSession != null) {
|
|
|
|
req.session.addAll(initialSession);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
..post(path, (RequestContext req, res) async {
|
|
|
|
client._session = req.session..addAll(req.body);
|
|
|
|
})
|
|
|
|
..patch(path, (RequestContext req, res) async {
|
|
|
|
req.body['keys'].forEach(req.session.remove);
|
|
|
|
client._session = req.session;
|
|
|
|
});
|
|
|
|
}
|
2016-12-10 17:05:31 +00:00
|
|
|
|
|
|
|
final server = await app.startServer();
|
2016-12-10 18:11:27 +00:00
|
|
|
final url = 'http://${server.address.address}:${server.port}';
|
|
|
|
client = new TestClient(server, url);
|
|
|
|
|
|
|
|
if (saveSession) {
|
|
|
|
await client.client.get('$url$path');
|
|
|
|
client._sessionPath = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return client;
|
2016-12-10 17:05:31 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 04:12:21 +00:00
|
|
|
Future<MockHttpResponse> mock(Angel app, String method, Uri uri,
|
|
|
|
{Iterable<Cookie> cookies: const [],
|
|
|
|
Map<String, dynamic> headers: const {}}) async {
|
|
|
|
var rq = new MockHttpRequest(method, uri);
|
|
|
|
rq.cookies.addAll(cookies ?? []);
|
|
|
|
headers.forEach(rq.headers.add);
|
|
|
|
await rq.close();
|
|
|
|
await app.handleRequest(rq);
|
|
|
|
return rq.response;
|
|
|
|
}
|
|
|
|
|
2016-12-10 18:11:27 +00:00
|
|
|
/// Interacts with an Angel server.
|
|
|
|
class TestClient extends client.Rest {
|
2016-12-10 17:05:31 +00:00
|
|
|
final HttpServer server;
|
2016-12-10 18:11:27 +00:00
|
|
|
HttpSession _session;
|
|
|
|
String _sessionPath;
|
|
|
|
|
|
|
|
/// Returns a pointer to the current session.
|
|
|
|
HttpSession get session => _session;
|
|
|
|
|
|
|
|
TestClient(this.server, String path) : super(path);
|
|
|
|
|
|
|
|
/// Adds data to the [session].
|
|
|
|
Future addToSession(Map data) => post(_sessionPath, body: data);
|
2016-12-10 17:05:31 +00:00
|
|
|
|
2016-12-10 18:11:27 +00:00
|
|
|
/// Removes data from the [session].
|
|
|
|
Future removeFromSession(List<String> keys) => patch(_sessionPath,
|
|
|
|
body: JSON.encode({'keys': keys}),
|
|
|
|
headers: {HttpHeaders.CONTENT_TYPE: ContentType.JSON.mimeType});
|
2016-12-10 17:05:31 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future close() async {
|
|
|
|
if (server != null) {
|
|
|
|
await server.close(force: true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|