Merge remote-tracking branch 'origin/jetty-9.4.x'
This commit is contained in:
commit
764307b9a7
|
@ -344,7 +344,7 @@ public class Response implements HttpServletResponse
|
|||
}
|
||||
|
||||
// add the set cookie
|
||||
_fields.add(HttpHeader.SET_COOKIE.toString(), buf.toString());
|
||||
_fields.add(HttpHeader.SET_COOKIE, buf.toString());
|
||||
|
||||
// Expire responses with set-cookie headers so they do not get cached.
|
||||
_fields.put(__EXPIRES_01JAN1970);
|
||||
|
|
|
@ -18,6 +18,11 @@
|
|||
|
||||
package org.eclipse.jetty.server;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
@ -42,7 +47,9 @@ import javax.servlet.ServletOutputStream;
|
|||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.eclipse.jetty.http.HttpField;
|
||||
import org.eclipse.jetty.http.HttpFields;
|
||||
import org.eclipse.jetty.http.HttpHeader;
|
||||
import org.eclipse.jetty.http.HttpURI;
|
||||
|
@ -139,7 +146,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testContentType() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
assertEquals(null, response.getContentType());
|
||||
|
||||
|
@ -244,7 +251,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testStrangeContentType() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
assertEquals(null, response.getContentType());
|
||||
|
||||
|
@ -258,7 +265,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testLocale() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
ContextHandler context = new ContextHandler();
|
||||
context.addLocaleEncoding(Locale.ENGLISH.toString(), "ISO-8859-1");
|
||||
|
@ -281,7 +288,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testContentTypeCharacterEncoding() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
response.setContentType("foo/bar");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
|
@ -309,7 +316,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testCharacterEncodingContentType() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.setContentType("foo/bar");
|
||||
assertEquals("foo/bar;charset=utf-8", response.getContentType());
|
||||
|
@ -336,7 +343,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testContentTypeWithCharacterEncoding() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
response.setCharacterEncoding("utf16");
|
||||
response.setContentType("foo/bar; charset=UTF-8");
|
||||
|
@ -372,10 +379,45 @@ public class ResponseTest
|
|||
assertEquals("foo/bar;charset=utf-8", response.getContentType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResetWithNewSession() throws Exception
|
||||
{
|
||||
Response response = getResponse();
|
||||
Request request = response.getHttpChannel().getRequest();
|
||||
|
||||
SessionHandler session_handler = new SessionHandler();
|
||||
session_handler.setServer(_server);
|
||||
session_handler.setUsingCookies(true);
|
||||
session_handler.start();
|
||||
request.setSessionHandler(session_handler);
|
||||
HttpSession session = request.getSession(true);
|
||||
|
||||
assertThat(session,not(nullValue()));
|
||||
assertTrue(session.isNew());
|
||||
|
||||
HttpField set_cookie = response.getHttpFields().getField(HttpHeader.SET_COOKIE);
|
||||
assertThat(set_cookie,not(nullValue()));
|
||||
assertThat(set_cookie.getValue(),startsWith("JSESSIONID"));
|
||||
assertThat(set_cookie.getValue(),containsString(session.getId()));
|
||||
response.setHeader("Some","Header");
|
||||
response.addCookie(new Cookie("Some","Cookie"));
|
||||
response.getOutputStream().print("X");
|
||||
assertThat(response.getHttpFields().size(),is(4));
|
||||
|
||||
response.reset();
|
||||
|
||||
set_cookie = response.getHttpFields().getField(HttpHeader.SET_COOKIE);
|
||||
assertThat(set_cookie,not(nullValue()));
|
||||
assertThat(set_cookie.getValue(),startsWith("JSESSIONID"));
|
||||
assertThat(set_cookie.getValue(),containsString(session.getId()));
|
||||
assertThat(response.getHttpFields().size(),is(2));
|
||||
response.getWriter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResetContentTypeWithoutCharacterEncoding() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.setContentType("wrong/answer");
|
||||
|
@ -390,7 +432,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testResetContentTypeWithCharacterEncoding() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
response.setContentType("wrong/answer;charset=utf-8");
|
||||
response.setContentType("foo/bar");
|
||||
|
@ -407,7 +449,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testContentTypeWithOther() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
response.setContentType("foo/bar; other=xyz");
|
||||
assertEquals("foo/bar; other=xyz", response.getContentType());
|
||||
|
@ -430,7 +472,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testContentTypeWithCharacterEncodingAndOther() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
response.setCharacterEncoding("utf16");
|
||||
response.setContentType("foo/bar; charset=utf-8 other=xyz");
|
||||
|
@ -458,26 +500,26 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testStatusCodes() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
response.sendError(404);
|
||||
assertEquals(404, response.getStatus());
|
||||
assertEquals("Not Found", response.getReason());
|
||||
|
||||
response = newResponse();
|
||||
response = getResponse();
|
||||
|
||||
response.sendError(500, "Database Error");
|
||||
assertEquals(500, response.getStatus());
|
||||
assertEquals("Database Error", response.getReason());
|
||||
assertEquals("must-revalidate,no-cache,no-store", response.getHeader(HttpHeader.CACHE_CONTROL.asString()));
|
||||
|
||||
response = newResponse();
|
||||
response = getResponse();
|
||||
|
||||
response.setStatus(200);
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals(null, response.getReason());
|
||||
|
||||
response = newResponse();
|
||||
response = getResponse();
|
||||
|
||||
response.sendError(406, "Super Nanny");
|
||||
assertEquals(406, response.getStatus());
|
||||
|
@ -489,26 +531,26 @@ public class ResponseTest
|
|||
public void testStatusCodesNoErrorHandler() throws Exception
|
||||
{
|
||||
_server.removeBean(_server.getBean(ErrorHandler.class));
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
response.sendError(404);
|
||||
assertEquals(404, response.getStatus());
|
||||
assertEquals("Not Found", response.getReason());
|
||||
|
||||
response = newResponse();
|
||||
response = getResponse();
|
||||
|
||||
response.sendError(500, "Database Error");
|
||||
assertEquals(500, response.getStatus());
|
||||
assertEquals("Database Error", response.getReason());
|
||||
assertThat(response.getHeader(HttpHeader.CACHE_CONTROL.asString()),Matchers.nullValue());
|
||||
|
||||
response = newResponse();
|
||||
response = getResponse();
|
||||
|
||||
response.setStatus(200);
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals(null, response.getReason());
|
||||
|
||||
response = newResponse();
|
||||
response = getResponse();
|
||||
|
||||
response.sendError(406, "Super Nanny");
|
||||
assertEquals(406, response.getStatus());
|
||||
|
@ -519,7 +561,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testWriteRuntimeIOException() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.println("test");
|
||||
|
@ -546,7 +588,7 @@ public class ResponseTest
|
|||
public void testEncodeRedirect()
|
||||
throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
Request request = response.getHttpChannel().getRequest();
|
||||
request.setAuthority("myhost",8888);
|
||||
request.setContextPath("/path");
|
||||
|
@ -626,7 +668,7 @@ public class ResponseTest
|
|||
{
|
||||
for (int i=0;i<tests.length;i++)
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
Request request = response.getHttpChannel().getRequest();
|
||||
|
||||
request.setScheme("http");
|
||||
|
@ -660,7 +702,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testSetBufferSizeAfterHavingWrittenContent() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
response.setBufferSize(20 * 1024);
|
||||
response.getWriter().print("hello");
|
||||
try
|
||||
|
@ -677,7 +719,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testZeroContent() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
PrintWriter writer = response.getWriter();
|
||||
response.setContentLength(0);
|
||||
assertTrue(!response.isCommitted());
|
||||
|
@ -746,7 +788,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testAddCookie() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
Cookie cookie = new Cookie("name", "value");
|
||||
cookie.setDomain("domain");
|
||||
|
@ -765,7 +807,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testCookiesWithReset() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
|
||||
Cookie cookie=new Cookie("name","value");
|
||||
cookie.setDomain("domain");
|
||||
|
@ -800,7 +842,7 @@ public class ResponseTest
|
|||
@Test
|
||||
public void testFlushAfterFullContent() throws Exception
|
||||
{
|
||||
Response response = newResponse();
|
||||
Response response = getResponse();
|
||||
byte[] data = new byte[]{(byte)0xCA, (byte)0xFE};
|
||||
ServletOutputStream output = response.getOutputStream();
|
||||
response.setContentLength(data.length);
|
||||
|
@ -810,7 +852,6 @@ public class ResponseTest
|
|||
output.flush();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSetCookie() throws Exception
|
||||
{
|
||||
|
@ -927,14 +968,13 @@ public class ResponseTest
|
|||
response.addSetCookie("name","value%=",null,null,-1,null,false,false,0);
|
||||
setCookie=fields.get("Set-Cookie");
|
||||
assertEquals("name=value%=",setCookie);
|
||||
|
||||
}
|
||||
|
||||
private Response newResponse()
|
||||
private Response getResponse()
|
||||
{
|
||||
_channel.recycle();
|
||||
_channel.getRequest().setMetaData(new MetaData.Request("GET",new HttpURI("/path/info"),HttpVersion.HTTP_1_0,new HttpFields()));
|
||||
return new Response(_channel, _channel.getResponse().getHttpOutput());
|
||||
return _channel.getResponse();
|
||||
}
|
||||
|
||||
private static class TestSession extends Session
|
||||
|
|
|
@ -81,8 +81,9 @@ public class ServletUpgradeResponse extends UpgradeResponse
|
|||
public void sendError(int statusCode, String message) throws IOException
|
||||
{
|
||||
setSuccess(false);
|
||||
commitHeaders();
|
||||
applyHeaders();
|
||||
response.sendError(statusCode, message);
|
||||
response.flushBuffer(); // commit response
|
||||
response = null;
|
||||
}
|
||||
|
||||
|
@ -90,8 +91,9 @@ public class ServletUpgradeResponse extends UpgradeResponse
|
|||
public void sendForbidden(String message) throws IOException
|
||||
{
|
||||
setSuccess(false);
|
||||
commitHeaders();
|
||||
applyHeaders();
|
||||
response.sendError(HttpServletResponse.SC_FORBIDDEN, message);
|
||||
response.flushBuffer(); // commit response
|
||||
response = null;
|
||||
}
|
||||
|
||||
|
@ -111,11 +113,11 @@ public class ServletUpgradeResponse extends UpgradeResponse
|
|||
|
||||
public void complete()
|
||||
{
|
||||
commitHeaders();
|
||||
applyHeaders();
|
||||
response = null;
|
||||
}
|
||||
|
||||
private void commitHeaders()
|
||||
private void applyHeaders()
|
||||
{
|
||||
// Transfer all headers to the real HTTP response
|
||||
for (Map.Entry<String, List<String>> entry : getHeaders().entrySet())
|
||||
|
|
Loading…
Reference in New Issue