Skip to content

Commit

Permalink
[java] Stop using apache http client in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Sep 26, 2018
1 parent f671fd2 commit 657e5ae
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 77 deletions.
4 changes: 2 additions & 2 deletions java/client/client.iml
Expand Up @@ -26,7 +26,7 @@
<orderEntry type="library" exported="" name="guava" level="project" />
<orderEntry type="library" exported="" name="gson" level="project" />
<orderEntry type="library" name="hamcrest" level="project" />
<orderEntry type="library" name="htmlunit" level="project" />
<orderEntry type="library" scope="TEST" name="htmlunit" level="project" />
<orderEntry type="library" name="junit" level="project" />
<orderEntry type="library" name="netty" level="project" />
<orderEntry type="library" name="servlet-api" level="project" />
Expand All @@ -37,7 +37,7 @@
<orderEntry type="library" scope="TEST" name="slf4j" level="project" />
<orderEntry type="library" name="mockito-core" level="project" />
<orderEntry type="library" name="objenesis" level="project" />
<orderEntry type="library" name="commons-httpclient" level="project" />
<orderEntry type="library" scope="TEST" name="commons-httpclient" level="project" />
<orderEntry type="library" scope="TEST" name="websocket" level="project" />
<orderEntry type="library" scope="TEST" name="github" level="project" />
<orderEntry type="library" name="javaparser" level="project" />
Expand Down
21 changes: 13 additions & 8 deletions java/client/test/org/openqa/selenium/environment/BUCK
Expand Up @@ -8,7 +8,6 @@ java_library(name = 'environment',
'//java/client/src/org/openqa/selenium/remote:remote',
'//java/client/test/org/openqa/selenium/testing:helpers',
'//third_party/java/guava:guava',
'//third_party/java/httpcomponents:httpclient',
'//third_party/java/jetty:jetty',
],
visibility = [
Expand All @@ -27,9 +26,20 @@ java_binary(name = 'webserver',
]
)

