File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 } ;
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments