Skip to content

Commit

Permalink
Return HTTP response in case session is not owned
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>
  • Loading branch information
VietND96 committed Oct 23, 2024
1 parent 09207df commit fca3b20
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit fca3b20

Please sign in to comment.