Skip to content

Commit 584c590

Browse files
[java] Handle bad gateway response from the server (#9444)
Co-authored-by: David Burns <[email protected]>
1 parent 39ba33e commit 584c590

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

java/client/src/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodec.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static com.google.common.base.Strings.nullToEmpty;
2121
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
22+
import static java.net.HttpURLConnection.HTTP_BAD_GATEWAY;
2223
import static java.net.HttpURLConnection.HTTP_BAD_METHOD;
2324
import static java.net.HttpURLConnection.HTTP_GATEWAY_TIMEOUT;
2425
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
@@ -90,7 +91,8 @@ public Response decode(HttpResponse encodedResponse) {
9091
if (HTTP_BAD_METHOD == encodedResponse.getStatus()) {
9192
response.setStatus(ErrorCodes.UNKNOWN_COMMAND);
9293
response.setValue(content);
93-
} else if (HTTP_GATEWAY_TIMEOUT == encodedResponse.getStatus()) {
94+
} else if (HTTP_GATEWAY_TIMEOUT == encodedResponse.getStatus() ||
95+
HTTP_BAD_GATEWAY == encodedResponse.getStatus()) {
9496
response.setStatus(ErrorCodes.UNHANDLED_ERROR);
9597
response.setValue(content);
9698
} else {

java/client/test/org/openqa/selenium/remote/codec/w3c/W3CHttpResponseCodecTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.openqa.selenium.remote.codec.w3c;
1919

20+
import static java.net.HttpURLConnection.HTTP_BAD_GATEWAY;
21+
import static java.net.HttpURLConnection.HTTP_GATEWAY_TIMEOUT;
2022
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
2123
import static java.net.HttpURLConnection.HTTP_OK;
2224
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -58,6 +60,56 @@ public void noErrorNoCry() {
5860
assertThat(decoded.getValue()).isEqualTo("cheese");
5961
}
6062

63+
@Test
64+
public void shouldBeAbleToHandleGatewayTimeoutError() {
65+
String responseString = "<html>\r\n" +
66+
"<body>\r\n" +
67+
"<h1>504 Gateway Time-out</h1>\r\n" +
68+
"The server didn't respond in time.\r\n" +
69+
"</body>\r\n" +
70+
"</html>";
71+
72+
byte[] contents = responseString.getBytes(UTF_8);
73+
74+
HttpResponse response = new HttpResponse();
75+
response.setStatus(HTTP_GATEWAY_TIMEOUT);
76+
response.addHeader("Server", "nginx");
77+
response.addHeader("Content-Type", "text/html");
78+
response.addHeader("Content-Length", String.valueOf(contents.length));
79+
response.setContent(bytes(contents));
80+
81+
Response decoded = new W3CHttpResponseCodec().decode(response);
82+
83+
assertThat(decoded.getStatus().intValue()).isEqualTo(ErrorCodes.UNHANDLED_ERROR);
84+
assertThat(decoded.getValue()).isEqualTo(responseString);
85+
}
86+
87+
88+
@Test
89+
public void shouldBeAbleToHandleBadGatewayError() {
90+
String responseString = "<html>\r\n" +
91+
"<head><title>502 Bad Gateway</title></head>\r\n" +
92+
"<body>\r\n" +
93+
"<center><h1>502 Bad Gateway</h1></center>\r\n" +
94+
"<hr><center>nginx</center>\r\n" +
95+
"</body>\r\n" +
96+
"</html>";
97+
98+
byte[] contents = responseString.getBytes(UTF_8);
99+
100+
HttpResponse response = new HttpResponse();
101+
response.setStatus(HTTP_BAD_GATEWAY);
102+
response.addHeader("Server", "nginx");
103+
response.addHeader("Content-Type", "text/html");
104+
response.addHeader("Content-Length", String.valueOf(contents.length));
105+
response.setContent(bytes(contents));
106+
107+
Response decoded = new W3CHttpResponseCodec().decode(response);
108+
109+
assertThat(decoded.getStatus().intValue()).isEqualTo(ErrorCodes.UNHANDLED_ERROR);
110+
assertThat(decoded.getValue()).isEqualTo(responseString);
111+
}
112+
61113
@Test
62114
public void decodingAnErrorWithoutAStacktraceIsDecodedProperlyForNonCompliantImplementations() {
63115
Map<String, Object> error = new HashMap<>();

0 commit comments

Comments
 (0)