Skip to content

Commit f855834

Browse files
committed
tests: adapt new form rules
1 parent f01ddd4 commit f855834

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

packages/binding-modbus/src/modbus-client.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,14 @@ export default class ModbusClient implements ProtocolClient {
212212
return endianness;
213213
}
214214

215-
private overrideFormFromURLPath(input: ModbusForm) {
215+
// This generates a form with url content based on the uri scheme
216+
// Ideally, more code should be refactored to use uri only
217+
private generateFormFromURLPath(input: ModbusForm) {
216218
const { pathname, searchParams: query } = new URL(input.href);
217219
const pathComp = pathname.split("/");
218-
220+
if ((pathComp.length < 3) || pathComp[1] ==='' || pathComp[2]==='') {
221+
throw new Error("Malformed href: unitID and address must be defined");
222+
}
219223
input["modv:unitID"] = parseInt(pathComp[1], 10);
220224
input["modv:address"] = parseInt(pathComp[2], 10);
221225

@@ -243,7 +247,7 @@ export default class ModbusClient implements ProtocolClient {
243247
const mode = contentLength > 0 ? "w" : "r";
244248

245249
// Use form values if provided, otherwise use form values (we are more merciful then the spec for retro-compatibility)
246-
this.overrideFormFromURLPath(form);
250+
this.generateFormFromURLPath(form);
247251

248252
// take over latest content of form into a new result set
249253
const result: ModbusForm = { ...form };

packages/binding-modbus/test/modbus-client-test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ describe("Modbus client test", () => {
269269
it("should throw exception for unknown function", () => {
270270
const form: ModbusForm = {
271271
href: "modbus+tcp://127.0.0.1:8502/1/0?quantity=3",
272-
// @ts-expect-error since unknown function
273272
"modv:function": 255,
274273
};
275274

@@ -286,7 +285,18 @@ describe("Modbus client test", () => {
286285

287286
const promise = client.readResource(form);
288287

289-
return promise.should.eventually.rejectedWith("Malformed form: address must be defined");
288+
return promise.should.eventually.rejectedWith("Malformed href: unitID and address must be defined");
289+
});
290+
291+
it("should throw exception for missing unitID", () => {
292+
const form: ModbusForm = {
293+
href: "modbus+tcp://127.0.0.1:8502",
294+
"modv:function": 1,
295+
};
296+
297+
const promise = client.readResource(form);
298+
299+
return promise.should.eventually.rejectedWith("Malformed href: unitID and address must be defined");
290300
});
291301
});
292302

0 commit comments

Comments
 (0)