|
| 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 | +} |
0 commit comments