fix(style): Format and lint test helpers
This commit is contained in:
parent
f49eb66bb7
commit
9cb6346c4d
@ -5,6 +5,8 @@ void initLogger() {
|
||||
Logger.root.level = Level.ALL;
|
||||
Logger.root.onRecord.listen((record) {
|
||||
// ignore: avoid_print
|
||||
print('[${record.level.name}] (${record.loggerName}) ${record.time}: ${record.message}');
|
||||
print(
|
||||
'[${record.level.name}] (${record.loggerName}) ${record.time}: ${record.message}',
|
||||
);
|
||||
});
|
||||
}
|
||||
|
@ -29,7 +29,14 @@ class TestingManagerHolder {
|
||||
allowPlainAuth: true,
|
||||
);
|
||||
|
||||
Future<XMLNode> _sendStanza(stanza, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async {
|
||||
Future<XMLNode> _sendStanza(
|
||||
stanza, {
|
||||
StanzaFromType addFrom = StanzaFromType.full,
|
||||
bool addId = true,
|
||||
bool awaitable = true,
|
||||
bool encrypted = false,
|
||||
bool forceEncryption = false,
|
||||
}) async {
|
||||
return XMLNode.fromString('<iq />');
|
||||
}
|
||||
|
||||
|
@ -1,28 +1,37 @@
|
||||
import 'package:moxxmpp/moxxmpp.dart';
|
||||
|
||||
bool compareXMLNodes(XMLNode actual, XMLNode expectation, { bool ignoreId = true}) {
|
||||
bool compareXMLNodes(
|
||||
XMLNode actual,
|
||||
XMLNode expectation, {
|
||||
bool ignoreId = true,
|
||||
}) {
|
||||
// Compare attributes
|
||||
if (expectation.tag != actual.tag) return false;
|
||||
|
||||
final attributesEqual = expectation.attributes.keys.every((key) {
|
||||
// Ignore the stanza ID
|
||||
if (key == 'id' && ignoreId) return true;
|
||||
// Ignore the stanza ID
|
||||
if (key == 'id' && ignoreId) return true;
|
||||
|
||||
return actual.attributes[key] == expectation.attributes[key];
|
||||
return actual.attributes[key] == expectation.attributes[key];
|
||||
});
|
||||
if (!attributesEqual) return false;
|
||||
|
||||
final actualAttributeLength = !ignoreId ? actual.attributes.length : (
|
||||
actual.attributes.containsKey('id') ? actual.attributes.length - 1 : actual.attributes.length
|
||||
);
|
||||
final expectedAttributeLength = !ignoreId ? expectation.attributes.length : (
|
||||
expectation.attributes.containsKey('id') ? expectation.attributes.length - 1 : expectation.attributes.length
|
||||
);
|
||||
final actualAttributeLength = !ignoreId
|
||||
? actual.attributes.length
|
||||
: (actual.attributes.containsKey('id')
|
||||
? actual.attributes.length - 1
|
||||
: actual.attributes.length);
|
||||
final expectedAttributeLength = !ignoreId
|
||||
? expectation.attributes.length
|
||||
: (expectation.attributes.containsKey('id')
|
||||
? expectation.attributes.length - 1
|
||||
: expectation.attributes.length);
|
||||
if (actualAttributeLength != expectedAttributeLength) return false;
|
||||
|
||||
if (expectation.innerText() != '' && actual.innerText() != expectation.innerText()) return false;
|
||||
if (expectation.innerText() != '' &&
|
||||
actual.innerText() != expectation.innerText()) return false;
|
||||
|
||||
return expectation.children.every((childe) {
|
||||
return actual.children.any((childa) => compareXMLNodes(childa, childe));
|
||||
return actual.children.any((childa) => compareXMLNodes(childa, childe));
|
||||
});
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ abstract class ExpectationBase {
|
||||
|
||||
/// Literally compare the input with the expectation
|
||||
class StringExpectation extends ExpectationBase {
|
||||
StringExpectation(String expectation, String response) : super(expectation, response);
|
||||
StringExpectation(super.expectation, super.response);
|
||||
|
||||
@override
|
||||
bool matches(String input) => input == expectation;
|
||||
@ -33,8 +33,12 @@ class StringExpectation extends ExpectationBase {
|
||||
|
||||
///
|
||||
class StanzaExpectation extends ExpectationBase {
|
||||
StanzaExpectation(String expectation, String response, {this.ignoreId = false, this.adjustId = false }) : super(expectation, response);
|
||||
|
||||
StanzaExpectation(
|
||||
super.expectation,
|
||||
super.response, {
|
||||
this.ignoreId = false,
|
||||
this.adjustId = false,
|
||||
});
|
||||
final bool ignoreId;
|
||||
final bool adjustId;
|
||||
|
||||
@ -52,7 +56,9 @@ class StanzaExpectation extends ExpectationBase {
|
||||
List<ExpectationBase> buildAuthenticatedPlay(ConnectionSettings settings) {
|
||||
assert(settings.allowPlainAuth, 'SASL PLAIN must be allowed');
|
||||
|
||||
final plain = base64.encode(utf8.encode('\u0000${settings.jid.local}\u0000${settings.password}'));
|
||||
final plain = base64.encode(
|
||||
utf8.encode('\u0000${settings.jid.local}\u0000${settings.password}'),
|
||||
);
|
||||
return [
|
||||
StringExpectation(
|
||||
"<stream:stream xmlns='jabber:client' version='1.0' xmlns:stream='http://etherx.jabber.org/streams' to='${settings.jid.domain}' xml:lang='en'>",
|
||||
@ -68,14 +74,14 @@ List<ExpectationBase> buildAuthenticatedPlay(ConnectionSettings settings) {
|
||||
<mechanism>PLAIN</mechanism>
|
||||
</mechanisms>
|
||||
</stream:features>''',
|
||||
),
|
||||
StringExpectation(
|
||||
"<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>$plain</auth>",
|
||||
'<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl" />'
|
||||
),
|
||||
StringExpectation(
|
||||
"<stream:stream xmlns='jabber:client' version='1.0' xmlns:stream='http://etherx.jabber.org/streams' to='${settings.jid.domain}' xml:lang='en'>",
|
||||
'''
|
||||
),
|
||||
StringExpectation(
|
||||
"<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>$plain</auth>",
|
||||
'<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl" />',
|
||||
),
|
||||
StringExpectation(
|
||||
"<stream:stream xmlns='jabber:client' version='1.0' xmlns:stream='http://etherx.jabber.org/streams' to='${settings.jid.domain}' xml:lang='en'>",
|
||||
'''
|
||||
<stream:stream
|
||||
xmlns="jabber:client"
|
||||
version="1.0"
|
||||
@ -88,30 +94,36 @@ List<ExpectationBase> buildAuthenticatedPlay(ConnectionSettings settings) {
|
||||
</bind>
|
||||
</stream:features>
|
||||
''',
|
||||
),
|
||||
StanzaExpectation(
|
||||
'<iq xmlns="jabber:client" type="set" id="a"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/></iq>',
|
||||
'<iq xmlns="jabber:client" type="result" id="a"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>${settings.jid.toBare()}/MU29eEZn</jid></bind></iq>',
|
||||
ignoreId: true,
|
||||
),
|
||||
StanzaExpectation(
|
||||
"<presence xmlns='jabber:client' from='${settings.jid.toBare()}/MU29eEZn'><show>chat</show></presence>",
|
||||
'',
|
||||
),
|
||||
),
|
||||
StanzaExpectation(
|
||||
'<iq xmlns="jabber:client" type="set" id="a"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/></iq>',
|
||||
'<iq xmlns="jabber:client" type="result" id="a"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>${settings.jid.toBare()}/MU29eEZn</jid></bind></iq>',
|
||||
ignoreId: true,
|
||||
),
|
||||
StanzaExpectation(
|
||||
"<presence xmlns='jabber:client' from='${settings.jid.toBare()}/MU29eEZn'><show>chat</show></presence>",
|
||||
'',
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
class StubTCPSocket extends BaseSocketWrapper { // Request -> Response(s)
|
||||
class StubTCPSocket extends BaseSocketWrapper {
|
||||
// Request -> Response(s)
|
||||
StubTCPSocket(this._play);
|
||||
|
||||
StubTCPSocket.authenticated(ConnectionSettings settings, List<ExpectationBase> play) : _play = [
|
||||
...buildAuthenticatedPlay(settings),
|
||||
...play,
|
||||
];
|
||||
StubTCPSocket.authenticated(
|
||||
ConnectionSettings settings,
|
||||
List<ExpectationBase> play,
|
||||
) : _play = [
|
||||
...buildAuthenticatedPlay(settings),
|
||||
...play,
|
||||
];
|
||||
|
||||
int _state = 0;
|
||||
final StreamController<String> _dataStream = StreamController<String>.broadcast();
|
||||
final StreamController<XmppSocketEvent> _eventStream = StreamController<XmppSocketEvent>.broadcast();
|
||||
final StreamController<String> _dataStream =
|
||||
StreamController<String>.broadcast();
|
||||
final StreamController<XmppSocketEvent> _eventStream =
|
||||
StreamController<XmppSocketEvent>.broadcast();
|
||||
final List<ExpectationBase> _play;
|
||||
String? lastId;
|
||||
|
||||
@ -122,22 +134,24 @@ class StubTCPSocket extends BaseSocketWrapper { // Request -> Response(s)
|
||||
Future<bool> secure(String domain) async => true;
|
||||
|
||||
@override
|
||||
Future<bool> connect(String domain, { String? host, int? port }) async => true;
|
||||
Future<bool> connect(String domain, {String? host, int? port}) async => true;
|
||||
|
||||
@override
|
||||
Stream<String> getDataStream() => _dataStream.stream.asBroadcastStream();
|
||||
@override
|
||||
Stream<XmppSocketEvent> getEventStream() => _eventStream.stream.asBroadcastStream();
|
||||
Stream<XmppSocketEvent> getEventStream() =>
|
||||
_eventStream.stream.asBroadcastStream();
|
||||
|
||||
/// Let the "connection" receive [data].
|
||||
void injectRawXml(String data) {
|
||||
// ignore: avoid_print
|
||||
print('<== $data');
|
||||
_dataStream.add(data);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(Object? object, { String? redact }) {
|
||||
var str = object as String;
|
||||
void write(Object? object, {String? redact}) {
|
||||
var str = object! as String;
|
||||
// ignore: avoid_print
|
||||
print('==> $str');
|
||||
|
||||
@ -148,7 +162,7 @@ class StubTCPSocket extends BaseSocketWrapper { // Request -> Response(s)
|
||||
|
||||
final expectation = _play[_state];
|
||||
|
||||
// TODO: Implement an XML matcher
|
||||
// TODO(Unknown): Implement an XML matcher
|
||||
if (str.startsWith("<?xml version='1.0'?>")) {
|
||||
str = str.substring(21);
|
||||
}
|
||||
@ -174,12 +188,13 @@ class StubTCPSocket extends BaseSocketWrapper { // Request -> Response(s)
|
||||
if (expectation.adjustId) {
|
||||
final outputNode = XMLNode.fromString(response);
|
||||
|
||||
outputNode.attributes['id'] = inputNode.attributes['id']!;
|
||||
outputNode.attributes['id'] = inputNode.attributes['id'];
|
||||
response = outputNode.toXml();
|
||||
}
|
||||
}
|
||||
|
||||
print("<== $response");
|
||||
// ignore: avoid_print
|
||||
print('<== $response');
|
||||
_dataStream.add(response);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user