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,122 @@
/*
* 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;
/**
* Representation for an HTTP Cookie.
*
* @author Rossen Stoyanchev
* @see <a href="https://tools.ietf.org/html/rfc6265">RFC 6265</a>
*/
public class HttpCookie {
private final String name;
private final String value;
private String domain;
private String path;
private long maxAge = Long.MIN_VALUE;
private boolean secure;
private boolean httpOnly;
public HttpCookie(String name, String value) {
this.name = name;
this.value = value;
}
/**
* Return the cookie name.
*/
public String getName() {
return this.name;
}
/**
* Return the cookie value.
*/
public String getValue() {
return this.value;
}
public HttpCookie setPath(String path) {
this.path = path;
return this;
}
/**
* Return the domain attribute of the cookie.
*/
public String getDomain() {
return this.domain;
}
public HttpCookie setDomain(String domain) {
this.domain = domain;
return this;
}
/**
* Return the path attribute of the cookie.
*/
public String getPath() {
return this.path;
}
public HttpCookie setMaxAge(long maxAge) {
this.maxAge = maxAge;
return this;
}
/**
* Return the maximum age attribute of the cookie in seconds or
* {@link Long#MIN_VALUE} if not set.
*/
public long getMaxAge() {
return this.maxAge;
}
public HttpCookie setSecure(boolean secure) {
this.secure = secure;
return this;
}
/**
* Return true if the "Secure" attribute of the cookie is present.
*/
public boolean isSecure() {
return this.secure;
}
public HttpCookie setHttpOnly(boolean httpOnly) {
this.httpOnly = httpOnly;
return this;
}
/**
* Return true if the "HttpOnly" attribute of the cookie is present.
* @see <a href="http://www.owasp.org/index.php/HTTPOnly">http://www.owasp.org/index.php/HTTPOnly</a>
*/
public boolean isHttpOnly() {
return this.httpOnly;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -17,8 +17,13 @@ package org.springframework.http.server.reactive;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpCookie;
/** /**
* Common base class for {@link ServerHttpRequest} implementations. * Common base class for {@link ServerHttpRequest} implementations.
@ -46,8 +51,8 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
} }
/** /**
* Initialize a URI that represents the request. * Initialize a URI that represents the request. Invoked lazily on the first
* Invoked lazily on the first call to {@link #getURI()} and then cached. * call to {@link #getURI()} and then cached.
* @throws URISyntaxException * @throws URISyntaxException
*/ */
protected abstract URI initUri() throws URISyntaxException; protected abstract URI initUri() throws URISyntaxException;
@ -55,15 +60,102 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
@Override @Override
public HttpHeaders getHeaders() { public HttpHeaders getHeaders() {
if (this.headers == null) { if (this.headers == null) {
this.headers = HttpHeaders.readOnlyHttpHeaders(initHeaders()); this.headers = new HttpHeaders(new HttpCookieInputMap());
initHeaders(this.headers);
} }
return this.headers; return this.headers;
} }
/** /**
* Initialize the headers from the underlying request. * Initialize the headers from the underlying request. Invoked lazily on the
* Invoked lazily on the first call to {@link #getHeaders()} and then cached. * first call to {@link #getHeaders()} and then cached.
* @param headers the map to add headers to
*/ */
protected abstract HttpHeaders initHeaders(); protected abstract void initHeaders(HttpHeaders headers);
/**
* Initialize the cookies from the underlying request. Invoked lazily on the
* first access to cookies via {@link #getHeaders()} and then cached.
* @param cookies the map to add cookies to
*/
protected abstract void initCookies(Map<String, Set<HttpCookie>> cookies);
/**
* Read-only map of input cookies with lazy initialization.
*/
private class HttpCookieInputMap implements Map<String, Set<HttpCookie>> {
private Map<String, Set<HttpCookie>> cookies;
private Map<String, Set<HttpCookie>> getCookies() {
if (this.cookies == null) {
this.cookies = new LinkedHashMap<>();
initCookies(this.cookies);
}
return this.cookies;
}
@Override
public int size() {
return getCookies().size();
}
@Override
public boolean isEmpty() {
return getCookies().isEmpty();
}
@Override
public boolean containsKey(Object key) {
return getCookies().containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return getCookies().containsValue(value);
}
@Override
public Set<HttpCookie> get(Object key) {
return getCookies().get(key);
}
@Override
public Set<String> keySet() {
return getCookies().keySet();
}
@Override
public Collection<Set<HttpCookie>> values() {
return getCookies().values();
}
@Override
public Set<Entry<String, Set<HttpCookie>>> entrySet() {
return getCookies().entrySet();
}
@Override
public Set<HttpCookie> put(String key, Set<HttpCookie> value) {
throw new UnsupportedOperationException("Read-only map of cookies.");
}
@Override
public Set<HttpCookie> remove(Object key) {
throw new UnsupportedOperationException("Read-only map of cookies.");
}
@Override
public void putAll(Map<? extends String, ? extends Set<HttpCookie>> m) {
throw new UnsupportedOperationException("Read-only map of cookies.");
}
@Override
public void clear() {
throw new UnsupportedOperationException("Read-only map of cookies.");
}
}
} }

