Skip to content

Commit 978350b

Browse files
tommywolukeis
authored andcommitted
Allow using query string in a GET request to HubStatusServlet (#2771)
1 parent 5772b63 commit 978350b

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

java/server/src/org/openqa/grid/web/servlet/HubStatusServlet.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.io.IOException;
3434
import java.io.InputStreamReader;
3535
import java.util.ArrayList;
36+
import java.util.Arrays;
3637
import java.util.List;
3738
import java.util.Map;
3839

@@ -55,9 +56,9 @@
5556
* ]
5657
* }
5758
*
58-
* if no param is specified, all params known to the hub are returned.
59+
* alternatively you can use a query string ?configuration=timeout,servlets
5960
*
60-
* {"configuration": [] }
61+
* if no param is specified, all params known to the hub are returned.
6162
*
6263
*/
6364
public class HubStatusServlet extends RegistryBasedServlet {
@@ -101,7 +102,10 @@ private JsonObject getResponse(HttpServletRequest request) throws IOException {
101102
if (request.getInputStream() != null) {
102103
JsonObject requestJSON = getRequestJSON(request);
103104
List<String> keysToReturn = null;
104-
if (requestJSON != null && requestJSON.has("configuration")) {
105+
106+
if (request.getParameter("configuration") != null && !"".equals(request.getParameter("configuration"))) {
107+
keysToReturn = Arrays.asList(request.getParameter("configuration").split(","));
108+
} else if (requestJSON != null && requestJSON.has("configuration")) {
105109
keysToReturn = new Gson().fromJson(requestJSON.getAsJsonArray("configuration"), ArrayList.class);
106110
}
107111

java/server/test/org/openqa/grid/internal/StatusServletTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import java.io.InputStreamReader;
5353
import java.net.URL;
5454
import java.net.URLEncoder;
55+
import java.util.ArrayList;
5556
import java.util.HashMap;
5657
import java.util.Map;
5758

@@ -273,6 +274,34 @@ public void testHubGetSpecifiedConfig() throws IOException {
273274

274275
}
275276

277+
/**
278+
* if a certain set of parameters are requested to the hub, only those params are returned.
279+
* @throws IOException
280+
*/
281+
@Test
282+
public void testHubGetSpecifiedConfigWithQueryString() throws IOException {
283+
284+
HttpClient client = httpClientFactory.getHttpClient();
285+
286+
ArrayList<String> keys = new ArrayList<>();
287+
keys.add(URLEncoder.encode("timeout", "UTF-8"));
288+
keys.add(URLEncoder.encode("I'm not a valid key", "UTF-8"));
289+
keys.add(URLEncoder.encode("servlets", "UTF-8"));
290+
291+
String query = "?configuration=" + String.join(",",keys);
292+
String url = hubApi.toExternalForm() + query ;
293+
BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("GET", url);
294+
295+
HttpResponse response = client.execute(host, r);
296+
assertEquals(200, response.getStatusLine().getStatusCode());
297+
JsonObject o = extractObject(response);
298+
299+
assertTrue(o.get("success").getAsBoolean());
300+
assertEquals(12345, o.get("timeout").getAsInt());
301+
assertNull(o.get("I'm not a valid key"));
302+
assertTrue(o.getAsJsonArray("servlets").size() == 0);
303+
assertFalse(o.has("capabilityMatcher"));
304+
}
276305

277306
/**
278307
* when no param is specified, a call to the hub API returns all the config params the hub

0 commit comments

Comments
 (0)