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 = {};
var statusCode = 200;
//print('\$method request to $action with Data: $data');
// Sync requests with timeout
if (data is Map<String, dynamic> && data['timeout'] is String) {
await Future.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] = <dynamic>[];
}
calledEndpoints[action]!.add(data);
if (api.containsKey(method) && api[method]!.containsKey(action)) {
res = api[method]![action](data);
if (res is Map && res.containsKey('errcode')) {
statusCode = 405;
}
} else if (method == 'PUT' &&
action.contains('/client/r0/sendToDevice/')) {
res = {};
} else if (method == 'GET' &&
action.contains('/client/r0/rooms/') &&
action.contains('/state/m.room.member/')) {
res = {'displayname': ''};
} else if (method == 'PUT' &&
action.contains(
'/client/r0/rooms/!1234%3AfakeServer.notExisting/send/')) {
res = {'event_id': '\$event${FakeMatrixApi.eventCounter++}'};
} else if (action.contains('/client/r0/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);
});