View File

@ -42,7 +42,7 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
@Override @Override
public HttpHeaders getHeaders() { public HttpHeaders getHeaders() {
return (this.headersWritten ? HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers); return (this.headersWritten ? org.springframework.http.HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers);
} }
@Override @Override
@ -64,6 +64,7 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
if (!this.headersWritten) { if (!this.headersWritten) {
try { try {
writeHeadersInternal(); writeHeadersInternal();
writeCookies();
} }
finally { finally {
this.headersWritten = true; this.headersWritten = true;
@ -73,9 +74,14 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
/** /**
* Implement this method to apply header changes from {@link #getHeaders()} * Implement this method to apply header changes from {@link #getHeaders()}
* to the underlying response. This method is protected from being called * to the underlying response. This method is called once only.
* more than once.
*/ */
protected abstract void writeHeadersInternal(); protected abstract void writeHeadersInternal();
/**
* Implement this method to add cookies from {@link #getHeaders()} to the
* underlying response. This method is called once only.
*/
protected abstract void writeCookies();
} }

View File

@ -18,11 +18,14 @@ package org.springframework.http.server.reactive;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Map;
import java.util.Set;
import reactor.Flux; import reactor.Flux;
import reactor.io.buffer.Buffer; import reactor.io.buffer.Buffer;
import reactor.io.net.http.HttpChannel; import reactor.io.net.http.HttpChannel;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -58,12 +61,15 @@ public class ReactorServerHttpRequest extends AbstractServerHttpRequest {
} }
@Override @Override
protected HttpHeaders initHeaders() { protected void initHeaders(HttpHeaders headers) {
HttpHeaders headers = new HttpHeaders();
for (String name : this.channel.headers().names()) { for (String name : this.channel.headers().names()) {
headers.put(name, this.channel.headers().getAll(name)); headers.put(name, this.channel.headers().getAll(name));
} }
return headers; }
@Override
protected void initCookies(Map<String, Set<HttpCookie>> cookies) {
// https://github.com/reactor/reactor/issues/614
} }
@Override @Override

View File

@ -67,4 +67,9 @@ public class ReactorServerHttpResponse extends AbstractServerHttpResponse {
} }
} }
@Override
protected void writeCookies() {
// https://github.com/reactor/reactor/issues/614
}
} }

View File

