Skip to content

Commit fca3b20

Browse files
committed
Return HTTP response in case session is not owned
Signed-off-by: Viet Nguyen Duc <[email protected]>
1 parent 09207df commit fca3b20

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

java/src/org/openqa/selenium/grid/node/ForwardWebDriverCommand.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
package org.openqa.selenium.grid.node;
1919

20+
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
2021
import static org.openqa.selenium.remote.HttpSessionId.getSessionId;
22+
import static org.openqa.selenium.remote.http.Contents.asJson;
2123

22-
import org.openqa.selenium.NoSuchSessionException;
24+
import com.google.common.collect.ImmutableMap;
2325
import org.openqa.selenium.internal.Require;
2426
import org.openqa.selenium.remote.SessionId;
2527
import org.openqa.selenium.remote.http.HttpHandler;
@@ -45,6 +47,11 @@ public HttpResponse execute(HttpRequest req) {
4547
if (matches(req)) {
4648
return node.executeWebDriverCommand(req);
4749
}
48-
throw new NoSuchSessionException(String.format("Session not found in node %s", node.getId()));
50+
return new HttpResponse()
51+
.setStatus(HTTP_INTERNAL_ERROR)
52+
.setContent(
53+
asJson(
54+
ImmutableMap.of(
55+
"error", String.format("Session not found in node %s", node.getId()))));
4956
}
5057
}

java/test/org/openqa/selenium/grid/node/ForwardWebDriverCommandTest.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717

1818
package org.openqa.selenium.grid.node;
1919

20+
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
2021
import static org.junit.jupiter.api.Assertions.assertEquals;
21-
import static org.junit.jupiter.api.Assertions.assertThrows;
22-
import static org.junit.jupiter.api.Assertions.assertTrue;
2322
import static org.mockito.Mockito.*;
23+
import static org.openqa.selenium.remote.http.Contents.asJson;
2424

25+
import com.google.common.collect.ImmutableMap;
2526
import java.util.UUID;
2627
import org.junit.jupiter.api.BeforeEach;
2728
import org.junit.jupiter.api.Test;
28-
import org.openqa.selenium.NoSuchSessionException;
2929
import org.openqa.selenium.grid.data.NodeId;
3030
import org.openqa.selenium.remote.SessionId;
3131
import org.openqa.selenium.remote.http.HttpRequest;
@@ -66,11 +66,15 @@ void testExecuteWithInvalidSessionOwner() {
6666
SessionId sessionId = new SessionId("5678");
6767
when(mockNode.isSessionOwner(sessionId)).thenReturn(false);
6868

69-
NoSuchSessionException exception =
70-
assertThrows(NoSuchSessionException.class, () -> command.execute(mockRequest));
71-
assertTrue(
72-
exception
73-
.getMessage()
74-
.startsWith(String.format("Session not found in node %s", mockNode.getId())));
69+
HttpResponse actualResponse = command.execute(mockRequest);
70+
HttpResponse expectResponse =
71+
new HttpResponse()
72+
.setStatus(HTTP_INTERNAL_ERROR)
73+
.setContent(
74+
asJson(
75+
ImmutableMap.of(
76+
"error", String.format("Session not found in node %s", mockNode.getId()))));
77+
assertEquals(expectResponse.getStatus(), actualResponse.getStatus());
78+
assertEquals(expectResponse.getContentEncoding(), actualResponse.getContentEncoding());
7579
}
7680
}

0 commit comments

Comments
 (0)