Repository navigation
fix(simulator): encode station identity in WebSocket connection URL #2177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jerome-benoit
merged 6 commits into
SAP:main
from
VitorCoelho:fix/encode-station-identity-in-ws-url
Oct 31, 2026
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c2c649e
fix(charging-station): percent-encode the station identity in the sup…
VitorCoelho b6e3969
test(charging-station): cover station identity encoding in the superv…
VitorCoelho 94c024e
fix(ui-web): strip the percent-encoded station identity from the supe…
VitorCoelho 22fa093
test(ui-web): cover the supervision URL prefill with an encoded identity
VitorCoelho 793445b
test(charging-station): skip the Unix socket identity test on Windows
VitorCoelho 66230cd
test(charging-station): delete the stations created by the identity e…
VitorCoelho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
tests/charging-station/ChargingStation-WsConnectionUrlIdentity.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /** | ||
| * @file Tests that the station identity is percent-encoded when appended to the supervision URL. | ||
| * @description `ChargingStation.wsConnectionUrl` appends the station identity | ||
| * to the supervision URL as one path segment. OCPP-J 1.6 section 3.1.1 | ||
| * requires that identity to be percent-encoded (RFC 3986), otherwise a | ||
| * reserved character in it becomes URL syntax: `FRESH#1` turns into a fragment | ||
| * the WebSocket client refuses, `A?B` into a query, `A/B` into two segments. | ||
| * The raw identity is preserved everywhere else. | ||
| */ | ||
| import assert from 'node:assert/strict' | ||
| import { createServer } from 'node:http' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { afterEach, describe, it } from 'node:test' | ||
| import { WebSocket, WebSocketServer } from 'ws' | ||
|
|
||
| import { getConfigurationKey } from '../../src/charging-station/ConfigurationKeyUtils.js' | ||
| import { flushMicrotasks, standardCleanup } from '../helpers/TestLifecycleHelpers.js' | ||
| import { TEST_SUPERVISION_URL } from '../utils/TestNetworkConstants.js' | ||
| import { TEST_CHARGING_STATION_BASE_NAME } from './ChargingStationTestConstants.js' | ||
| import { | ||
| cleanupStationTemplates, | ||
| copyStationTemplate, | ||
| createStationFromTemplate, | ||
| } from './helpers/StationHelpers.realStation.js' | ||
|
|
||
| // Shipped templates use this custom key instead of the default ConnectionUrl. | ||
| const OCPP_SUPERVISION_URL_KEY = 'ocppcentraladdress' | ||
| const RESERVED_IDENTITIES: Record<string, string> = { | ||
| 'CS 1': 'CS%201', | ||
| 'CS%1': 'CS%251', | ||
| 'CS/1': 'CS%2F1', | ||
| 'CS?1': 'CS%3F1', | ||
| 'FRESH#1': 'FRESH%231', | ||
| } | ||
|
|
||
| const SUPERVISION_URL = `${TEST_SUPERVISION_URL}/OCPP16` | ||
|
|
||
| const createStationNamed = (baseName: string, supervisionUrl = SUPERVISION_URL) => { | ||
| const station = createStationFromTemplate(copyStationTemplate(), { | ||
| baseName, | ||
| fixedName: true, | ||
| persistentConfiguration: false, | ||
| }) | ||
| // The template ships its own supervision URL; pin the one under test. | ||
| station.setSupervisionUrl(supervisionUrl) | ||
| return station | ||
| } | ||
|
|
||
| await describe('ChargingStation wsConnectionUrl identity encoding', async () => { | ||
| afterEach(async () => { | ||
| await flushMicrotasks() | ||
| standardCleanup() | ||
| cleanupStationTemplates() | ||
|
jerome-benoit marked this conversation as resolved.
Resolved
|
||
| }) | ||
|
|
||
| await it('should leave an ordinary identity unchanged', () => { | ||
| const station = createStationNamed(TEST_CHARGING_STATION_BASE_NAME) | ||
| assert.strictEqual(station.stationInfo?.chargingStationId, TEST_CHARGING_STATION_BASE_NAME) | ||
| assert.strictEqual( | ||
| station.wsConnectionUrl.href, | ||
| `${SUPERVISION_URL}/${TEST_CHARGING_STATION_BASE_NAME}` | ||
| ) | ||
| }) | ||
|
|
||
| await it('should percent-encode reserved characters of the identity as one path segment', () => { | ||
| for (const [identity, encoded] of Object.entries(RESERVED_IDENTITIES)) { | ||
| const station = createStationNamed(identity) | ||
| const url = station.wsConnectionUrl | ||
| assert.strictEqual(station.stationInfo?.chargingStationId, identity, identity) | ||
| assert.strictEqual(url.href, `${SUPERVISION_URL}/${encoded}`, identity) | ||
| assert.strictEqual(url.pathname, `/OCPP16/${encoded}`, identity) | ||
| assert.strictEqual(url.search, '', identity) | ||
| assert.strictEqual(url.hash, '', identity) | ||
| assert.strictEqual(decodeURIComponent(url.pathname.split('/').at(-1) ?? ''), identity) | ||
| } | ||
| }) | ||
|
|
||
| await it('should encode the identity on the OCPP configuration key branch too', () => { | ||
| // The key carries the base URL without going through setSupervisionUrl | ||
| // (template Configuration, or an OCPP ChangeConfiguration); the getter is | ||
| // the only place that appends the identity to it. | ||
| const station = createStationFromTemplate( | ||
| copyStationTemplate({ | ||
| Configuration: { | ||
| configurationKey: [ | ||
| { key: OCPP_SUPERVISION_URL_KEY, readonly: false, value: SUPERVISION_URL }, | ||
| ], | ||
| }, | ||
| supervisionUrlOcppConfiguration: true, | ||
| supervisionUrlOcppKey: OCPP_SUPERVISION_URL_KEY, | ||
| }), | ||
| { baseName: 'FRESH#1', fixedName: true, persistentConfiguration: false } | ||
| ) | ||
| assert.strictEqual( | ||
| getConfigurationKey(station, OCPP_SUPERVISION_URL_KEY)?.value, | ||
| SUPERVISION_URL | ||
| ) | ||
| assert.strictEqual(station.stationInfo?.chargingStationId, 'FRESH#1') | ||
| assert.strictEqual(station.wsConnectionUrl.href, `${SUPERVISION_URL}/FRESH%231`) | ||
| }) | ||
|
|
||
| for (const transport of ['tcp', 'unix'] as const) { | ||
| // Unix domain sockets are not a filesystem path on Windows (`listen EACCES`); only | ||
| // the Unix transport case is skipped there, the TCP case runs everywhere. | ||
| const skip = transport === 'unix' && process.platform === 'win32' | ||
| await it( | ||
| `should let a real ${transport} WebSocket server receive one encoded identity segment`, | ||
| { skip }, | ||
| async () => { | ||
| const server = createServer() | ||
| const wss = new WebSocketServer({ server }) | ||
| const seen: string[] = [] | ||
| wss.on('connection', (socket, request) => { | ||
| seen.push(request.url ?? '') | ||
| socket.close() | ||
| }) | ||
| const socketPath = join(tmpdir(), `sim-${String(process.pid)}-${String(Date.now())}.sock`) | ||
| await new Promise<void>(resolve => { | ||
| if (transport === 'unix') { | ||
| server.listen(socketPath, resolve) | ||
| } else { | ||
| server.listen(0, '127.0.0.1', resolve) | ||
| } | ||
| }) | ||
| try { | ||
| const address = server.address() | ||
| const supervisionUrl = | ||
| transport === 'unix' || address == null || typeof address === 'string' | ||
| ? `ws+unix://${socketPath}:/OCPP16` | ||
| : `ws://127.0.0.1:${String(address.port)}/OCPP16` | ||
| for (const identity of Object.keys(RESERVED_IDENTITIES)) { | ||
| const station = createStationNamed(identity, supervisionUrl) | ||
| const ws = new WebSocket(station.wsConnectionUrl) | ||
| await new Promise<void>((resolve, reject) => { | ||
| ws.once('open', resolve) | ||
| ws.once('error', reject) | ||
| }) | ||
| ws.terminate() | ||
| assert.strictEqual(seen.at(-1), `/OCPP16/${RESERVED_IDENTITIES[identity]}`, identity) | ||
| } | ||
| } finally { | ||
| wss.close() | ||
| await new Promise<void>(resolve => { | ||
| server.close(() => { | ||
| resolve() | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,18 @@ | ||
| /** | ||
| * Removes a trailing `/<stationId>` suffix from a supervision URL. | ||
| * | ||
| * The connection URL published by the simulator ends with the station identity | ||
| * percent-encoded as one path segment (`encodeURIComponent`), so the encoded | ||
| * form is matched first; the raw form is kept for URLs that were composed | ||
| * without encoding. | ||
| * @param url - The supervision URL potentially containing the station ID | ||
| * @param stationId - The station ID to strip | ||
| * @returns The URL without the trailing station ID suffix | ||
| */ | ||
| export function stripStationId (url: string, stationId: string): string { | ||
| if (stationId.length === 0) return url | ||
| const suffix = `/${stationId}` | ||
| return url.endsWith(suffix) ? url.slice(0, -suffix.length) : url | ||
| for (const suffix of [`/${encodeURIComponent(stationId)}`, `/${stationId}`]) { | ||
| if (url.endsWith(suffix)) return url.slice(0, -suffix.length) | ||
| } | ||
| return url | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.