diff --git a/java/src/org/openqa/selenium/grid/node/ForwardWebDriverCommand.java b/java/src/org/openqa/selenium/grid/node/ForwardWebDriverCommand.java index 4c8a03796c7db..fadcfff4c80fd 100644 --- a/java/src/org/openqa/selenium/grid/node/ForwardWebDriverCommand.java +++ b/java/src/org/openqa/selenium/grid/node/ForwardWebDriverCommand.java @@ -17,9 +17,11 @@ package org.openqa.selenium.grid.node; +import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR; import static org.openqa.selenium.remote.HttpSessionId.getSessionId; +import static org.openqa.selenium.remote.http.Contents.asJson; -import org.openqa.selenium.NoSuchSessionException; +import com.google.common.collect.ImmutableMap; import org.openqa.selenium.internal.Require; import org.openqa.selenium.remote.SessionId; import org.openqa.selenium.remote.http.HttpHandler; @@ -45,6 +47,11 @@ public HttpResponse execute(HttpRequest req) { if (matches(req)) { return node.executeWebDriverCommand(req); } - throw new NoSuchSessionException(String.format("Session not found in node %s", node.getId())); + return new HttpResponse() + .setStatus(HTTP_INTERNAL_ERROR) + .setContent( + asJson( + ImmutableMap.of( + "error", String.format("Session not found in node %s", node.getId())))); } } diff --git a/java/test/org/openqa/selenium/grid/node/ForwardWebDriverCommandTest.java b/java/test/org/openqa/selenium/grid/node/ForwardWebDriverCommandTest.java index 721db0b32c7d2..8f0df29251870 100644 --- a/java/test/org/openqa/selenium/grid/node/ForwardWebDriverCommandTest.java +++ b/java/test/org/openqa/selenium/grid/node/ForwardWebDriverCommandTest.java @@ -17,15 +17,15 @@ package org.openqa.selenium.grid.node; +import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.*; +import static org.openqa.selenium.remote.http.Contents.asJson; +import com.google.common.collect.ImmutableMap; import java.util.UUID; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.openqa.selenium.NoSuchSessionException; import org.openqa.selenium.grid.data.NodeId; import org.openqa.selenium.remote.SessionId; import org.openqa.selenium.remote.http.HttpRequest; @@ -66,11 +66,15 @@ void testExecuteWithInvalidSessionOwner() { SessionId sessionId = new SessionId("5678"); when(mockNode.isSessionOwner(sessionId)).thenReturn(false); - NoSuchSessionException exception = - assertThrows(NoSuchSessionException.class, () -> command.execute(mockRequest)); - assertTrue( - exception - .getMessage() - .startsWith(String.format("Session not found in node %s", mockNode.getId()))); + HttpResponse actualResponse = command.execute(mockRequest); + HttpResponse expectResponse = + new HttpResponse() + .setStatus(HTTP_INTERNAL_ERROR) + .setContent( + asJson( + ImmutableMap.of( + "error", String.format("Session not found in node %s", mockNode.getId())))); + assertEquals(expectResponse.getStatus(), actualResponse.getStatus()); + assertEquals(expectResponse.getContentEncoding(), actualResponse.getContentEncoding()); } }