Skip to content

Commit df42311

Browse files
committed
test: add test for exploreDirectory query parameters
1 parent 8f99cd8 commit df42311

File tree

1 file changed

+74
-2
lines changed

1 file changed

+74
-2
lines changed

test/core/discovery_test.dart

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ final directoryTestUri2 = Uri.parse("$testUriScheme://[::4]/.well-known/wot");
2121
final directoryTestThingsUri2 = Uri.parse("$testUriScheme://[::4]/things");
2222
final directoryTestUri3 = Uri.parse("$testUriScheme://[::5]/.well-known/wot");
2323
final directoryTestThingsUri3 = Uri.parse("$testUriScheme://[::5]/things");
24+
final directoryTestUri4 = Uri.parse("$testUriScheme://[::6]/.well-known/wot");
25+
final directoryTestThingsUri4 = Uri.parse(
26+
"$testUriScheme://[::3]/things?offset=2&limit=3&format=array",
27+
);
2428

2529
const validTestTitle1 = "Test TD 1";
2630
const validTestThingDescription = '''
@@ -49,6 +53,26 @@ final directoryThingDescription1 = '''
4953
},
5054
"properties": {
5155
"things": {
56+
"uriVariables": {
57+
"offset": {
58+
"title": "Number of TDs to skip before the page",
59+
"type": "number",
60+
"default": 0
61+
},
62+
"limit": {
63+
"title": "Number of TDs in a page",
64+
"type": "number"
65+
},
66+
"format": {
67+
"title": "Payload format",
68+
"type": "string",
69+
"enum": [
70+
"array",
71+
"collection"
72+
],
73+
"default": "array"
74+
}
75+
},
5276
"forms": [
5377
{
5478
"href": "$directoryTestThingsUri1"
@@ -128,8 +152,9 @@ class _MockedProtocolClient implements ProtocolClient {
128152
}
129153

130154
@override
131-
Future<Content> readResource(Form form) async {
132-
final href = form.href;
155+
Future<Content> readResource(AugmentedForm form) async {
156+
final href = form.resolvedHref;
157+
133158
if (href == directoryTestThingsUri1) {
134159
return "[$validTestThingDescription]".toContent("application/td+json");
135160
}
@@ -142,6 +167,10 @@ class _MockedProtocolClient implements ProtocolClient {
142167
return invalidTestThingDescription2.toContent("application/td+json");
143168
}
144169

170+
if (href == directoryTestThingsUri4) {
171+
return "[$validTestThingDescription]".toContent("application/ld+json");
172+
}
173+
145174
throw StateError("Encountered an unknown URI $href.");
146175
}
147176

@@ -167,6 +196,10 @@ class _MockedProtocolClient implements ProtocolClient {
167196
return directoryThingDescription3.toDiscoveryContent(url);
168197
}
169198

199+
if (url == directoryTestUri4) {
200+
return directoryThingDescription1.toDiscoveryContent(url);
201+
}
202+
170203
throw StateError("Encountered invalid URL.");
171204
}
172205

@@ -377,5 +410,44 @@ void main() {
377410
await thingDiscoveryProcess.stop();
378411
expect(thingDiscoveryProcess.done, true);
379412
});
413+
414+
test("should support the experimental query parameters API", () async {
415+
final servient = Servient(
416+
clientFactories: [
417+
_MockedProtocolClientFactory(),
418+
],
419+
);
420+
421+
final wot = await servient.start();
422+
final thingDiscoveryProcess = await wot.exploreDirectory(
423+
directoryTestUri4,
424+
offset: 2,
425+
limit: 3,
426+
format: DirectoryPayloadFormat.array,
427+
);
428+
429+
var counter = 0;
430+
await for (final thingDescription in thingDiscoveryProcess) {
431+
counter++;
432+
expect(thingDescription.title, validTestTitle1);
433+
}
434+
expect(counter, 1);
435+
expect(thingDiscoveryProcess.done, true);
436+
});
437+
438+
test(
439+
'should currently not support the "collection" format when using the '
440+
"experimental query parameters API", () async {
441+
final servient = Servient();
442+
final wot = await servient.start();
443+
444+
expect(
445+
() async => await wot.exploreDirectory(
446+
directoryTestUri4,
447+
format: DirectoryPayloadFormat.collection,
448+
),
449+
throwsArgumentError,
450+
);
451+
});
380452
});
381453
}

0 commit comments

Comments
 (0)