Skip to content

Commit bf64d6c

Browse files
committed
updated the selenium test pages app to be deployable to heroku
fixed the find_by_playground and disabled the file upload and shutdown for server deplou
1 parent d3886d0 commit bf64d6c

13 files changed

Lines changed: 128 additions & 53 deletions

File tree

java/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/upload/

java/pom.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>uk.co.compendiumdev.testing</groupId>
88
<artifactId>testapp</artifactId>
99
<packaging>pom</packaging>
10-
<version>1-3-5</version>
10+
<version>1-3-6</version>
1111
<!-- <version>1.3</version>-->
1212

1313
<!--
@@ -42,8 +42,9 @@ v1.4 - split into multiple pom.xnl
4242

4343
<!--
4444
1.3.1 has a fix for search engine page because the javascript for cookies was not loaded
45+
1.3.2 fixed find_by_playground, and updated for heroku release
4546
-->
46-
<seleniumtestpages.version>1-3-1</seleniumtestpages.version>
47+
<seleniumtestpages.version>1-3-2</seleniumtestpages.version>
4748
<testingappsroot.version>1-1-0</testingappsroot.version>
4849

4950
<!-- Last release was...
@@ -67,7 +68,8 @@ v1.4 - split into multiple pom.xnl
6768
<!-- 1.3.3 has compendiumdevapps 1.1.1 -->
6869
<!-- 1-3-4 has restlisticator 1-2-1 and thepulper 1-2-3 -->
6970
<!-- 1-3-5 has updated to thepulper 1-2-4 -->
70-
<collectedapp.version>1-3-5</collectedapp.version>
71+
<!-- 1-3-6 has updated the selenium test pages to work with heroku -->
72+
<collectedapp.version>1-3-6</collectedapp.version>
7173

7274

7375
<herokumaven.version>1.2.0</herokumaven.version>

java/testingapps/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>uk.co.compendiumdev.testing</groupId>
77
<artifactId>testapp</artifactId>
8-
<version>1-3-5</version>
8+
<version>1-3-6</version>
99
</parent>
1010

