forked from eclipse-thingweb/node-wot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientTest.ts
More file actions
1048 lines (927 loc) · 38 KB
/
ClientTest.ts
File metadata and controls
1048 lines (927 loc) · 38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************************
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
/**
* Basic test suite to demonstrate test setup
* uncomment the @skip to see failing tests
*
* h0ru5: there is currently some problem with VSC failing to recognize experimentalDecorators option, it is present in both tsconfigs
*/
import { suite, test } from "@testdeck/mocha";
import { expect, should, use as chaiUse, assert } from "chai";
import { Subscription } from "rxjs/Subscription";
import Servient from "../src/servient";
import ConsumedThing from "../src/consumed-thing";
import { AllOfSecurityScheme, Form, OneOfSecurityScheme, SecurityScheme } from "../src/thing-description";
import { ProtocolClient, ProtocolClientFactory } from "../src/protocol-interfaces";
import { Content } from "../src/content";
import { ContentSerdes } from "../src/content-serdes";
import Helpers from "../src/helpers";
import { Readable } from "stream";
import { createLoggers, ProtocolHelpers } from "../src/core";
import { ThingDescription } from "wot-typescript-definitions";
import chaiAsPromised from "chai-as-promised";
import { fail } from "assert";
const { debug } = createLoggers("core", "ClientTest");
chaiUse(chaiAsPromised);
// should must be called to augment all variables
should();
const myThingDesc = {
"@context": ["https://w3c.github.io/wot/w3c-wot-td-context.jsonld"],
"@type": ["Thing"],
id: "urn:dev:wot:test-thing",
title: "aThing",
security: [{ scheme: "nosec" }],
uriVariables: {
idTestGlobal: {
type: "string",
default: "test2",
},
},
properties: {
aProperty: {
type: "integer",
readOnly: false,
forms: [
{
href: "testdata://host/athing/properties/aproperty{?idTest,idTestGlobal}",
mediaType: "application/json",
},
],
uriVariables: {
idTest: {
type: "string",
},
},
},
aPropertyToObserve: {
type: "integer",
readOnly: false,
observable: true,
forms: [
{
href: "testdata://host/athing/properties/apropertytoobserve",
mediaType: "application/json",
op: ["observeproperty", "unobserveproperty"],
},
{
href: "testdata://host/athing/properties/apropertytoobserve1",
contentType: "application/json",
op: ["observeproperty"],
},
{
href: "testdata://host/athing/properties/apropertytoobserve1",
contentType: "application/json",
op: ["unobserveproperty"],
},
],
},
},
actions: {
anAction: {
input: { type: "integer" },
output: { type: "integer" },
forms: [
{
href: "testdata://host/athing/actions/anaction",
mediaType: "application/json",
response: { contentType: "application/json" },
},
],
},
anAsyncAction: {
input: { type: "integer" },
output: { type: "integer" },
synchronous: false,
forms: [
{
href: "testdata://host/athing/actions/anasyncaction",
mediaType: "application/json",
response: { contentType: "application/json" },
},
],
},
},
events: {
anEvent: {
data: {
type: "string",
},
forms: [
{
href: "testdata://host/athing/events/anevent",
mediaType: "application/json",
op: ["subscribeevent", "unsubscribeevent"],
},
{
href: "testdata://host/athing/events/anevent",
contentType: "application/json",
op: ["subscribeevent"],
},
{
href: "testdata://host/athing/events/anevent",
contentType: "application/json",
op: ["unsubscribeevent"],
},
],
},
},
};
class TDClient implements ProtocolClient {
public readResource(form: Form): Promise<Content> {
// Note: this is not a "real" DataClient! Instead it just reports the same TD in any case
const c: Content = new Content(ContentSerdes.TD, Readable.from(Buffer.from(JSON.stringify(myThingDesc))));
return Promise.resolve(c);
}
public writeResource(form: Form, content: Content): Promise<void> {
return Promise.reject(new Error("writeResource not implemented"));
}
public invokeResource(form: Form, content: Content): Promise<Content> {
return Promise.reject(new Error("invokeResource not implemented"));
}
public unlinkResource(form: Form): Promise<void> {
return Promise.reject(new Error("unlinkResource not implemented"));
}
public async subscribeResource(
form: Form,
next: (value: Content) => void,
error?: (error: Error) => void,
complete?: () => void
): Promise<Subscription> {
return new Subscription();
}
async requestThingDescription(uri: string): Promise<Content> {
throw new Error("Method not implemented.");
}
public async start(): Promise<void> {
// do nothing
}
public async stop(): Promise<void> {
// do nothing
}
public setSecurity = (metadata: SecurityScheme[]) => false;
public toString(): string {
return "TDClient";
}
}
class TDClientFactory implements ProtocolClientFactory {
public readonly scheme: string = "td";
client = new TDClient();
public getClient(): ProtocolClient {
return this.client;
}
public init(): boolean {
return true;
}
public destroy(): boolean {
return true;
}
}
class TrapClient implements ProtocolClient {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private trap: (...args: any[]) => Content | Promise<Content> = () =>
new Content("application/json", Readable.from(Buffer.from("")));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public setTrap(callback: (...args: any[]) => Content | Promise<Content>) {
this.trap = callback;
}
public async readResource(form: Form): Promise<Content> {
return await this.trap(form);
}
public async writeResource(form: Form, content: Content): Promise<void> {
await this.trap(form, content);
}
public async invokeResource(form: Form, content: Content): Promise<Content> {
return await this.trap(form, content);
}
public async unlinkResource(form: Form): Promise<void> {
await this.trap(form);
}
public async subscribeResource(
form: Form,
next: (value: Content) => void,
error?: (error: Error) => void,
complete?: () => void
): Promise<Subscription> {
// send one event
next(this.trap(form) as Content);
// then complete
setImmediate(() => {
complete?.();
});
return new Subscription();
}
async requestThingDescription(uri: string): Promise<Content> {
throw new Error("Method not implemented.");
}
public async start(): Promise<void> {
// do nothing
}
public async stop(): Promise<void> {
// do nothing
}
public setSecurity = (metadata: SecurityScheme[]) => false;
}
class TrapClientFactory implements ProtocolClientFactory {
public scheme = "testdata";
client = new TrapClient();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public setTrap(callback: (...args: any[]) => Content | Promise<Content>) {
this.client.setTrap(callback);
}
public getClient(): ProtocolClient {
return this.client;
}
public init(): boolean {
return true;
}
public destroy(): boolean {
return true;
}
}
class TrapClientFactory2 extends TrapClientFactory {
public scheme = "testdata2";
}
class TestProtocolClient implements ProtocolClient {
readResource(form: Form): Promise<Content> {
throw new Error("Method not implemented.");
}
writeResource(form: Form, content: Content): Promise<void> {
throw new Error("Method not implemented.");
}
invokeResource(form: Form, content: Content): Promise<Content> {
throw new Error("Method not implemented.");
}
unlinkResource(form: Form): Promise<void> {
throw new Error("Method not implemented.");
}
subscribeResource(
form: Form,
next: (content: Content) => void,
error?: (error: Error) => void,
complete?: () => void
): Promise<Subscription> {
throw new Error("Method not implemented.");
}
async requestThingDescription(uri: string): Promise<Content> {
throw new Error("Method not implemented.");
}
async start(): Promise<void> {
throw new Error("Method not implemented.");
}
async stop(): Promise<void> {
throw new Error("Method not implemented.");
}
public securitySchemes: SecurityScheme[] = [];
setSecurity(securitySchemes: SecurityScheme[], credentials?: Record<string, unknown>): boolean {
this.securitySchemes = securitySchemes;
return true;
}
}
@suite("client flow of servient")
class WoTClientTest {
static servient: Servient;
static clientFactory: TrapClientFactory;
static WoT: typeof WoT;
static WoTHelpers: Helpers;
static before() {
this.servient = new Servient();
this.WoTHelpers = new Helpers(this.servient);
this.clientFactory = new TrapClientFactory();
this.servient.addClientFactory(this.clientFactory);
this.servient.addClientFactory(new TDClientFactory());
this.servient.start().then((myWoT) => {
this.WoT = myWoT;
});
debug("started test suite");
}
static async after(): Promise<void> {
await this.servient.shutdown();
debug("finished test suite");
}
@test async "read a Property"() {
// let the client return 42
WoTClientTest.clientFactory.setTrap(() => {
return new Content("application/json", Readable.from(Buffer.from("42")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing.getThingDescription()).to.have.property("properties");
expect(thing.getThingDescription()).to.have.property("properties").to.have.property("aProperty");
const result = await thing.readProperty("aProperty");
// eslint-disable-next-line no-unused-expressions
expect(result).not.to.be.null;
const value = await result.value();
expect(value?.toString()).to.equal("42");
}
@test async "read all properties"() {
// let the client return 42
WoTClientTest.clientFactory.setTrap(() => {
return new Content("application/json", Readable.from(Buffer.from("42")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing.getThingDescription()).to.have.property("properties");
expect(thing.getThingDescription()).to.have.property("properties").to.have.property("aProperty");
const result: WoT.PropertyReadMap = await thing.readAllProperties();
// eslint-disable-next-line no-unused-expressions
expect(result).not.to.be.null;
// eslint-disable-next-line no-unused-expressions
expect(result.get("aProperty")).not.to.be.null;
// eslint-disable-next-line no-unused-expressions
expect(result.get("aPropertyToObserve")).to.be.undefined;
const io = result.get("aProperty");
const value = await io?.value();
expect(value).to.equal(42);
}
@test async "write a Property with raw readable stream"() {
// verify the value transmitted
WoTClientTest.clientFactory.setTrap(async (form: Form, content: Content) => {
const valueData = await content.toBuffer();
expect(valueData.toString()).to.equal("23");
return content;
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("properties").that.has.property("aProperty");
const stream = Readable.from(Buffer.from("23"));
try {
await thing.writeProperty("aProperty", ProtocolHelpers.toWoTStream(stream));
} catch (error) {
console.log(error);
}
}
@test async "write a Property with data schema value"() {
// verify the value transmitted
WoTClientTest.clientFactory.setTrap(async (form: Form, content: Content) => {
const valueData = await content.toBuffer();
expect(valueData.toString()).to.equal("58");
return content;
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("properties").that.has.property("aProperty");
return thing.writeProperty("aProperty", 58);
}
@test async "write multiple property new api"() {
// verify the value transmitted
WoTClientTest.clientFactory.setTrap(async (form: Form, content: Content) => {
const valueData = await content.toBuffer();
expect(valueData.toString()).to.equal("66");
return content;
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("properties").that.has.property("aProperty");
const valueMap = new Map();
const stream = Readable.from(Buffer.from("66"));
valueMap.set("aProperty", ProtocolHelpers.toWoTStream(stream));
return thing.writeMultipleProperties(valueMap);
}
@test async "call an action"() {
// an action
WoTClientTest.clientFactory.setTrap(async (form: Form, content: Content) => {
const valueData = await content.toBuffer();
expect(valueData.toString()).to.equal("23");
return new Content("application/json", Readable.from(Buffer.from("42")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("actions").that.has.property("anAction");
const result = await thing.invokeAction("anAction", 23);
// eslint-disable-next-line no-unused-expressions
expect(result).not.to.be.null;
const value = await result?.value();
expect(value).to.equal(42);
}
@test async "call an action with enhanced contentType"() {
// an action
WoTClientTest.clientFactory.setTrap(async (form: Form, content: Content) => {
const valueData = await content.toBuffer();
expect(valueData.toString()).to.equal("23");
return new Content("application/json; charset=utf-8", Readable.from(Buffer.from("42")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("actions").that.has.property("anAction");
const result = await thing.invokeAction("anAction", 23);
// eslint-disable-next-line no-unused-expressions
expect(result).not.to.be.null;
const value = await result?.value();
expect(value).to.equal(42);
}
@test async "call an action with wrong contentType based on TD response"() {
// an action
WoTClientTest.clientFactory.setTrap(async (form: Form, content: Content) => {
const valueData = await content.toBuffer();
expect(valueData.toString()).to.equal("23");
// Note: application/json expected based on TD response
return new Content("text/plain", Readable.from(Buffer.from("42")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("actions").that.has.property("anAction");
try {
await thing.invokeAction("anAction", 23);
fail("Should report unexpected content type");
} catch (e) {
const error = e instanceof Error ? e : new Error(JSON.stringify(e));
expect(error.message).to.contain("type");
}
}
@test async "call an async action"() {
// should not throw Error: Invalid value according to DataSchema
WoTClientTest.clientFactory.setTrap(async (form: Form, content: Content) => {
const valueData = await content.toBuffer();
expect(valueData.toString()).to.equal("23");
return new Content("application/json", Readable.from(Buffer.from(JSON.stringify({ status: "pending" }))));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("actions").that.has.property("anAction");
// deal with ActionStatus object
const result = await thing.invokeAction("anAsyncAction", 23);
// eslint-disable-next-line no-unused-expressions
expect(result).not.to.be.null;
const value = await result?.value();
expect(value).to.have.property("status");
}
@test async "subscribe to event"() {
WoTClientTest.clientFactory.setTrap(() => {
return new Content("application/json", Readable.from(Buffer.from("triggered")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("events").that.has.property("anEvent");
await thing.subscribeEvent("anEvent", async (x) => {
const value = await x.value();
expect(value).to.equal("triggered");
});
}
@test async "should unsubscribe to event"() {
WoTClientTest.clientFactory.setTrap(() => {
return new Content("application/json", Readable.from(Buffer.from("triggered")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("events").that.has.property("anEvent");
return new Promise((resolve) => {
thing.subscribeEvent("anEvent", async (x) => {
const value = await x.value();
expect(value).to.equal("triggered");
resolve(true);
});
});
}
@test async "subscribe to event with formIndex"() {
WoTClientTest.clientFactory.setTrap(() => {
return new Content("application/json", Readable.from(Buffer.from("triggered")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("events").that.has.property("anEvent");
const subscription = await thing.subscribeEvent(
"anEvent",
() => {
/* */
},
undefined,
{ formIndex: 1 }
);
await subscription.stop();
}
@test async "should not subscribe twice to event"() {
WoTClientTest.clientFactory.setTrap(() => {
return new Content("application/json", Readable.from(Buffer.from("triggered")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("events").that.has.property("anEvent");
await thing.subscribeEvent("anEvent", () => {
/** */
});
await expect(
thing.subscribeEvent("anEvent", () => {
/** */
})
).to.be.rejected;
}
@test async "should be able to subscribe again after unsubscribe to event"() {
WoTClientTest.clientFactory.setTrap(() => {
return new Content("application/json", Readable.from(Buffer.from("triggered")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("events").that.has.property("anEvent");
const subscription = await thing.subscribeEvent("anEvent", () => {
/** */
});
await subscription.stop();
return new Promise((resolve) => {
thing.subscribeEvent("anEvent", async (x) => {
const value = await x.value();
expect(value).to.equal("triggered");
resolve(true);
});
});
}
@test async "observe property"() {
WoTClientTest.clientFactory.setTrap(() => {
return new Content("application/json", Readable.from(Buffer.from("12")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("properties").that.has.property("aPropertyToObserve");
return new Promise((resolve) => {
thing.observeProperty("aPropertyToObserve", async (data) => {
const value = await data.value();
expect(value).to.equal(12);
resolve(true);
});
});
}
@test async "observe property with formIndex"() {
WoTClientTest.clientFactory.setTrap(() => {
return new Content("application/json", Readable.from(Buffer.from("12")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("properties").that.has.property("aPropertyToObserve");
const subscription = await thing.observeProperty(
"aPropertyToObserve",
() => {
/** */
},
undefined,
{ formIndex: 1 }
);
await subscription.stop();
}
@test async "should not observe twice a property"() {
WoTClientTest.clientFactory.setTrap(() => {
return new Content("application/json", Readable.from(Buffer.from("triggered")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("events").that.has.property("anEvent");
await thing.observeProperty("aPropertyToObserve", () => {
/** */
});
await expect(
thing.observeProperty("aPropertyToObserve", () => {
/** */
})
).to.be.rejected;
}
@test "observe property should fail"(done: Mocha.Done) {
WoTClientTest.WoTHelpers.fetch("td://foo")
.then((td) => {
return WoTClientTest.WoT.consume(td as ThingDescription);
})
.then((thing) => {
expect(thing).to.have.property("title").that.equals("aThing");
expect(thing).to.have.property("properties").that.has.property("aProperty");
thing
.observeProperty("aProperty", () => {
done(new Error("property is not observable"));
})
.catch(() => {
done();
});
})
.catch((err) => {
done(err);
});
}
@test "ensure security thing level"(done: Mocha.Done) {
try {
const ct = new ConsumedThing(WoTClientTest.servient);
ct.securityDefinitions = {
basic_sc: {
scheme: "basic",
},
};
ct.security = ["basic_sc"];
const pc = new TestProtocolClient();
const form: Form = {
href: "https://example.com/",
};
ct.ensureClientSecurity(pc, form);
expect(pc.securitySchemes.length).equals(1);
expect(pc.securitySchemes[0].scheme).equals("basic");
done();
} catch (err) {
done(err);
}
}
@test "ensure security form level"(done: Mocha.Done) {
try {
const ct = new ConsumedThing(WoTClientTest.servient);
ct.securityDefinitions = {
basic_sc: {
scheme: "basic",
},
apikey_sc: {
scheme: "apikey",
},
};
ct.security = ["basic_sc"];
const pc = new TestProtocolClient();
const form: Form = {
href: "https://example.com/",
security: ["apikey_sc"],
};
ct.ensureClientSecurity(pc, form);
expect(pc.securitySchemes.length).equals(1);
expect(pc.securitySchemes[0].scheme).equals("apikey");
done();
} catch (err) {
done(err);
}
}
@test async "take into account global uriVariables"() {
// let the client return 42
WoTClientTest.clientFactory.setTrap((form: Form) => {
expect(form.href).to.contain("idTest=test");
expect(form.href).to.contain("idTestGlobal=test2");
return new Content("application/json", Readable.from(Buffer.from("42")));
});
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
const result = await thing.readProperty("aProperty", { uriVariables: { idTest: "test" } });
// eslint-disable-next-line no-unused-expressions
expect(result).not.to.be.null;
const value = await result.value();
expect(value?.toString()).to.equal("42");
}
@test async "give error on wrong uriVariable"() {
const td = (await WoTClientTest.WoTHelpers.fetch("td://foo")) as ThingDescription;
const thing = await WoTClientTest.WoT.consume(td);
expect(
thing.readProperty("aProperty", { uriVariables: { idTestWrong: "test" } })
).to.eventually.be.rejectedWith(Error);
}
@test async "adding and removing client factory"() {
const tcf2 = new TrapClientFactory2();
// eslint-disable-next-line no-unused-expressions
expect(WoTClientTest.servient.hasClientFor(tcf2.scheme)).to.be.not.true;
WoTClientTest.servient.addClientFactory(new TrapClientFactory2());
// eslint-disable-next-line no-unused-expressions
expect(WoTClientTest.servient.hasClientFor(tcf2.scheme)).to.be.true;
const result = WoTClientTest.servient.removeClientFactory(tcf2.scheme);
expect(result);
// eslint-disable-next-line no-unused-expressions
expect(WoTClientTest.servient.hasClientFor(tcf2.scheme)).to.be.not.true;
}
@test "ensure combo security - allOf"() {
const ct = new ConsumedThing(WoTClientTest.servient);
ct.securityDefinitions = {
basic_sc: {
scheme: "basic",
},
opcua_secure_channel_sc: {
scheme: "opcua-channel-security",
},
opcua_authetication_sc: {
scheme: "opcua-authentication",
},
combo_sc: {
scheme: "combo",
allOf: ["opcua_secure_channel_sc", "opcua_authetication_sc"],
},
};
ct.security = ["combo_sc"];
const pc = new TestProtocolClient();
const form: Form = {
href: "https://example.com/",
};
ct.ensureClientSecurity(pc, form);
expect(pc.securitySchemes.length).equals(1);
expect(pc.securitySchemes[0].scheme).equals("combo");
const comboScheme = pc.securitySchemes[0] as AllOfSecurityScheme;
expect(comboScheme.allOf).instanceOf(Array);
expect(comboScheme.allOf.length).equal(2);
expect(comboScheme.allOf[0].scheme).equals("opcua-channel-security");
expect(comboScheme.allOf[1].scheme).equals("opcua-authentication");
}
@test "ensure combo security - oneOf"() {
const ct = new ConsumedThing(WoTClientTest.servient);
ct.securityDefinitions = {
basic_sc: {
scheme: "basic",
},
opcua_secure_channel_encrypt_sc: {
scheme: "opcua-channel-security",
mode: "encrypt",
},
opcua_secure_channel_sign_sc: {
scheme: "opcua-channel-security",
mode: "sign",
},
opcua_authetication_sc: {
scheme: "opcua-authentication",
},
comob_opcua_secure_channel: {
scheme: "combo",
oneOf: ["opcua_secure_channel_encrypt_sc", "opcua_secure_channel_sign_sc"],
},
combo_sc: {
scheme: "combo",
allOf: ["comob_opcua_secure_channel", "opcua_authetication_sc"],
},
};
ct.security = ["combo_sc"];
const pc = new TestProtocolClient();
const form: Form = {
href: "https://example.com/",
};
ct.ensureClientSecurity(pc, form);
expect(pc.securitySchemes.length).equals(1);
expect(pc.securitySchemes[0].scheme).equals("combo");
const comboScheme = pc.securitySchemes[0] as AllOfSecurityScheme;
expect(comboScheme.allOf).instanceOf(Array);
expect(comboScheme.allOf.length).equal(2);
expect(comboScheme.allOf[0].scheme).equals("combo");
expect(comboScheme.allOf[1].scheme).equals("opcua-authentication");
//
const firstScheme = comboScheme.allOf[0] as OneOfSecurityScheme;
expect(firstScheme.scheme).equal("combo");
expect(firstScheme.oneOf).instanceOf(Array);
expect(firstScheme.oneOf.length).equal(2);
expect(firstScheme.oneOf[0].scheme).equal("opcua-channel-security");
expect(firstScheme.oneOf[0].scheme).equal("opcua-channel-security");
}
@test "ensure combo security in form - allOf"() {
const ct = new ConsumedThing(WoTClientTest.servient);
ct.securityDefinitions = {
basic_sc: {
scheme: "basic",
},
opcua_secure_channel_sc: {
scheme: "opcua-channel-security",
},
opcua_authetication_sc: {
scheme: "opcua-authentication",
},
combo_sc: {
scheme: "combo",
allOf: ["opcua_secure_channel_sc", "opcua_authetication_sc"],
},
};
ct.security = "basic";
const pc = new TestProtocolClient();
const form: Form = {
href: "https://example.com/",
security: ["combo_sc"],
};
ct.ensureClientSecurity(pc, form);
expect(pc.securitySchemes.length).equals(1);
const comboScheme = pc.securitySchemes[0] as AllOfSecurityScheme;
expect(comboScheme.allOf[0].scheme).equals("opcua-channel-security");
expect(comboScheme.allOf[1].scheme).equals("opcua-authentication");
}
@test "ensure no infinite loop with recursive combo security"() {
const ct = new ConsumedThing(WoTClientTest.servient);
ct.securityDefinitions = {
// a badly designed combo that goes into infinite loop
combo_sc: {
scheme: "combo",
allOf: ["combo_sc", "combo_sc"],
},
};
ct.security = "basic";
const pc = new TestProtocolClient();
const form: Form = {
href: "https://example.com/",
security: ["combo_sc"],
};
ct.ensureClientSecurity(pc, form);
expect(pc.securitySchemes.length).equals(1);
}
@test "complex combo security with repeated elements"() {
const ct = new ConsumedThing(WoTClientTest.servient);
ct.securityDefinitions = {
// a badly designed combo that goes into infinite loop
a: {
scheme: "a",
},
b: {
scheme: "b",
},
c: {
scheme: "c",
},
combo_a_and_b: {
scheme: "combo",
allOf: ["a", "b"],
},
combo_a_and_c: {
scheme: "combo",
allOf: ["a", "c"],
},
combo_a_or_b: {
scheme: "combo",
oneOf: ["a", "b"],
},
combo_of_combo: {
scheme: "combo",
oneOf: ["combo_a_and_b", "combo_a_and_c"],
},
};
ct.security = ["combo_of_combo"];
const pc = new TestProtocolClient();
const form: Form = {
href: "https://example.com/",
};
ct.ensureClientSecurity(pc, form);
expect(pc.securitySchemes.length).equals(1);
expect(pc.securitySchemes[0].scheme).equal("combo");
const comboOfCombo = pc.securitySchemes[0] as OneOfSecurityScheme;
expect(comboOfCombo.oneOf).instanceOf(Array);
expect(comboOfCombo.oneOf.length).equal(2);
expect(comboOfCombo.oneOf[0].scheme).equal("combo");
expect(comboOfCombo.oneOf[1].scheme).equal("combo");
const first = comboOfCombo.oneOf[0] as AllOfSecurityScheme;
expect(first.allOf).instanceOf(Array);
expect(first.allOf[0].scheme).equal("a");
expect(first.allOf[1].scheme).equal("b");
const second = comboOfCombo.oneOf[1] as AllOfSecurityScheme;
expect(second.allOf).instanceOf(Array);
expect(second.allOf[0].scheme).equal("a");
expect(second.allOf[1].scheme).equal("c");
// Verfy that a has been processed once - with strict equality
const a1 = first.allOf[0];
const a2 = second.allOf[0];