@ -19,13 +19,18 @@ package org.springframework.http.server.reactive;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.cookie.Cookie;
import io.reactivex.netty.protocol.http.server.HttpServerRequest; import io.reactivex.netty.protocol.http.server.HttpServerRequest;
import reactor.Flux; import reactor.Flux;
import reactor.core.publisher.convert.RxJava1Converter; import reactor.core.publisher.convert.RxJava1Converter;
import rx.Observable; import rx.Observable;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -53,26 +58,43 @@ public class RxNettyServerHttpRequest extends AbstractServerHttpRequest {
@Override @Override
public HttpMethod getMethod() { public HttpMethod getMethod() {
return HttpMethod.valueOf(this.getRxNettyRequest().getHttpMethod().name()); return HttpMethod.valueOf(this.request.getHttpMethod().name());
} }
@Override @Override
protected URI initUri() throws URISyntaxException { protected URI initUri() throws URISyntaxException {
return new URI(this.getRxNettyRequest().getUri()); return new URI(this.request.getUri());
} }
@Override @Override
protected HttpHeaders initHeaders() { protected void initHeaders(HttpHeaders headers) {
HttpHeaders headers = new HttpHeaders(); for (String name : this.request.getHeaderNames()) {
for (String name : this.getRxNettyRequest().getHeaderNames()) { headers.put(name, this.request.getAllHeaderValues(name));
headers.put(name, this.getRxNettyRequest().getAllHeaderValues(name)); }
}
@Override
protected void initCookies(Map<String, Set<HttpCookie>> map) {
for (String name : this.request.getCookies().keySet()) {
Set<HttpCookie> set = map.get(name);
if (set == null) {
set = new LinkedHashSet<>();
map.put(name, set);
}
for (Cookie cookie : this.request.getCookies().get(name)) {
set.add(new HttpCookie(name, cookie.value())
.setDomain(cookie.domain())
.setPath(cookie.path())
.setMaxAge(cookie.maxAge())
.setSecure(cookie.isSecure())
.setHttpOnly(cookie.isHttpOnly()));
}
} }
return headers;
} }
@Override @Override
public Flux<ByteBuffer> getBody() { public Flux<ByteBuffer> getBody() {
Observable<ByteBuffer> content = this.getRxNettyRequest().getContent().map(ByteBuf::nioBuffer); Observable<ByteBuffer> content = this.request.getContent().map(ByteBuf::nioBuffer);
content = content.concatWith(Observable.empty()); // See GH issue #58 content = content.concatWith(Observable.empty()); // See GH issue #58
return RxJava1Converter.from(content); return RxJava1Converter.from(content);
} }

View File

@ -19,14 +19,15 @@ package org.springframework.http.server.reactive;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import io.reactivex.netty.protocol.http.server.HttpServerResponse; import io.reactivex.netty.protocol.http.server.HttpServerResponse;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.Flux;
import reactor.Mono; import reactor.Mono;
import reactor.core.publisher.convert.RxJava1Converter; import reactor.core.publisher.convert.RxJava1Converter;
import rx.Observable; import rx.Observable;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpCookie;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -53,13 +54,13 @@ public class RxNettyServerHttpResponse extends AbstractServerHttpResponse {
@Override @Override
public void setStatusCode(HttpStatus status) { public void setStatusCode(HttpStatus status) {
getRxNettyResponse().setStatus(HttpResponseStatus.valueOf(status.value())); this.response.setStatus(HttpResponseStatus.valueOf(status.value()));
} }
@Override @Override
protected Mono<Void> setBodyInternal(Publisher<ByteBuffer> publisher) { protected Mono<Void> setBodyInternal(Publisher<ByteBuffer> publisher) {
Observable<byte[]> content = RxJava1Converter.from(publisher).map(this::toBytes); Observable<byte[]> content = RxJava1Converter.from(publisher).map(this::toBytes);
Observable<Void> completion = getRxNettyResponse().writeBytes(content); Observable<Void> completion = this.response.writeBytes(content);
return RxJava1Converter.from(completion).after(); return RxJava1Converter.from(completion).after();
} }
@ -77,4 +78,19 @@ public class RxNettyServerHttpResponse extends AbstractServerHttpResponse {
} }
} }
@Override
protected void writeCookies() {
for (String name : getHeaders().getCookies().keySet()) {
for (HttpCookie httpCookie : getHeaders().getCookies().get(name)) {
Cookie cookie = new DefaultCookie(name, httpCookie.getValue());
cookie.setDomain(httpCookie.getDomain());
cookie.setPath(httpCookie.getPath());
cookie.setMaxAge(httpCookie.getMaxAge());
cookie.setSecure(httpCookie.isSecure());
cookie.setHttpOnly(httpCookie.isHttpOnly());
this.response.addCookie(cookie);
}
}
}
} }

View File