1111
<groupId>uk.co.compendiumdev.testing</groupId>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: java $JAVA_OPTS -cp target/classes:target/dependency/*:target/* com.seleniumsimplified.MainTestPages
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: java -cp ./target/seleniumtestpages-1-3-2-jar-with-dependencies.jar com.seleniumsimplified.MainTestPages
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# deploy to heroku
2+
3+
Development notes.
4+
5+
First install heroku
6+
7+
https://devcenter.heroku.com/articles/heroku-cli
8+
9+
I'm on mac
10+
11+
- `brew tap heroku/brew && brew install heroku`
12+
- `heroku plugins:install heroku-cli-deploy`
13+
- `heroku login`
14+
- `mvn package`
15+
- `heroku deploy:jar target/seleniumtestpages-1-3-2-jar-with-dependencies.jar --app testpages`
16+
17+
had to add `:target/*` to the procfile when deploying fat app
18+
19+
after deployment:
20+
21+
https://testpages.herokuapp.com
22+
23+
24+
To run locally
25+
26+
`heroku local -f Procfile.mac.local`

java/testingapps/seleniumtestpages/src/main/java/com/seleniumsimplified/MainTestPages.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,29 @@
88

99
import static spark.Spark.*;
1010

11-
/**
12-
* Created by Alan on 15/06/2016.
13-
*/
1411
public class MainTestPages {
1512

13+
static boolean hasHerokuAssignedPort(){
14+
ProcessBuilder processBuilder = new ProcessBuilder();
15+
return (processBuilder.environment().get("PORT") != null);
16+
}
17+
18+
static int getHerokuAssignedPort() {
19+
ProcessBuilder processBuilder = new ProcessBuilder();
20+
if (hasHerokuAssignedPort()) {
21+
return Integer.parseInt(processBuilder.environment().get("PORT"));
22+
}
23+
return -1; //return default port if heroku-port isn't set (i.e. on localhost)
24+
}
25+
1626
public static void main(String[] args) {
1727

1828
Integer proxyport = 4567; // default for spark
1929

30+
if(hasHerokuAssignedPort()) {
31+
proxyport = getHerokuAssignedPort();
32+
}
33+
2034
for(String arg : args){
2135
if(arg.startsWith("-port")){
2236
String[]details = arg.split("=");
@@ -35,7 +49,15 @@ public static void main(String[] args) {
3549

3650

3751
// add a shutdown url in case left running on port 4567
38-
get("/shutdown", (req, res) -> {System.exit(0); return "";});
52+
String envVar = System.getenv("SELENIUM_TEST_PAGES_DISALLOW_SHUTDOWN");
53+
if(envVar != null && envVar.length()>0) {
54+
// shutdown is not enabled
55+
}else{
56+
get("/shutdown", (req, res) -> {
57+
System.exit(0);
58+
return "";
59+
});
60+
}
3961

4062

4163
// because Spark is a singleton - these just have to register their routings, they don't need to do anything else

java/testingapps/seleniumtestpages/src/main/java/com/seleniumsimplified/seleniumtestpages/spark/app/FileUploadProcessor.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.seleniumsimplified.seleniumtestpages.spark.app;
22

3-
import org.eclipse.jetty.util.MultiMap;
4-
import org.eclipse.jetty.util.UrlEncoded;
53
import spark.Request;
64
import spark.Response;
75

@@ -18,27 +16,31 @@ public class FileUploadProcessor {
1816

1917
private final Request req;
2018
private final Response res;
19+
private final Boolean allowupload;
2120
private StringBuilder html;
2221

23-
public FileUploadProcessor(Request req, Response res) {
22+
public FileUploadProcessor(Request req, Response res, Boolean allowupload) {
2423
this.req = req;
2524
this.res = res;
25+
this.allowupload = allowupload; // need to configure not uploading when deployed to server
2626
}
2727

2828
public String post() {
2929
html = new StringBuilder();
3030

31+
File uploadDir;
32+
Path tempFile=null;
3133

32-
File uploadDir = new File("upload");
33-
// create the upload directory if it does not exist
34-
uploadDir.mkdir();
34+
if(allowupload) {
35+
uploadDir = new File("upload");
36+
// create the upload directory if it does not exist
37+
uploadDir.mkdir();
3538

36-
37-
Path tempFile = null;
38-
try {
39-
tempFile = Files.createTempFile(uploadDir.toPath(), "", "");
40-
} catch (IOException e) {
41-
e.printStackTrace();
39+
try {
40+
tempFile = Files.createTempFile(uploadDir.toPath(), "", "");
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
}
4244
}
4345

4446
req.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
@@ -47,30 +49,35 @@ public String post() {
4749
File uploadedFile=null;
4850
boolean isImage = false;
4951

50-
try (InputStream input = req.raw().getPart("filename").getInputStream()) { // getPart needs to use same "name" as input field in form
52+
String reportedName = "NoFileUploadsAllowed.txt";
5153

52-
Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING);
54+
if(allowupload) {
55+
try (InputStream input = req.raw().getPart("filename").getInputStream()) { // getPart needs to use same "name" as input field in form
5356

54-
uploadedFile = new File(tempFile.toFile().getPath() + "_" + req.raw().getPart("filename").getSubmittedFileName());
55-
tempFile.toFile().renameTo(uploadedFile);
57+
Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING);
5658

57-
if(req.raw().getPart("filename").getContentType().contains("image")){
58-
isImage=true;
59-
}
59+
uploadedFile = new File(tempFile.toFile().getPath() + "_" + req.raw().getPart("filename").getSubmittedFileName());
60+
tempFile.toFile().renameTo(uploadedFile);
6061

61-
} catch (IOException e) {
62-
e.printStackTrace();
63-
} catch (ServletException e) {
64-
e.printStackTrace();
65-
}
62+
if (req.raw().getPart("filename").getContentType().contains("image")) {
63+
isImage = true;
64+
}
6665

66+
reportedName = uploadedFile.getName();
67+
68+
} catch (IOException e) {
69+
e.printStackTrace();
70+
} catch (ServletException e) {
71+
e.printStackTrace();
72+
}
73+
}
6774

6875

6976

7077
if(isImage) {
71-
html.append("<h2>You uploaded this image:</h2><img src='/upload/" + uploadedFile.getName() + "'>\n");
78+
html.append("<h2>You uploaded this image:</h2><img src='/upload/" + reportedName + "'>\n");
7279
}else{
73-
html.append("<h2>You uploaded this file:</h2><a href='/upload/" + uploadedFile.getName() + "'/>" + uploadedFile.getName() + "</a>\n");
80+
html.append("<h2>You uploaded this file:</h2><a href='/upload/" + reportedName + "'/>" + reportedName + "</a>\n");
7481
}
7582

7683
return html.toString();

java/testingapps/seleniumtestpages/src/main/java/com/seleniumsimplified/seleniumtestpages/spark/app/SeleniumTestPagesForSpark.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@ public class SeleniumTestPagesForSpark {
1111

1212
public SeleniumTestPagesForSpark(){
1313

14-
15-
1614
// create backwards compatibility with selenium page on compendiumdev.co.uk
1715
// avoid redirects
1816

1917
get("/selenium", (req, res) -> {res.redirect("/selenium.html"); return "";});
2018
get("/selenium/", (req, res) -> {res.redirect("/selenium.html"); return "";});
19+
get("/selenium/testpages/", (req, res) -> {res.redirect("/selenium.html"); return "";});
2120

2221

2322
get("/selenium/ajaxselect.php", (req, res) -> {return new PhpAjaxSelect(req,res).get();});
2423
get("/selenium/calculate.php", (req, res) -> {return new PhpCalculate(req,res).get();});
2524
post("/selenium/calculate.php", (req, res) -> {return new PhpCalculate(req,res).post();});
2625
get("/selenium/refresh.php", (req, res) -> {return new PhpRefresh(req,res).get();});
2726
post("/selenium/form_processor.php", (req, res) -> {return new PhpFormProcessor(req,res).post();});
28-
27+
get("/selenium/find_by_playground.php", (req, res) -> {return new ResourceReader().asString("/web/find_by_playground.html");});
2928

3029
// some tests check the url of the asked for page so don't redirect this one
3130
get("/selenium/basic_web_page.html", (req, res) -> {return new ResourceReader().asString("/web/basic_web_page.html");});
@@ -37,6 +36,7 @@ public SeleniumTestPagesForSpark(){
3736
post("/calculate.php", (req, res) -> {return new PhpCalculate(req,res).post();});
3837
get("/refresh.php", (req, res) -> {return new PhpRefresh(req,res).get();});
3938
post("/form_processor.php", (req, res) -> {return new PhpFormProcessor(req,res).post();});
39+
get("/find_by_playground.php", (req, res) -> {return new ResourceReader().asString("/web/find_by_playground.html");});
4040

4141
//search.php - do not use a search engine, just have a set of random urls that we put up so it looks like a search
4242
// testing, java, related
@@ -49,12 +49,21 @@ public SeleniumTestPagesForSpark(){
4949
//get("/find_by_playground.php", (req, res) -> {return new PhpFindByPlayground(req,res).get();});
5050

5151
// the upload functionality makes this insecure for external sites - do not release with this active
52-
post("/uploads/fileprocessor", (req, res) -> {return new FileUploadProcessor(req,res).post();});
52+
post("/uploads/fileprocessor", (req, res) -> {
53+
Boolean allowUploads = true; // assume working locally
54+
55+
String envVar = System.getenv("SELENIUM_TEST_PAGES_DISALLOW_UPLOAD");
56+
if(envVar != null && envVar.length()>0){
57+
allowUploads = false;
58+
}
59+
return new FileUploadProcessor(req,res, allowUploads).post();
60+
});
61+
get("/upload/NoFileUploadsAllowed.txt", (req, res) -> {return new ResourceReader().asString("/web/NoFileUploadsAllowed.txt");});
5362
get("/upload/*", (req, res) -> {return new UploadedFile(req,res).get("upload/"+req.splat()[0]);});
5463

5564
// everything else just redirect
65+
get("/selenium/testpages/*", (req, res) -> {res.redirect("/" + req.splat()[0]); return "";});
5666
get("/selenium/*", (req, res) -> {res.redirect("/" + req.splat()[0]); return "";});
5767

58-
5968
}
6069
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
File uploads are currently disabled.

0 commit comments

Comments
 (0)