Add HttpCookie + server support through HttpHeaders

This commit is contained in:
Rossen Stoyanchev
2016-01-10 22:36:45 -05:00
parent f8ef2e0220
commit 1faeb0ec87
13 changed files with 1698 additions and 30 deletions

View File

@@ -0,0 +1,160 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.server.reactive;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import reactor.Mono;
import org.springframework.http.HttpCookie;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.boot.HttpServer;
import org.springframework.http.server.reactive.boot.JettyHttpServer;
import org.springframework.http.server.reactive.boot.RxNettyHttpServer;
import org.springframework.http.server.reactive.boot.TomcatHttpServer;
import org.springframework.http.server.reactive.boot.UndertowHttpServer;
import org.springframework.util.SocketUtils;
import org.springframework.web.client.RestTemplate;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
/**
* Temporarily does not extend AbstractHttpHandlerIntegrationTests in order to
* exclude Reactor Net due to https://github.com/reactor/reactor/issues/614.
*
* @author Rossen Stoyanchev
*/
@RunWith(Parameterized.class)
public class CookieIntegrationTests {
protected int port;
@Parameterized.Parameter(0)
public HttpServer server;
private CookieHandler cookieHandler;
@Parameterized.Parameters(name = "server [{0}]")
public static Object[][] arguments() {
return new Object[][] {
{new JettyHttpServer()},
{new RxNettyHttpServer()},
// {new ReactorHttpServer()},
{new TomcatHttpServer()},
{new UndertowHttpServer()}
};
}
@Before
public void setup() throws Exception {
this.port = SocketUtils.findAvailableTcpPort();
this.server.setPort(this.port);
this.server.setHandler(createHttpHandler());
this.server.afterPropertiesSet();
this.server.start();
}
protected HttpHandler createHttpHandler() {
this.cookieHandler = new CookieHandler();
return this.cookieHandler;
}
@After
public void tearDown() throws Exception {
this.server.stop();
}
@SuppressWarnings("unchecked")
@Test
public void basicTest() throws Exception {
URI url = new URI("http://localhost:" + port);
String header = "SID=31d4d96e407aad42; lang=en-US";
ResponseEntity<Void> response = new RestTemplate().exchange(
RequestEntity.get(url).header("Cookie", header).build(), Void.class);
Map<String, Set<HttpCookie>> requestCookies = this.cookieHandler.requestCookies;
assertEquals(2, requestCookies.size());
Set<HttpCookie> set = requestCookies.get("SID");
assertEquals(1, set.size());
assertEquals("31d4d96e407aad42", set.iterator().next().getValue());
set = requestCookies.get("lang");
assertEquals(1, set.size());
assertEquals("en-US", set.iterator().next().getValue());
List<String> headerValues = response.getHeaders().get("Set-Cookie");
assertEquals(2, headerValues.size());
List<String> parts = splitCookieHeader(headerValues.get(0));
assertThat(parts, containsInAnyOrder(equalTo("SID=31d4d96e407aad42"),
equalToIgnoringCase("Path=/"), equalToIgnoringCase("Secure"),
equalToIgnoringCase("HttpOnly")));
parts = splitCookieHeader(headerValues.get(1));
assertThat(parts, containsInAnyOrder(equalTo("lang=en-US"),
equalToIgnoringCase("Path=/"), equalToIgnoringCase("Domain=example.com")));
}
// No client side HttpCookie support yet
private List<String> splitCookieHeader(String value) {
List<String> list = new ArrayList<>();
for (String s : value.split(";")){
list.add(s.trim());
}
return list;
}
private class CookieHandler implements HttpHandler {
private Map<String, Set<HttpCookie>> requestCookies;
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
this.requestCookies = request.getHeaders().getCookies();
this.requestCookies.size(); // Cause lazy loading
response.getHeaders().addCookie(new HttpCookie("SID", "31d4d96e407aad42")
.setPath("/").setHttpOnly(true).setSecure(true));
response.getHeaders().addCookie(new HttpCookie("lang", "en-US")
.setDomain("example.com").setPath("/"));
response.writeHeaders();
return Mono.empty();
}
}
}