Skip to content

Commit b66efc8

Browse files
committed
Ensure that we forward metadata in the new session payload
1 parent 6c27816 commit b66efc8

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

java/client/src/org/openqa/selenium/remote/NewSessionPayload.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ public void writeTo(Appendable appendable) throws IOException {
219219

220220
Map<String, Object> first = getOss();
221221
if (first == null) {
222-
//noinspection unchecked
223-
first = (Map<String, Object>) stream().findFirst()
222+
first = stream().findFirst()
224223
.orElse(new ImmutableCapabilities())
225224
.asMap();
226225
}
@@ -246,10 +245,36 @@ public void writeTo(Appendable appendable) throws IOException {
246245
json.endArray();
247246

248247
json.endObject(); // Close "capabilities" object
248+
249+
writeMetaData(json);
250+
249251
json.endObject();
250252
}
251253
}
252254

255+
private void writeMetaData(JsonOutput out) throws IOException {
256+
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
257+
try (Reader reader = charSource.openBufferedStream();
258+
JsonInput input = json.newInput(reader)) {
259+
input.beginObject();
260+
while (input.hasNext()) {
261+
String name = input.nextName();
262+
switch (name) {
263+
case "capabilities":
264+
case "desiredCapabilities":
265+
case "requiredCapabilities":
266+
input.skipValue();
267+
break;
268+
269+
default:
270+
out.name(name);
271+
out.write(input.<Object>read(Object.class), Object.class);
272+
break;
273+
}
274+
}
275+
}
276+
}
277+
253278
private void streamW3CProtocolParameters(JsonOutput out, Map<String, Object> des) {
254279
// Technically we should be building up a combination of "alwaysMatch" and "firstMatch" options.
255280
// We're going to do a little processing to figure out what we might be able to do, and assume

java/server/test/org/openqa/selenium/remote/server/NewSessionPayloadTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020

2121
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertNull;
2223
import static org.junit.Assert.fail;
24+
import static org.openqa.selenium.json.Json.MAP_TYPE;
2325

2426
import com.google.common.collect.ImmutableList;
2527
import com.google.common.collect.ImmutableMap;
@@ -211,6 +213,34 @@ public void convertEverythingToFirstMatchOnlyifPayloadContainsAlwaysMatchSection
211213
capabilities);
212214
}
213215

216+
@Test
217+
public void forwardsMetaDataAssociatedWithARequest() throws IOException {
218+
try (NewSessionPayload payload = NewSessionPayload.create( ImmutableMap.of(
219+
"desiredCapabilities", ImmutableMap.of(),
220+
"cloud:user", "bob",
221+
"cloud:key", "there is no cake"))) {
222+
StringBuilder toParse = new StringBuilder();
223+
payload.writeTo(toParse);
224+
Map<String, Object> seen = new Json().toType(toParse.toString(), MAP_TYPE);
225+
226+
assertEquals("bob", seen.get("cloud:user"));
227+
assertEquals("there is no cake", seen.get("cloud:key"));
228+
}
229+
}
230+
231+
@Test
232+
public void doesNotForwardRequiredCapabilitiesAsTheseAreVeryLegacy() throws IOException {
233+
try (NewSessionPayload payload = NewSessionPayload.create( ImmutableMap.of(
234+
"capabilities", ImmutableMap.of(),
235+
"requiredCapabilities", ImmutableMap.of("key", "so it's not empty")))) {
236+
StringBuilder toParse = new StringBuilder();
237+
payload.writeTo(toParse);
238+
Map<String, Object> seen = new Json().toType(toParse.toString(), MAP_TYPE);
239+
240+
assertNull(seen.get("requiredCapabilities"));
241+
}
242+
}
243+
214244
private List<Capabilities> create(Map<String, ?> source) throws IOException {
215245
List<Capabilities> presumablyFromMemory;
216246
List<Capabilities> fromDisk;

0 commit comments

Comments
 (0)