diff --git a/test/core/discovery_test.dart b/test/core/discovery_test.dart index 4be01c17..8e5bc3f2 100644 --- a/test/core/discovery_test.dart +++ b/test/core/discovery_test.dart @@ -21,6 +21,10 @@ final directoryTestUri2 = Uri.parse("$testUriScheme://[::4]/.well-known/wot"); final directoryTestThingsUri2 = Uri.parse("$testUriScheme://[::4]/things"); final directoryTestUri3 = Uri.parse("$testUriScheme://[::5]/.well-known/wot"); final directoryTestThingsUri3 = Uri.parse("$testUriScheme://[::5]/things"); +final directoryTestUri4 = Uri.parse("$testUriScheme://[::6]/.well-known/wot"); +final directoryTestThingsUri4 = Uri.parse( + "$testUriScheme://[::3]/things?offset=2&limit=3&format=array", +); const validTestTitle1 = "Test TD 1"; const validTestThingDescription = ''' @@ -49,6 +53,26 @@ final directoryThingDescription1 = ''' }, "properties": { "things": { + "uriVariables": { + "offset": { + "title": "Number of TDs to skip before the page", + "type": "number", + "default": 0 + }, + "limit": { + "title": "Number of TDs in a page", + "type": "number" + }, + "format": { + "title": "Payload format", + "type": "string", + "enum": [ + "array", + "collection" + ], + "default": "array" + } + }, "forms": [ { "href": "$directoryTestThingsUri1" @@ -128,8 +152,9 @@ class _MockedProtocolClient implements ProtocolClient { } @override - Future readResource(Form form) async { - final href = form.href; + Future readResource(AugmentedForm form) async { + final href = form.resolvedHref; + if (href == directoryTestThingsUri1) { return "[$validTestThingDescription]".toContent("application/td+json"); } @@ -142,6 +167,10 @@ class _MockedProtocolClient implements ProtocolClient { return invalidTestThingDescription2.toContent("application/td+json"); } + if (href == directoryTestThingsUri4) { + return "[$validTestThingDescription]".toContent("application/ld+json"); + } + throw StateError("Encountered an unknown URI $href."); } @@ -167,6 +196,10 @@ class _MockedProtocolClient implements ProtocolClient { return directoryThingDescription3.toDiscoveryContent(url); } + if (url == directoryTestUri4) { + return directoryThingDescription1.toDiscoveryContent(url); + } + throw StateError("Encountered invalid URL."); } @@ -377,5 +410,44 @@ void main() { await thingDiscoveryProcess.stop(); expect(thingDiscoveryProcess.done, true); }); + + test("should support the experimental query parameters API", () async { + final servient = Servient( + clientFactories: [ + _MockedProtocolClientFactory(), + ], + ); + + final wot = await servient.start(); + final thingDiscoveryProcess = await wot.exploreDirectory( + directoryTestUri4, + offset: 2, + limit: 3, + format: DirectoryPayloadFormat.array, + ); + + var counter = 0; + await for (final thingDescription in thingDiscoveryProcess) { + counter++; + expect(thingDescription.title, validTestTitle1); + } + expect(counter, 1); + expect(thingDiscoveryProcess.done, true); + }); + + test( + 'should currently not support the "collection" format when using the ' + "experimental query parameters API", () async { + final servient = Servient(); + final wot = await servient.start(); + + expect( + () async => await wot.exploreDirectory( + directoryTestUri4, + format: DirectoryPayloadFormat.collection, + ), + throwsArgumentError, + ); + }); }); }