Skip to content

Commit

Permalink
test: add test for exploreDirectory query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 5, 2024
1 parent 8f99cd8 commit df42311
Showing 1 changed file with 74 additions and 2 deletions.
76 changes: 74 additions & 2 deletions test/core/discovery_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '''
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -128,8 +152,9 @@ class _MockedProtocolClient implements ProtocolClient {
}

@override
Future<Content> readResource(Form form) async {
final href = form.href;
Future<Content> readResource(AugmentedForm form) async {
final href = form.resolvedHref;

if (href == directoryTestThingsUri1) {
return "[$validTestThingDescription]".toContent("application/td+json");
}
Expand All @@ -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.");
}

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

if (url == directoryTestUri4) {
return directoryThingDescription1.toDiscoveryContent(url);
}

throw StateError("Encountered invalid URL.");
}

Expand Down Expand Up @@ -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,
);
});
});
}

0 comments on commit df42311

Please sign in to comment.