java_test(name = "webserver-test",
java_library(name = "webserver-test-base",
srcs = [
"webserver/AppServerTestBase.java",
],
deps = [
":environment",
"//java/client/src/org/openqa/selenium/remote:remote",
"//third_party/java/junit:junit",
'//java/client/test/org/openqa/selenium/testing/drivers:drivers',
],
)

java_test(name = "webserver-test",
srcs = [
"webserver/JettyAppServerTest.java",
"webserver/JreAppServerTest.java",
],
Expand All @@ -38,12 +48,7 @@ java_test(name = "webserver-test",
],
deps = [
":environment",
"//java/client/src/org/openqa/selenium/chrome:chrome",
"//java/client/src/org/openqa/selenium/remote:remote",
"//third_party/java/guava:guava",
"//third_party/java/httpcomponents:httpclient",
"//third_party/java/junit:junit",
'//java/client/test/org/openqa/selenium/testing/drivers:drivers',
":webserver-test-base",
],
)

Expand Up @@ -20,29 +20,26 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.common.io.Files;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.testing.drivers.WebDriverBuilder;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.stream.StreamSupport;

@RunWith(JUnit4.class)
public abstract class AppServerTestBase {
private static final String APPCACHE_MIME_TYPE = "text/cache-manifest";
private AppServer server;
Expand Down Expand Up @@ -118,12 +115,12 @@ public void dealsWithUtf16() {
}

@Test
public void manifestHasCorrectMimeType() {
public void manifestHasCorrectMimeType() throws IOException {
assertUrlHasContentType(server.whereIs("html5/test.appcache"), APPCACHE_MIME_TYPE);
}

@Test
public void manifestHasCorrectMimeTypeUnderJavascript() {
public void manifestHasCorrectMimeTypeUnderJavascript() throws IOException {
String appcacheUrl =
server.whereIs("/javascript/atoms/test/html5/testdata/with_fallback.appcache");
assertUrlHasContentType(appcacheUrl, APPCACHE_MIME_TYPE);
Expand All @@ -134,7 +131,7 @@ public void uploadsFile() throws Throwable {
String FILE_CONTENTS = "Uploaded file";
File testFile = File.createTempFile("webdriver", "tmp");
testFile.deleteOnExit();
Files.asCharSink(testFile, StandardCharsets.UTF_8).write(FILE_CONTENTS);
Files.write(testFile.toPath(), FILE_CONTENTS.getBytes());

driver.get(server.whereIs("upload.html"));
driver.findElement(By.id("upload")).sendKeys(testFile.getAbsolutePath());
Expand All @@ -148,21 +145,12 @@ public void uploadsFile() throws Throwable {
assertEquals(FILE_CONTENTS, body.getText());
}

private void assertUrlHasContentType(String url, String appcacheMimeType) {
HttpClient httpclient = HttpClientBuilder.create().build();
HttpGet httpget = new HttpGet(url);
HttpResponse response;

try {
response = httpclient.execute(httpget);
} catch (RuntimeException e) {
throw e;
} catch (Throwable t) {
throw new RuntimeException(t);
}

Header[] contentTypeHeaders = response.getHeaders("Content-Type");
assertEquals(1, contentTypeHeaders.length);
assertTrue(contentTypeHeaders[0].getValue().contains(appcacheMimeType));
private void assertUrlHasContentType(String url, String appcacheMimeType) throws IOException {
HttpClient.Factory factory = HttpClient.Factory.createDefault();
HttpClient client = factory.createClient(new URL(url));
HttpResponse response = client.execute(new HttpRequest(HttpMethod.GET, url));

assertTrue(StreamSupport.stream(response.getHeaders("Content-Type").spliterator(), false)
.anyMatch(header -> header.contains(appcacheMimeType)));
}
}
1 change: 0 additions & 1 deletion java/client/test/org/openqa/selenium/firefox/BUCK
Expand Up @@ -46,7 +46,6 @@ java_library(name = 'tests',
'//java/client/test/org/openqa/selenium/testing/drivers:drivers',
'//third_party/java/guava:guava',
'//third_party/java/assertj:assertj',
'//third_party/java/httpcomponents:httpclient',
'//third_party/java/junit:junit',
'//third_party/java/mockito:mockito-core',
],
Expand Down
1 change: 0 additions & 1 deletion java/client/test/org/openqa/selenium/remote/BUCK
Expand Up @@ -14,7 +14,6 @@ java_test(name = 'common-tests',
'//java/client/src/org/openqa/selenium/remote:remote',
'//third_party/java/guava:guava',
'//third_party/java/assertj:assertj',
'//third_party/java/httpcomponents:httpclient',
'//third_party/java/junit:junit',
])

Expand Down
2 changes: 0 additions & 2 deletions java/server/.classpath
Expand Up @@ -6,8 +6,6 @@
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/client"/>
<classpathentry kind="lib" path="/third-party/java/gson/gson-2.8.4.jar"/>
<classpathentry kind="lib" path="/third-party/java/servlet/javax.servlet-api-3.1.0.jar" sourcepath="/third-party/java/servlet/javax.servlet-api-3.1.0-sources.jar"/>
<classpathentry kind="lib" path="/third-party/java/httpcomponents/httpclient-4.5.5.jar" sourcepath="/third-party/java/httpcomponents/httpclient-4.5.5-sources.jar"/>
<classpathentry kind="lib" path="/third-party/java/httpcomponents/httpcore-4.4.9.jar" sourcepath="/third-party/java/httpcomponents/httpcore-4.4.9-sources.jar"/>
<classpathentry kind="lib" path="/third-party/java/junit/junit-4.12.jar" sourcepath="/third-party/java/junit/junit-4.12-sources.jar"/>
<classpathentry kind="lib" path="/third-party/java/commons-logging/commons-logging-1.2.jar"/>
<classpathentry kind="lib" path="/third-party/java/jcip/jcip-annotations-1.0.jar" sourcepath="/third-party/java/jcip/jcip-annotations-1.0-sources.jar"/>
Expand Down
1 change: 0 additions & 1 deletion java/server/server.iml
Expand Up @@ -22,7 +22,6 @@
<orderEntry type="library" name="snakeyaml" level="project" />
<orderEntry type="library" name="mockito-core" level="project" />
<orderEntry type="library" name="objenesis" level="project" />
<orderEntry type="library" name="commons-httpclient" level="project" />
<orderEntry type="library" scope="TEST" name="assertj" level="project" />
<orderEntry type="library" name="auto-service" level="project" />
</component>
Expand Down
1 change: 0 additions & 1 deletion java/server/test/org/openqa/grid/BUCK
Expand Up @@ -37,7 +37,6 @@ java_library(name = 'test',
'//third_party/java/beust:jcommander',
'//third_party/java/gson:gson',
'//third_party/java/guava:guava',
'//third_party/java/httpcomponents:httpclient',
'//third_party/java/jetty:jetty',
'//third_party/java/junit:junit',
'//third_party/java/mockito:mockito-core',
Expand Down
1 change: 0 additions & 1 deletion java/server/test/org/openqa/grid/e2e/BUCK
Expand Up @@ -22,7 +22,6 @@ java_library(name = 'tests',
'//java/server/src/org/openqa/selenium/remote/server:standalone-server-lib',
'//third_party/java/beust:jcommander',
'//third_party/java/guava:guava',
'//third_party/java/httpcomponents:httpclient',
'//third_party/java/junit:junit',
'//third_party/java/assertj:assertj',
'//third_party/java/mockito:mockito-core',
Expand Down
1 change: 0 additions & 1 deletion java/server/test/org/openqa/selenium/remote/server/BUCK
Expand Up @@ -37,7 +37,6 @@ java_library(name = 'tests',
'//third_party/java/gson:gson',
'//third_party/java/guava:guava',
'//third_party/java/assertj:assertj',
'//third_party/java/httpcomponents:httpclient',
'//third_party/java/jetty:jetty',
'//third_party/java/junit:junit',
'//third_party/java/mockito:mockito-core',
Expand Down
Expand Up @@ -17,22 +17,13 @@

package org.openqa.selenium.remote.server;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertTrue;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.testing.Driver.CHROME;
import static org.openqa.selenium.testing.Driver.HTMLUNIT;
import static org.openqa.selenium.testing.Driver.IE;
import static org.openqa.selenium.testing.Driver.SAFARI;

import com.google.common.io.CharStreams;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -43,14 +34,17 @@
import org.openqa.selenium.logging.SessionLogs;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.drivers.Browser;
import org.openqa.selenium.testing.drivers.OutOfProcessSeleniumServer;
import org.openqa.selenium.testing.drivers.WebDriverBuilder;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -106,18 +100,11 @@ public void sessionLogsShouldContainAllAvailableLogTypes() throws Exception {
}

private static Map<String, Object> getValueForPostRequest(URL serverUrl) throws Exception {
String postRequest = serverUrl + "/logs";
HttpClient client = HttpClientBuilder.create().build();
HttpPost postCmd = new HttpPost(postRequest);
HttpResponse response = client.execute(postCmd);
HttpEntity entity = response.getEntity();
try (InputStreamReader reader = new InputStreamReader(entity.getContent(), UTF_8)) {
String str = CharStreams.toString(reader);
Map<String, Object> map = new Json().toType(str, MAP_TYPE);
//noinspection unchecked
return (Map<String, Object>) map.get("value");
} finally {
EntityUtils.consume(entity);
}
String url = serverUrl + "/logs";
HttpClient.Factory factory = HttpClient.Factory.createDefault();
HttpClient client = factory.createClient(new URL(url));
HttpResponse response = client.execute(new HttpRequest(HttpMethod.POST, url));
Map<String, Object> map = new Json().toType(response.getContentString(), MAP_TYPE);
return (Map<String, Object>) map.get("value");
}
}
2 changes: 0 additions & 2 deletions java/server/test/org/openqa/selenium/remote/server/auth/BUCK
Expand Up @@ -12,8 +12,6 @@ java_library(
'//java/client/src/org/openqa/selenium:selenium',
'//java/server/src/org/openqa/selenium/remote/server:server',
'//java/server/src/org/openqa/selenium/remote/server:webdriver-servlet',
'//third_party/java/guava:guava',
'//third_party/java/httpcomponents:httpclient',
'//third_party/java/jetty:jetty',
'//third_party/java/servlet:javax.servlet-api',
],
Expand Down
4 changes: 0 additions & 4 deletions third_party/java/httpcomponents/BUCK
Expand Up @@ -8,10 +8,6 @@ prebuilt_jar(
'//third_party/java/commons-codec:commons-codec',
'//third_party/java/commons-logging:commons-logging'
],
visibility = [
'//java/client/test/...',
'//java/server/test/...',
],
)

prebuilt_jar(
Expand Down

0 comments on commit 657e5ae

Please sign in to comment.