Updating gzip tests to include 1 other flavor

This commit is contained in:
Joakim Erdfelt 2014-11-07 11:13:37 -07:00
parent 80ff2da394
commit e4cc9ea5de
3 changed files with 95 additions and 9 deletions

View File

@ -30,6 +30,7 @@ import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlets.gzip.AsyncScheduledWrite;
import org.eclipse.jetty.servlets.gzip.AsyncTimeoutDispatchBasedWrite;
import org.eclipse.jetty.servlets.gzip.AsyncTimeoutWrite;
import org.eclipse.jetty.servlets.gzip.GzipTester;
import org.eclipse.jetty.servlets.gzip.TestServletBufferTypeLengthWrite;
@ -44,7 +45,6 @@ import org.eclipse.jetty.toolchain.test.TestTracker;
import org.eclipse.jetty.toolchain.test.TestingDir;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -81,6 +81,7 @@ public class GzipFilterContentLengthTest
return Arrays.asList(new Object[][]
{
{ AsyncGzipFilter.class, AsyncTimeoutWrite.class, GzipFilter.GZIP },
{ AsyncGzipFilter.class, AsyncTimeoutDispatchBasedWrite.class, GzipFilter.GZIP },
{ AsyncGzipFilter.class, AsyncScheduledWrite.class, GzipFilter.GZIP },
{ AsyncGzipFilter.class, TestServletLengthStreamTypeWrite.class, GzipFilter.GZIP },
{ AsyncGzipFilter.class, TestServletLengthTypeStreamWrite.class, GzipFilter.GZIP },
@ -91,8 +92,6 @@ public class GzipFilterContentLengthTest
{ AsyncGzipFilter.class, TestServletTypeStreamLengthWrite.class, GzipFilter.GZIP },
{ AsyncGzipFilter.class, TestServletBufferTypeLengthWrite.class, GzipFilter.GZIP },
//{ GzipFilter.class, AsyncTimeoutWrite.class, GzipFilter.GZIP },
//{ GzipFilter.class, AsyncScheduledWrite.class, GzipFilter.GZIP },
{ GzipFilter.class, TestServletLengthStreamTypeWrite.class, GzipFilter.GZIP },
{ GzipFilter.class, TestServletLengthTypeStreamWrite.class, GzipFilter.GZIP },
{ GzipFilter.class, TestServletStreamLengthTypeWrite.class, GzipFilter.GZIP },
@ -101,8 +100,6 @@ public class GzipFilterContentLengthTest
{ GzipFilter.class, TestServletTypeLengthStreamWrite.class, GzipFilter.GZIP },
{ GzipFilter.class, TestServletTypeStreamLengthWrite.class, GzipFilter.GZIP },
//{ GzipFilter.class, AsyncTimeoutWrite.class, GzipFilter.DEFLATE },
//{ GzipFilter.class, AsyncScheduledWrite.class, GzipFilter.DEFLATE },
{ GzipFilter.class, TestServletLengthStreamTypeWrite.class, GzipFilter.DEFLATE },
{ GzipFilter.class, TestServletLengthTypeStreamWrite.class, GzipFilter.DEFLATE },
{ GzipFilter.class, TestServletStreamLengthTypeWrite.class, GzipFilter.DEFLATE },

View File

@ -0,0 +1,89 @@
//
// ========================================================================
// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.servlets.gzip;
import java.io.IOException;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class AsyncTimeoutDispatchBasedWrite extends TestDirContentServlet implements AsyncListener
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
AsyncContext ctx = (AsyncContext)request.getAttribute(AsyncContext.class.getName());
if (ctx == null)
{
// First pass through
ctx = request.startAsync();
ctx.addListener(this);
ctx.setTimeout(200);
request.setAttribute(AsyncContext.class.getName(),ctx);
}
else
{
// second pass through, as result of timeout -> dispatch
String fileName = request.getServletPath();
byte[] dataBytes = loadContentFileBytes(fileName);
response.setContentLength(dataBytes.length);
ServletOutputStream out = response.getOutputStream();
if (fileName.endsWith("txt"))
response.setContentType("text/plain");
else if (fileName.endsWith("mp3"))
response.setContentType("audio/mpeg");
response.setHeader("ETag","W/etag-" + fileName);
out.write(dataBytes);
// no need to call AsyncContext.complete() from here
// in fact, it will cause an IllegalStateException if we do
// ctx.complete();
}
}
@Override
public void onComplete(AsyncEvent event) throws IOException
{
}
@Override
public void onTimeout(AsyncEvent event) throws IOException
{
event.getAsyncContext().dispatch();
}
@Override
public void onError(AsyncEvent event) throws IOException
{
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException
{
}
}

View File

@ -18,6 +18,9 @@
package org.eclipse.jetty.servlets.gzip;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.IOException;
import javax.servlet.AsyncContext;
@ -34,11 +37,8 @@ public class AsyncTimeoutWrite extends TestDirContentServlet implements AsyncLis
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
if (request.getAttribute("filename")!=null)
throw new IllegalStateException();
assertThat("'filename' request attribute shouldn't be declared", request.getAttribute("filename"), nullValue());
ServletOutputStream out = response.getOutputStream();
AsyncContext ctx = (AsyncContext)request.getAttribute(AsyncContext.class.getName());
ctx = request.startAsync();
String fileName = request.getServletPath();