Skip to content

Commit 620c9a6

Browse files
committed
Move generated js tests from a servlet to a handler
1 parent f2f89b8 commit 620c9a6

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

java/client/test/org/openqa/selenium/environment/webserver/GeneratedJsTestServlet.java renamed to java/client/test/org/openqa/selenium/environment/webserver/GeneratedJsTestHandler.java

+37-21
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,64 @@
1919

2020
import com.google.common.base.Strings;
2121
import com.google.common.net.MediaType;
22+
import org.openqa.selenium.remote.http.Contents;
23+
import org.openqa.selenium.remote.http.HttpHandler;
24+
import org.openqa.selenium.remote.http.HttpRequest;
25+
import org.openqa.selenium.remote.http.HttpResponse;
2226

27+
import javax.servlet.http.HttpServlet;
28+
import javax.servlet.http.HttpServletRequest;
29+
import javax.servlet.http.HttpServletResponse;
2330
import java.io.IOException;
2431
import java.io.OutputStream;
32+
import java.io.UncheckedIOException;
2533
import java.nio.charset.StandardCharsets;
2634

27-
import javax.servlet.http.HttpServlet;
28-
import javax.servlet.http.HttpServletRequest;
29-
import javax.servlet.http.HttpServletResponse;
35+
public class GeneratedJsTestHandler implements HttpHandler {
3036

31-
public class GeneratedJsTestServlet extends HttpServlet {
37+
private final String stripPrefix;
38+
39+
public GeneratedJsTestHandler(String stripPrefix) {
40+
this.stripPrefix = stripPrefix;
41+
}
3242

3343
@Override
34-
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
35-
throws IOException {
36-
String symbol = Strings.nullToEmpty(req.getPathInfo()).replace("../", "").replace("/", "$");
37-
byte[] data =
44+
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
45+
String path = req.getUri();
46+
if (path.startsWith(stripPrefix)) {
47+
path = path.substring(stripPrefix.length());
48+
}
49+
String symbol = path.replace("../", "").replace("/", "$");
50+
String data =
3851
("<!DOCTYPE html>\n"
3952
+ "<html>\n"
4053
+ "<head>\n"
4154
+ "<meta http-equiv=\"X-UA-Compatible\" content=\"IE-Edge\">\n"
4255
+ "<!-- File generated by " + getClass().getName() + " -->\n"
43-
+ "<title>" + req.getPathInfo() + "</title>\n"
56+
+ "<title>" + path + "</title>\n"
4457
+ "<script src=\"/third_party/closure/goog/base.js\"></script>\n"
4558
+ "<script src=\"/javascript/deps.js\"></script>\n"
4659
+ "<script>\n"
4760
+ " (function() {\n"
48-
+ " var path = '../../.." + req.getPathInfo() + "';\n"
61+
+ " var path = '../../.." + path + "';\n"
4962
+ " var loadFlags = goog.dependencies_.loadFlags[path];\n"
5063
+ " goog.addDependency(path, ['" + symbol + "'],\n"
51-
+ " goog.dependencies_.requires['../../.." + req.getPathInfo() + "'] || [],\n"
64+
+ " goog.dependencies_.requires['../../.." + path + "'] || [],\n"
5265
+ " (loadFlags && loadFlags['module'] == 'goog'));\n"
5366
+ " goog.require('" + symbol + "');\n"
5467
+ " })()\n"
55-
+ "</script></head><body></body></html>").getBytes(StandardCharsets.UTF_8);
56-
57-
resp.setStatus(HttpServletResponse.SC_OK);
58-
resp.setContentType(MediaType.HTML_UTF_8.toString());
59-
resp.setContentLength(data.length);
60-
61-
OutputStream stream = resp.getOutputStream();
62-
stream.write(data);
63-
stream.flush();
64-
stream.close();
68+
+ "</script></head><body></body></html>");
69+
//
70+
// resp.setStatus(HttpServletResponse.SC_OK);
71+
// resp.setContentType(MediaType.HTML_UTF_8.toString());
72+
// resp.setContentLength(data.length);
73+
//
74+
// OutputStream stream = resp.getOutputStream();
75+
// stream.write(data);
76+
// stream.flush();
77+
// stream.close();
78+
return new HttpResponse()
79+
.setHeader("Content-Type", MediaType.HTML_UTF_8.toString())
80+
.setContent(Contents.utf8String(data));
6581
}
6682
}

java/client/test/org/openqa/selenium/environment/webserver/JettyAppServer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {
140140
defaultContext.setInitParameter("webSrc", webSrc.toAbsolutePath().toString());
141141

142142
addServlet(defaultContext, "/quitquitquit", KillSwitchServlet.class);
143-
addServlet(defaultContext, "/generated/*", GeneratedJsTestServlet.class);
144143

145144
CreatePageHandler createPageHandler = new CreatePageHandler(
146145
tempPageDir.toPath(),
@@ -154,6 +153,7 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {
154153
Route.get("/basicAuth").to(BasicAuthHandler::new),
155154
Route.get("/cookie").to(CookieHandler::new),
156155
Route.get("/encoding").to(EncodingHandler::new),
156+
Route.matching(req -> req.getUri().startsWith("/generated/")).to(() -> new GeneratedJsTestHandler("/generated")),
157157
Route.matching(req -> req.getUri().startsWith("/page/") && req.getMethod() == GET).to(PageHandler::new),
158158
Route.post("/createPage").to(() -> createPageHandler),
159159
Route.get("/redirect").to(RedirectHandler::new),

0 commit comments

Comments
 (0)