Skip to content

Commit bc94505

Browse files
krmahadevandiemol
authored andcommitted
Fixing review comments
* Renamed the CLI arg to downloads-path * Download is available only via GET /session/{sessionId}/se/file * Files downloaded would be available ONLY as a zip file.
1 parent 6276055 commit bc94505

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

java/src/org/openqa/selenium/grid/node/Node.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,6 @@ protected Node(Tracer tracer, NodeId id, URI uri, Secret registrationSecret) {
150150
post("/session/{sessionId}/se/file")
151151
.to(params -> new UploadFile(this, sessionIdFrom(params)))
152152
.with(spanDecorator("node.upload_file")),
153-
get("/session/{sessionId}/file")
154-
.to(params -> new DownloadFile(this, sessionIdFrom(params)))
155-
.with(spanDecorator("node.download_file")),
156153
get("/session/{sessionId}/se/file")
157154
.to(params -> new DownloadFile(this, sessionIdFrom(params)))
158155
.with(spanDecorator("node.download_file")),

java/src/org/openqa/selenium/grid/node/config/NodeFlags.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@ public class NodeFlags implements HasRoles {
222222
private String nodeImplementation = DEFAULT_NODE_IMPLEMENTATION;
223223

224224
@Parameter(
225-
names = {"--downloads-dir"},
225+
names = {"--downloads-path"},
226226
description = "The default location wherein all browser triggered file downloads would be "
227227
+ "available to be retrieved from. This is usually the directory that you configure in "
228228
+ "your browser as the default location for storing downloaded files.")
229-
@ConfigValue(section = NODE_SECTION, name = "downloads-dir", example = "")
230-
private String downloadsDir = "";
229+
@ConfigValue(section = NODE_SECTION, name = "downloads-path", example = "")
230+
private String downloadsPath = "";
231231

232232
@Override
233233
public Set<Role> getRoles() {

java/src/org/openqa/selenium/grid/node/config/NodeOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public Optional<URI> getPublicGridUri() {
149149
}
150150

151151
public Optional<String> getDownloadsDirectory() {
152-
return config.get(NODE_SECTION, "downloads-dir");
152+
return config.get(NODE_SECTION, "downloads-path");
153153
}
154154

155155
public Node getNode() {

java/src/org/openqa/selenium/grid/node/local/LocalNode.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import org.openqa.selenium.io.Zip;
6464
import org.openqa.selenium.json.Json;
6565
import org.openqa.selenium.remote.SessionId;
66-
import org.openqa.selenium.remote.http.Contents;
6766
import org.openqa.selenium.remote.http.HttpRequest;
6867
import org.openqa.selenium.remote.http.HttpResponse;
6968
import org.openqa.selenium.remote.tracing.AttributeKey;
@@ -123,7 +122,7 @@ public class LocalNode extends Node {
123122
private final int maxSessionCount;
124123
private final int configuredSessionCount;
125124
private final boolean cdpEnabled;
126-
private final String downloadsDir;
125+
private final String downloadsPath;
127126

128127
private final boolean bidiEnabled;
129128
private final AtomicBoolean drainAfterSessions = new AtomicBoolean();
@@ -148,7 +147,7 @@ private LocalNode(
148147
Duration heartbeatPeriod,
149148
List<SessionSlot> factories,
150149
Secret registrationSecret,
151-
String downloadsDir) {
150+
String downloadsPath) {
152151
super(tracer, new NodeId(UUID.randomUUID()), uri, registrationSecret);
153152

154153
this.bus = Require.nonNull("Event bus", bus);
@@ -165,7 +164,7 @@ private LocalNode(
165164
this.sessionCount.set(drainAfterSessionCount);
166165
this.cdpEnabled = cdpEnabled;
167166
this.bidiEnabled = bidiEnabled;
168-
this.downloadsDir = Optional.ofNullable(downloadsDir).orElse("");
167+
this.downloadsPath = Optional.ofNullable(downloadsPath).orElse("");
169168

170169
this.healthCheck = healthCheck == null ?
171170
() -> {
@@ -180,10 +179,8 @@ private LocalNode(
180179
.ticker(ticker)
181180
.removalListener((RemovalListener<SessionId, TemporaryFilesystem>) notification -> {
182181
TemporaryFilesystem tempFS = notification.getValue();
183-
if (tempFS != null) {
184-
tempFS.deleteTemporaryFiles();
185-
tempFS.deleteBaseDir();
186-
}
182+
tempFS.deleteTemporaryFiles();
183+
tempFS.deleteBaseDir();
187184
})
188185
.build();
189186

@@ -485,17 +482,18 @@ public HttpResponse downloadFile(HttpRequest req, SessionId id) {
485482
if (slot != null && slot.getSession() instanceof DockerSession) {
486483
return executeWebDriverCommand(req);
487484
}
488-
if (this.downloadsDir.isEmpty()) {
489-
throw new WebDriverException(
490-
"Please specify the directory that would contain downloaded files and restart the node.");
485+
if (this.downloadsPath.isEmpty()) {
486+
String msg = "Please specify the path wherein the files downloaded using the browser "
487+
+ "would be available via the command line arg [--downloads-path] and restart the node";
488+
throw new WebDriverException(msg);
491489
}
492-
File dir = new File(this.downloadsDir);
490+
File dir = new File(this.downloadsPath);
493491
if (!dir.exists()) {
494492
throw new WebDriverException(
495-
String.format("Cannot locate downloads directory %s.", downloadsDir));
493+
String.format("Cannot locate downloads directory %s.", downloadsPath));
496494
}
497495
if (!dir.isDirectory()) {
498-
throw new WebDriverException(String.format("Invalid directory: %s.",downloadsDir));
496+
throw new WebDriverException(String.format("Invalid directory: %s.", downloadsPath));
499497
}
500498
String filename = req.getQueryParameter("filename");
501499
try {
@@ -504,13 +502,13 @@ public HttpResponse downloadFile(HttpRequest req, SessionId id) {
504502
).orElse(new File[]{});
505503
if (allFiles.length == 0) {
506504
throw new WebDriverException(
507-
String.format("Cannot find file [%s] in directory %s.", filename, downloadsDir));
505+
String.format("Cannot find file [%s] in directory %s.", filename, downloadsPath));
508506
}
509507
if (allFiles.length != 1) {
510508
throw new WebDriverException(
511509
String.format("Expected there to be only 1 file. There were: %s.", allFiles.length));
512510
}
513-
String content = Contents.string(allFiles[0]);
511+
String content = Zip.zip(allFiles[0]);
514512
ImmutableMap<String, Object> result = ImmutableMap.of(
515513
"filename", filename,
516514
"contents", content);

java/test/org/openqa/selenium/grid/node/NodeTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import com.google.common.collect.ImmutableMap;
3131
import com.google.common.collect.ImmutableSet;
3232

33-
import java.util.Base64;
33+
import java.nio.file.Path;
3434
import org.junit.jupiter.api.BeforeEach;
3535
import org.junit.jupiter.api.Test;
3636
import org.openqa.selenium.Capabilities;
@@ -495,15 +495,17 @@ void canDownloadAFile() throws IOException {
495495
Session session = response.right().getSession();
496496
String hello = "Hello, world!";
497497

498-
HttpRequest req = new HttpRequest(GET, String.format("/session/%s/file", session.getId()));
498+
HttpRequest req = new HttpRequest(GET, String.format("/session/%s/se/file", session.getId()));
499499
File zip = createTmpFile(hello);
500500
req.addQueryParameter("filename", zip.getName());
501501
HttpResponse rsp = node.execute(req);
502502
node.stop(session.getId());
503503
Map<String, Object> map = new Json().toType(string(rsp), Json.MAP_TYPE);
504504
File baseDir = getTemporaryFilesystemBaseDir(TemporaryFilesystem.getDefaultTmpFS());
505505
String encodedContents = map.get("contents").toString();
506-
String decodedContents = new String(Base64.getMimeDecoder().decode(encodedContents));
506+
Zip.unzip(encodedContents, baseDir);
507+
Path path = new File(baseDir.getAbsolutePath() + "/" + map.get("filename")).toPath();
508+
String decodedContents = String.join("", Files.readAllLines(path));
507509
assertThat(decodedContents).isEqualTo(hello);
508510
}
509511

0 commit comments

Comments
 (0)