@ -21,12 +21,16 @@ import java.net.URISyntaxException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.Flux; import reactor.Flux;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -73,8 +77,7 @@ public class ServletServerHttpRequest extends AbstractServerHttpRequest {
} }
@Override @Override
protected HttpHeaders initHeaders() { protected void initHeaders(HttpHeaders headers) {
HttpHeaders headers = new HttpHeaders();
for (Enumeration<?> names = getServletRequest().getHeaderNames(); names.hasMoreElements(); ) { for (Enumeration<?> names = getServletRequest().getHeaderNames(); names.hasMoreElements(); ) {
String name = (String) names.nextElement(); String name = (String) names.nextElement();
for (Enumeration<?> values = getServletRequest().getHeaders(name); values.hasMoreElements(); ) { for (Enumeration<?> values = getServletRequest().getHeaders(name); values.hasMoreElements(); ) {
@ -105,7 +108,24 @@ public class ServletServerHttpRequest extends AbstractServerHttpRequest {
headers.setContentLength(contentLength); headers.setContentLength(contentLength);
} }
} }
return headers; }
@Override
protected void initCookies(Map<String, Set<HttpCookie>> map) {
for (Cookie cookie : this.request.getCookies()) {
String name = cookie.getName();
Set<HttpCookie> set = map.get(name);
if (set == null) {
set = new LinkedHashSet<>();
map.put(name, set);
}
set.add(new HttpCookie(name, cookie.getValue())
.setDomain(cookie.getDomain())
.setPath(cookie.getPath())
.setMaxAge(cookie.getMaxAge() == -1 ? Long.MIN_VALUE : cookie.getMaxAge())
.setHttpOnly(cookie.isHttpOnly())
.setSecure(cookie.getSecure()));
}
} }
@Override @Override

View File

@ -21,11 +21,13 @@ import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.Mono; import reactor.Mono;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -84,4 +86,23 @@ public class ServletServerHttpResponse extends AbstractServerHttpResponse {
} }
} }
@Override
protected void writeCookies() {
for (String name : getHeaders().getCookies().keySet()) {
for (HttpCookie httpCookie : getHeaders().getCookies().get(name)) {
Cookie cookie = new Cookie(name, httpCookie.getValue());
if (httpCookie.getDomain() != null) {
cookie.setDomain(httpCookie.getDomain());
}
if (httpCookie.getPath() != null) {
cookie.setPath(httpCookie.getPath());
}
cookie.setMaxAge(httpCookie.getMaxAge() == Long.MIN_VALUE ? -1 : (int) httpCookie.getMaxAge());
cookie.setSecure(httpCookie.isSecure());
cookie.setHttpOnly(httpCookie.isHttpOnly());
this.response.addCookie(cookie);
}
}
}
} }

View File

@ -19,12 +19,17 @@ package org.springframework.http.server.reactive;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import io.undertow.server.HttpServerExchange; import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.Cookie;
import io.undertow.util.HeaderValues; import io.undertow.util.HeaderValues;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.Flux; import reactor.Flux;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -67,12 +72,28 @@ public class UndertowServerHttpRequest extends AbstractServerHttpRequest {
} }
@Override @Override
protected HttpHeaders initHeaders() { protected void initHeaders(HttpHeaders headers) {
HttpHeaders headers = new HttpHeaders();
for (HeaderValues values : this.getUndertowExchange().getRequestHeaders()) { for (HeaderValues values : this.getUndertowExchange().getRequestHeaders()) {
headers.put(values.getHeaderName().toString(), values); headers.put(values.getHeaderName().toString(), values);
} }
return headers; }
@Override
protected void initCookies(Map<String, Set<HttpCookie>> map) {
for (String name : this.exchange.getRequestCookies().keySet()) {
Set<HttpCookie> set = map.get(name);
if (set == null) {
set = new LinkedHashSet<>();
map.put(name, set);
}
Cookie cookie = this.exchange.getRequestCookies().get(name);
set.add(new HttpCookie(name, cookie.getValue())
.setDomain(cookie.getDomain())
.setPath(cookie.getPath())
.setMaxAge(cookie.getMaxAge() != null ? cookie.getMaxAge() : Long.MIN_VALUE)
.setSecure(cookie.isSecure())
.setHttpOnly(cookie.isHttpOnly()));
}
} }
@Override @Override

View File

@ -22,10 +22,13 @@ import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import io.undertow.server.HttpServerExchange; import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.Cookie;
import io.undertow.server.handlers.CookieImpl;
import io.undertow.util.HttpString; import io.undertow.util.HttpString;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.Mono; import reactor.Mono;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -75,4 +78,19 @@ public class UndertowServerHttpResponse extends AbstractServerHttpResponse {
} }
} }
@Override
protected void writeCookies() {
for (String name : getHeaders().getCookies().keySet()) {
for (HttpCookie httpCookie : getHeaders().getCookies().get(name)) {
Cookie cookie = new CookieImpl(name, httpCookie.getValue());
cookie.setDomain(httpCookie.getDomain());
cookie.setPath(httpCookie.getPath());
cookie.setMaxAge(httpCookie.getMaxAge() == Long.MIN_VALUE ? null : (int) httpCookie.getMaxAge());
cookie.setSecure(httpCookie.isSecure());
cookie.setHttpOnly(httpCookie.isHttpOnly());
this.exchange.getResponseCookies().putIfAbsent(name, cookie);
}
}
}
} }

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();
}
}
}