FakeMatrixApi constructor
FakeMatrixApi()
Implementation
FakeMatrixApi()
: super((request) async {
// Collect data from Request
var action = request.url.path;
if (request.url.path.contains('/_matrix')) {
action =
'${request.url.path.split('/_matrix').last}?${request.url.query}';
}
if (action.endsWith('?')) {
action = action.substring(0, action.length - 1);
}
if (action.endsWith('/')) {
action = action.substring(0, action.length - 1);
}
final method = request.method;
final dynamic data =
method == 'GET' ? request.url.queryParameters : request.body;
dynamic res = <String, Object?>{};
var statusCode = 200;
//print('\$method request to $action with Data: $data');
// Sync requests with timeout
if (data is Map<String, Object?> && data['timeout'] is String) {
await Future<void>.delayed(Duration(seconds: 5));
}
if (request.url.origin != 'https://fakeserver.notexisting') {
return Response(
'<html><head></head><body>Not found...</body></html>', 404);
}
// Call API
if (!calledEndpoints.containsKey(action)) {
calledEndpoints[action] = [];
}
calledEndpoints[action]!.add(data);
if (api.containsKey(method) && api[method]!.containsKey(action)) {
res = api[method]![action]?.call(data);
if (res is Map && res.containsKey('errcode')) {
statusCode = 405;
}
} else if (method == 'PUT' &&
action.contains('/client/v3/sendToDevice/')) {
res = <String, Object?>{};
} else if (method == 'GET' &&
action.contains('/client/v3/rooms/') &&
action.contains('/state/m.room.member/')) {
res = {'displayname': ''};
} else if (method == 'PUT' &&
action.contains(
'/client/v3/rooms/!1234%3AfakeServer.notExisting/send/')) {
res = {'event_id': '\$event${FakeMatrixApi.eventCounter++}'};
} else if (action.contains('/client/v3/sync')) {
res = {
'next_batch': DateTime.now().millisecondsSinceEpoch.toString
};
} else {
res = {
'errcode': 'M_UNRECOGNIZED',
'error': 'Unrecognized request'
};
statusCode = 405;
}
return Response.bytes(utf8.encode(json.encode(res)), statusCode);
});