Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

[FEATURE REQUEST] Allow Complex WebSocket Request Mapping #518

@IgnacioPuigMargalef

Description

@IgnacioPuigMargalef

Hi, thanks for your excellent work on stubby4j, it's been extremely useful in our testing workflows!

While reviewing the WebSocket string message handling, I noticed the current implementation relies on an exact match using Arrays.equals(...) to compare incoming string payloads:

Class 'StubsServerWebSocket' in the library:

@OnWebSocketMessage
    public void onWebSocketText(final String message) {
        final ServletUpgradeRequest upgradeRequest = (ServletUpgradeRequest) this.session.getUpgradeRequest();
        ConsoleUtils.logIncomingWebSocketTextRequest(upgradeRequest, message);

        boolean found = false;
        final List<StubWebSocketOnMessageLifeCycle> onMessage = stubWebSocketConfig.getOnMessage();
        for (final StubWebSocketOnMessageLifeCycle lifeCycle : onMessage) {
            final StubWebSocketClientRequest clientRequest = lifeCycle.getClientRequest();
            final StubWebSocketServerResponse serverResponse = lifeCycle.getServerResponse(true);

            if (clientRequest.getBodyAsString().equals(message.trim())) {
                found = true;
                dispatchServerResponse(serverResponse);
                break;
            }
        }

        if (!found) {
            this.remote.sendStringByFuture(String.format("404 Not Found: client request %s", message));
        }
    }

In practice, this strict comparison makes it hard to stub binary responses reliably when the incoming payload has dynamic parts (e.g., timestamps, IDs).

It would be great to allow more flexible matching strategies for WebSocket text messages, simply using 'String.matches(...)' or any strategy.

Please let me know if you're able to make this change, or if I should submit a pull request for you to review as a potential improvement to your library.

Thanks again!

Nacho

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions