Skip to content

Commit 1fd39b2

Browse files
committed
fixup! test: add tests for exploreDirectory method
1 parent 5eda27d commit 1fd39b2

File tree

1 file changed

+61
-5
lines changed

1 file changed

+61
-5
lines changed

test/core/discovery_test.dart

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,30 @@ final invalidTestDiscoveryUri =
1818
Uri.parse('$testUriScheme://[::2]/.well-known/wot');
1919
final directoryTestUri1 = Uri.parse('$testUriScheme://[::3]/.well-known/wot');
2020
final directoryTestThingsUri1 = Uri.parse('$testUriScheme://[::3]/things');
21+
final directoryTestUri2 = Uri.parse('$testUriScheme://[::4]/.well-known/wot');
22+
final directoryTestThingsUri2 = Uri.parse('$testUriScheme://[::4]/things');
2123

22-
const validTestTitle = 'Test TD';
24+
const validTestTitle1 = 'Test TD 1';
2325
const validTestThingDescription = '''
2426
{
2527
"@context": "https://www.w3.org/2022/wot/td/v1.1",
26-
"title": "$validTestTitle",
28+
"title": "$validTestTitle1",
2729
"security": "nosec_sc",
2830
"securityDefinitions": {
2931
"nosec_sc": {"scheme": "nosec"}
3032
}
3133
}
3234
''';
3335

36+
const validDirectoryTestTitle1 = 'Test TD 2';
3437
final directoryThingDescription1 = '''
3538
{
3639
"@context": [
3740
"https://www.w3.org/2022/wot/td/v1.1",
3841
"https://www.w3.org/2022/wot/discovery"
3942
],
4043
"@type": "ThingDirectory",
41-
"title": "$validTestTitle",
44+
"title": "$validDirectoryTestTitle1",
4245
"security": "nosec_sc",
4346
"securityDefinitions": {
4447
"nosec_sc": {"scheme": "nosec"}
@@ -55,6 +58,31 @@ final directoryThingDescription1 = '''
5558
}
5659
''';
5760

61+
const validDirectoryTestTitle2 = 'Test TD 2';
62+
final directoryThingDescription2 = '''
63+
{
64+
"@context": [
65+
"https://www.w3.org/2022/wot/td/v1.1",
66+
"https://www.w3.org/2022/wot/discovery"
67+
],
68+
"@type": "ThingDirectory",
69+
"title": "$validDirectoryTestTitle2",
70+
"security": "nosec_sc",
71+
"securityDefinitions": {
72+
"nosec_sc": {"scheme": "nosec"}
73+
},
74+
"properties": {
75+
"things": {
76+
"forms": [
77+
{
78+
"href": "$directoryTestThingsUri2"
79+
}
80+
]
81+
}
82+
}
83+
}
84+
''';
85+
5886
const invalidTestThingDescription = '"Hi there!"';
5987

6088
void main() {
@@ -70,7 +98,7 @@ void main() {
7098
final thingDescription =
7199
await wot.requestThingDescription(validTestDiscoveryUri);
72100

73-
expect(thingDescription.title, validTestTitle);
101+
expect(thingDescription.title, validTestTitle1);
74102
});
75103

76104
test(
@@ -107,11 +135,31 @@ void main() {
107135
var counter = 0;
108136
await for (final thingDescription in thingDiscoveryProcess) {
109137
counter++;
110-
expect(thingDescription.title, validTestTitle);
138+
expect(thingDescription.title, validTestTitle1);
111139
}
112140
expect(counter, 1);
113141
expect(thingDiscoveryProcess.done, true);
114142
});
143+
144+
test('should be able to handle invalid TDs during discovery', () async {
145+
final servient = Servient(
146+
clientFactories: [
147+
_MockedProtocolClientFactory(),
148+
],
149+
);
150+
151+
final wot = await servient.start();
152+
final thingDiscoveryProcess =
153+
await wot.exploreDirectory(directoryTestUri2);
154+
155+
var counter = 0;
156+
await for (final _ in thingDiscoveryProcess) {
157+
counter++;
158+
}
159+
expect(counter, 0);
160+
expect(thingDiscoveryProcess.done, true);
161+
// expect(thingDiscoveryProcess.error, isNotNull);
162+
});
115163
});
116164
}
117165

@@ -135,6 +183,10 @@ class _MockedProtocolClient implements ProtocolClient {
135183
return '[$validTestThingDescription]'.toContent('application/td+json');
136184
}
137185

186+
if (href == directoryTestThingsUri2) {
187+
return '[$invalidTestThingDescription]'.toContent('application/td+json');
188+
}
189+
138190
throw StateError('Encountered an unknown URI $href.');
139191
}
140192

@@ -152,6 +204,10 @@ class _MockedProtocolClient implements ProtocolClient {
152204
return directoryThingDescription1.toDiscoveryContent(url);
153205
}
154206

207+
if (url == directoryTestUri2) {
208+
return directoryThingDescription2.toDiscoveryContent(url);
209+
}
210+
155211
throw StateError('Encountered invalid URL.');
156212
}
157213

0 commit comments

Comments
 (0)