Testing your code
@node-net/netconf/testing has FakeServer, an in-process NETCONF server you can script. It speaks real RFC 6242 framing over an in-memory connection, so your code runs the same client code it would against a device, with no network and no container.
import { connect, x } from '@node-net/netconf';
import { FakeServer, type RpcHandler } from '@node-net/netconf/testing';
const SYS = 'urn:example:system';
const handler: RpcHandler = (rpc, { operation }) => {
if (operation?.name === 'get-config') {
return { data: x('system', { xmlns: SYS }, x('hostname', 'r1')) };
}
if (operation?.name === 'commit') {
return {
errors: [
{ type: 'application', tag: 'operation-failed', message: 'nope' },
],
};
}
return 'ok';
};
const server = new FakeServer({
capabilities: [
'urn:ietf:params:netconf:base:1.1',
'urn:ietf:params:netconf:capability:candidate:1.0',
],
handler,
});
await using client = await connect({ transport: server.transport() });
await myCodeUnderTest(client);
const sent = server.requests.map((r) => r.elements[0]?.name);
expect(sent).toEqual(['get-config', 'edit-config', 'commit']);
expect(server.problems).toEqual([]);Replies
A handler gets the <rpc> element and { messageId, operation, server }, and returns, or resolves with:
| Return | Reply |
|---|---|
'ok' | <ok/> |
{ data } | <data>…</data> with these children |
{ errors: [{ type, tag, severity?, appTag?, path?, message?, info? }] } | one <rpc-error> each |
{ body } | these children, as given |
undefined | no reply at all, for testing timeouts |
A handler that throws produces an operation-failed error. <close-session> is always answered for you.
Driving the server
server.notify(element, eventTime?) | send an RFC 5277 notification |
server.send(message) | send any message in the current framing |
server.sendRaw(bytes) | write bytes with no framing, for testing broken input |
server.end(), server.destroy(err?) | close cleanly, or drop the connection |
server.requests | every <rpc> received, in order |
server.clientHello | resolves with the client's <hello> |
server.problems | bad framing or XML the server received; assert it stays empty |
Options: sessionId, capabilities (default base 1.0 and 1.1), and hello, which replaces the server's hello with your own text, or sends none if false.
A FakeServer serves a single connection. server.connection is the client's end of it, if you want to drive the bytes yourself. To serve a FakeServer over a real transport, such as an SSH channel in a test server, splice the two with joinConnections() (see Your own transport).
FakeServer needs nothing from Node.js, so it works with @node-net/netconf/core in a browser or a worker as well.