Skip to content

Commit 709aea8

Browse files
committed
Make unmatched websocket behaviour match requests
This sticks with the existing 503 response, but changes some of the details, includes a proper explanation in the body, and defines it more simply & directly (like the request equivalent).
1 parent 829999f commit 709aea8

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/rules/websockets/websocket-handlers.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,9 @@ export class ListenWebSocketHandler extends ListenWebSocketHandlerDefinition {
528528

529529
export class RejectWebSocketHandler extends RejectWebSocketHandlerDefinition {
530530

531-
async handle(req: OngoingRequest, socket: net.Socket, head: Buffer) {
531+
async handle(req: OngoingRequest, socket: net.Socket) {
532532
socket.write(rawResponse(this.statusCode, this.statusMessage, objectHeadersToRaw(this.headers)));
533-
if (this.body) socket.write(this.body);
534-
socket.write('\r\n');
533+
if (this.body) socket.end(this.body);
535534
socket.destroy();
536535
}
537536

src/server/mockttp-server.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ import {
8888
} from "../util/header-utils";
8989
import { AbortError } from "../rules/requests/request-handlers";
9090
import { WebSocketRuleData, WebSocketRule } from "../rules/websockets/websocket-rule";
91-
import { RejectWebSocketHandler, WebSocketHandler } from "../rules/websockets/websocket-handlers";
9291
import { SocksServerOptions } from "./socks-server";
9392

9493
type ExtendedRawRequest = (http.IncomingMessage | http2.Http2ServerRequest) & {
@@ -125,8 +124,6 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
125124

126125
private readonly initialDebugSetting: boolean;
127126

128-
private readonly defaultWsHandler!: WebSocketHandler;
129-
130127
constructor(options: MockttpOptions = {}) {
131128
super(options);
132129

@@ -139,8 +136,6 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
139136
this.maxBodySize = options.maxBodySize ?? Infinity;
140137
this.eventEmitter = new EventEmitter();
141138

142-
this.defaultWsHandler = new RejectWebSocketHandler(503, "Request for unmocked endpoint");
143-
144139
this.app = connect();
145140

146141
if (this.corsOptions) {
@@ -858,18 +853,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
858853
: undefined
859854
});
860855
} else {
861-
// Unmatched requests get passed through untouched automatically. This exists for
862-
// historical/backward-compat reasons, to match the initial WS implementation, and
863-
// will probably be removed to match handleRequest in future.
864-
await this.defaultWsHandler.handle(
865-
request as OngoingRequest & http.IncomingMessage,
866-
socket,
867-
head,
868-
{ emitEventCallback: (this.eventEmitter.listenerCount('rule-event') !== 0)
869-
? (type, event) => this.announceRuleEventAsync(request.id, nextRule!.id, type, event)
870-
: undefined
871-
}
872-
);
856+
await this.sendUnmatchedWebSocketError(request, socket, head);
873857
}
874858
} catch (e) {
875859
if (e instanceof AbortError) {
@@ -951,6 +935,23 @@ ${await this.suggestRule(request)}`
951935
response.end(await this.getUnmatchedRequestExplanation(request));
952936
}
953937

938+
private async sendUnmatchedWebSocketError(
939+
request: OngoingRequest,
940+
socket: net.Socket,
941+
head: Buffer
942+
) {
943+
const errorBody = await this.getUnmatchedRequestExplanation(request);
944+
socket.on('error', () => {}); // Best efforts, we don't care about failures here.
945+
socket.end([
946+
'HTTP/1.1 503 Request for unmocked endpoint',
947+
'Connection: close',
948+
'Content-Type: text/plain'
949+
].join('\r\n') +
950+
'\r\n\r\n' +
951+
errorBody);
952+
socket.destroy();
953+
}
954+
954955
private async sendWebSocketErrorResponse(socket: net.Socket, error: unknown) {
955956
if (socket.writable) {
956957
socket.end(

0 commit comments

Comments
 (0)