WebSocketHandler { public boolean checkWebSocket(HTTPMethod method, HeaderMap headers) { if (method == HTTPMethod.GET && headers.getFirst(Headers.SEC_WEB_SOCKET_KEY) != null) { String version = headers.getFirst(Headers.SEC_WEB_SOCKET_VERSION); if ("13".equals(version)) return true; throw new BadRequestException("only support web socket version 13, version=" + version, "INVALID_HTTP_REQUEST"); } return false; } WebSocketHandler(LogManager logManager, SessionManager sessionManager, RateControl rateControl); boolean checkWebSocket(HTTPMethod method, HeaderMap headers); void handle(HttpServerExchange exchange, RequestImpl request, ActionLog actionLog); void shutdown(); void add(String path, ChannelHandler<?, ?> handler); final WebSocketContextImpl context; }
@Test void checkWebSocket() { var headers = new HeaderMap() .put(Headers.SEC_WEB_SOCKET_KEY, "xxx") .put(Headers.SEC_WEB_SOCKET_VERSION, "13"); assertThat(handler.checkWebSocket(HTTPMethod.GET, headers)).isTrue(); assertThat(handler.checkWebSocket(HTTPMethod.PUT, headers)).isFalse(); assertThatThrownBy(() -> handler.checkWebSocket(HTTPMethod.GET, headers.put(Headers.SEC_WEB_SOCKET_VERSION, "07"))) .isInstanceOf(BadRequestException.class) .hasMessageContaining("only support web socket version 13"); }