Support response cookies in HtmlUnit integration

Issue: SPR-14051
This commit is contained in:
Rossen Stoyanchev 2016-03-16 15:26:06 -04:00
parent 92dd4eb3ef
commit f5a5a81e95
2 changed files with 35 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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
@ -19,8 +19,11 @@ package org.springframework.test.web.servlet.htmlunit;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.servlet.http.Cookie;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.WebResponseData;
@ -34,6 +37,7 @@ import org.springframework.util.StringUtils;
/**
* @author Rob Winch
* @author Sam Brannen
* @author Rossen Stoyanchev
* @since 4.2
*/
final class MockWebResponseBuilder {
@ -98,7 +102,20 @@ final class MockWebResponseBuilder {
if (location != null) {
responseHeaders.add(new NameValuePair("Location", location));
}
for (Cookie cookie : this.response.getCookies()) {
responseHeaders.add(new NameValuePair("Set-Cookie", valueOfCookie(cookie)));
}
return responseHeaders;
}
private String valueOfCookie(Cookie cookie) {
Date expires = null;
if (cookie.getMaxAge() > -1) {
expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000);
}
return new com.gargoylesoftware.htmlunit.util.Cookie(
cookie.getDomain(), cookie.getName(), cookie.getValue(),
cookie.getPath(), expires, cookie.getSecure(), cookie.isHttpOnly()).toString();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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
@ -19,6 +19,8 @@ package org.springframework.test.web.servlet.htmlunit;
import java.net.URL;
import java.util.List;
import javax.servlet.http.Cookie;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletResponse;
@ -27,7 +29,9 @@ import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;
/**
@ -94,16 +98,27 @@ public class MockWebResponseBuilderTests {
public void buildResponseHeaders() throws Exception {
this.response.addHeader("Content-Type", "text/html");
this.response.addHeader("X-Test", "value");
Cookie cookie = new Cookie("cookieA", "valueA");
cookie.setDomain("domain");
cookie.setPath("/path");
cookie.setMaxAge(1800);
cookie.setSecure(true);
cookie.setHttpOnly(true);
this.response.addCookie(cookie);
WebResponse webResponse = this.responseBuilder.build();
List<NameValuePair> responseHeaders = webResponse.getResponseHeaders();
assertThat(responseHeaders.size(), equalTo(2));
assertThat(responseHeaders.size(), equalTo(3));
NameValuePair header = responseHeaders.get(0);
assertThat(header.getName(), equalTo("Content-Type"));
assertThat(header.getValue(), equalTo("text/html"));
header = responseHeaders.get(1);
assertThat(header.getName(), equalTo("X-Test"));
assertThat(header.getValue(), equalTo("value"));
header = responseHeaders.get(2);
assertThat(header.getName(), equalTo("Set-Cookie"));
assertThat(header.getValue(), startsWith("cookieA=valueA;domain=domain;path=/path;expires="));
assertThat(header.getValue(), endsWith(";secure;httpOnly"));
}
@Test