Skip to content

Commit 1e8d95f

Browse files
committed
Add a test for the ApacheHttpClient
Turns out it wasn't capturing all the headers correctly.
1 parent f96cd95 commit 1e8d95f

File tree

4 files changed

+126
-3
lines changed

4 files changed

+126
-3
lines changed

java/client/src/org/openqa/selenium/remote/internal/ApacheHttpClient.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
import java.io.IOException;
4545
import java.net.BindException;
46+
import java.net.MalformedURLException;
4647
import java.net.URI;
4748
import java.net.URISyntaxException;
4849
import java.net.URL;
@@ -98,9 +99,7 @@ private HttpResponse createResponse(
9899

99100
internalResponse.setStatus(response.getStatusLine().getStatusCode());
100101
for (Header header : response.getAllHeaders()) {
101-
for (HeaderElement headerElement : header.getElements()) {
102-
internalResponse.addHeader(header.getName(), headerElement.getValue());
103-
}
102+
internalResponse.addHeader(header.getName(), header.getValue());
104103
}
105104

106105
HttpEntity entity = response.getEntity();
@@ -257,5 +256,19 @@ private static synchronized HttpClientFactory getDefaultHttpClientFactory() {
257256
public void close() throws IOException {
258257
client.getConnectionManager().closeIdleConnections(0, TimeUnit.SECONDS);
259258
}
259+
260+
public static void main(String[] args) throws IOException {
261+
org.openqa.selenium.remote.http.HttpClient client = new Factory().createClient(new URL("http://www.google.com/"));
262+
263+
HttpRequest request = new HttpRequest(HttpMethod.GET, "/");
264+
HttpResponse response = client.execute(request, true);
265+
266+
for (String names : response.getHeaderNames()) {
267+
Iterable<String> header = response.getHeaders(names);
268+
for (String h : header) {
269+
System.out.println(names + ": " + h);
270+
}
271+
}
272+
}
260273

261274
}

java/client/test/org/openqa/selenium/remote/BUCK

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ java_test(name = 'client-tests',
2929
'RemoteClientTests.java',
3030
'RemoteLogsTest.java',
3131
'RemoteWebDriverInitializationTest.java',
32+
'internal/ApacheHttpClientTest.java',
3233
'internal/CircularOutputStreamTest.java',
3334
'internal/WebElementToJsonConverterTest.java',
3435
],
@@ -38,6 +39,7 @@ java_test(name = 'client-tests',
3839
'//third_party/java/gson:gson',
3940
'//third_party/java/guava:guava',
4041
'//third_party/java/hamcrest:hamcrest-library',
42+
'//third_party/java/jetty:jetty',
4143
'//third_party/java/junit:junit',
4244
'//third_party/java/mockito:mockito',
4345
])
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.remote.internal;
19+
20+
import static org.junit.Assert.*;
21+
22+
import com.google.common.collect.HashMultimap;
23+
import com.google.common.collect.ImmutableList;
24+
import com.google.common.collect.Multimap;
25+
import com.google.common.collect.MultimapBuilder;
26+
27+
import org.junit.Test;
28+
import org.openqa.selenium.net.PortProber;
29+
import org.openqa.selenium.remote.http.HttpClient;
30+
import org.openqa.selenium.remote.http.HttpMethod;
31+
import org.openqa.selenium.remote.http.HttpRequest;
32+
import org.openqa.selenium.remote.http.HttpResponse;
33+
import org.seleniumhq.jetty9.server.HttpConnectionFactory;
34+
import org.seleniumhq.jetty9.server.Server;
35+
import org.seleniumhq.jetty9.server.ServerConnector;
36+
import org.seleniumhq.jetty9.servlet.ServletContextHandler;
37+
import org.seleniumhq.jetty9.servlet.ServletHolder;
38+
39+
import java.io.IOException;
40+
41+
import javax.servlet.ServletException;
42+
import javax.servlet.http.HttpServlet;
43+
import javax.servlet.http.HttpServletRequest;
44+
import javax.servlet.http.HttpServletResponse;
45+
46+
public class ApacheHttpClientTest {
47+
48+
@Test
49+
public void responseShouldCaptureASingleHeader() throws Exception {
50+
HashMultimap<String, String> headers = HashMultimap.create();
51+
headers.put("Cake", "Delicious");
52+
53+
HttpResponse response = getResponseWithHeaders(headers);
54+
55+
String value = response.getHeader("Cake");
56+
assertEquals("Delicious", value);
57+
}
58+
59+
/**
60+
* The HTTP Spec that it should be
61+
* <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2">safe to combine them
62+
* </a>, but things like the <a href="https://www.ietf.org/rfc/rfc2109.txt">cookie spec</a> make
63+
* this hard (notably when a legal value may contain a comma).
64+
*/
65+
@Test
66+
public void responseShouldKeepMultipleHeadersSeparate() throws Exception {
67+
HashMultimap<String, String> headers = HashMultimap.create();
68+
headers.put("Cheese", "Cheddar");
69+
headers.put("Cheese", "Brie, Gouda");
70+
71+
HttpResponse response = getResponseWithHeaders(headers);
72+
73+
ImmutableList<String> values = ImmutableList.copyOf(response.getHeaders("Cheese"));
74+
75+
assertEquals("Cheddar", values.get(0));
76+
assertEquals("Brie, Gouda", values.get(1));
77+
}
78+
79+
private HttpResponse getResponseWithHeaders(final Multimap<String, String> headers)
80+
throws Exception {
81+
Server server = new Server(PortProber.findFreePort());
82+
ServletContextHandler handler = new ServletContextHandler();
83+
handler.setContextPath("");
84+
85+
class Headers extends HttpServlet {
86+
@Override
87+
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
88+
throws ServletException, IOException {
89+
headers.forEach(resp::addHeader);
90+
resp.setContentLengthLong(0);
91+
}
92+
}
93+
ServletHolder holder = new ServletHolder(new Headers());
94+
handler.addServlet(holder, "/*");
95+
96+
server.setHandler(handler);
97+
98+
server.start();
99+
try {
100+
HttpClient client = new ApacheHttpClient.Factory().createClient(server.getURI().toURL());
101+
HttpRequest request = new HttpRequest(HttpMethod.GET, "/foo");
102+
return client.execute(request, true);
103+
} finally {
104+
server.stop();
105+
}
106+
}
107+
}

third_party/java/jetty/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ java_library(
1414
'//java/client/test/com/thoughtworks/selenium:tests',
1515
'//java/client/test/org/openqa/selenium:tests',
1616
'//java/client/test/org/openqa/selenium/environment:environment',
17+
'//java/client/test/org/openqa/selenium/remote:client-tests',
1718
'//java/server/src/org/openqa/grid:grid',
1819
'//java/server/test/org/openqa/grid:test',
1920
'//java/server/src/org/openqa/selenium/remote/server:standalone-server-lib',

0 commit comments

Comments
 (0)