Skip to content

Commit

Permalink
Ensure that we forward metadata in the new session payload
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Nov 6, 2017
1 parent 6c27816 commit b66efc8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
29 changes: 27 additions & 2 deletions java/client/src/org/openqa/selenium/remote/NewSessionPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ public void writeTo(Appendable appendable) throws IOException {

Map<String, Object> first = getOss();
if (first == null) {
//noinspection unchecked
first = (Map<String, Object>) stream().findFirst()
first = stream().findFirst()
.orElse(new ImmutableCapabilities())
.asMap();
}
Expand All @@ -246,10 +245,36 @@ public void writeTo(Appendable appendable) throws IOException {
json.endArray();

json.endObject(); // Close "capabilities" object

writeMetaData(json);

json.endObject();
}
}

private void writeMetaData(JsonOutput out) throws IOException {
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
try (Reader reader = charSource.openBufferedStream();
JsonInput input = json.newInput(reader)) {
input.beginObject();
while (input.hasNext()) {
String name = input.nextName();
switch (name) {
case "capabilities":
case "desiredCapabilities":
case "requiredCapabilities":
input.skipValue();
break;

default:
out.name(name);
out.write(input.<Object>read(Object.class), Object.class);
break;
}
}
}
}

private void streamW3CProtocolParameters(JsonOutput out, Map<String, Object> des) {
// Technically we should be building up a combination of "alwaysMatch" and "firstMatch" options.
// We're going to do a little processing to figure out what we might be able to do, and assume
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.openqa.selenium.json.Json.MAP_TYPE;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -211,6 +213,34 @@ public void convertEverythingToFirstMatchOnlyifPayloadContainsAlwaysMatchSection
capabilities);
}

@Test
public void forwardsMetaDataAssociatedWithARequest() throws IOException {
try (NewSessionPayload payload = NewSessionPayload.create( ImmutableMap.of(
"desiredCapabilities", ImmutableMap.of(),
"cloud:user", "bob",
"cloud:key", "there is no cake"))) {
StringBuilder toParse = new StringBuilder();
payload.writeTo(toParse);
Map<String, Object> seen = new Json().toType(toParse.toString(), MAP_TYPE);

assertEquals("bob", seen.get("cloud:user"));
assertEquals("there is no cake", seen.get("cloud:key"));
}
}

@Test
public void doesNotForwardRequiredCapabilitiesAsTheseAreVeryLegacy() throws IOException {
try (NewSessionPayload payload = NewSessionPayload.create( ImmutableMap.of(
"capabilities", ImmutableMap.of(),
"requiredCapabilities", ImmutableMap.of("key", "so it's not empty")))) {
StringBuilder toParse = new StringBuilder();
payload.writeTo(toParse);
Map<String, Object> seen = new Json().toType(toParse.toString(), MAP_TYPE);

assertNull(seen.get("requiredCapabilities"));
}
}

private List<Capabilities> create(Map<String, ?> source) throws IOException {
List<Capabilities> presumablyFromMemory;
List<Capabilities> fromDisk;
Expand Down

0 comments on commit b66efc8

Please sign in to comment.