use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class EncodingSelectionTestCase method testEncodingSelectionWithQValueAndPredicate.
@Test
public void testEncodingSelectionWithQValueAndPredicate() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
final EncodingHandler handler = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("compress", ContentEncodingProvider.IDENTITY, 100, Predicates.falsePredicate()).addEncodingHandler("bzip", ContentEncodingProvider.IDENTITY, 50)).setNext(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
//we need some content to encode
exchange.getResponseSender().send("hi");
}
});
DefaultServer.setRootHandler(handler);
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "bzip, compress;q=0.6");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Header[] header = result.getHeaders(HEADER);
Assert.assertEquals("bzip", header[0].getValue());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "*;q=0.00");
result = client.execute(get);
Assert.assertEquals(StatusCodes.NOT_ACCEPTABLE, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "compress");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals(0, header.length);
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "*;q=0.00 bzip;q=0.3");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals("bzip", header[0].getValue());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "compress;q=0.1 bzip;q=0.05");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals("bzip", header[0].getValue());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "compress;q=0.1, bzip;q=1.000");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals("bzip", header[0].getValue());
HttpClientUtils.readResponse(result);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class EncodingSelectionTestCase method testEncodingSelectWithQValue.
/**
* Tests encoding selection with a qvalue
*
* @throws IOException
*/
@Test
public void testEncodingSelectWithQValue() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
final EncodingHandler handler = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("compress", ContentEncodingProvider.IDENTITY, 100).addEncodingHandler("bzip", ContentEncodingProvider.IDENTITY, 50)).setNext(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
//we need some content to encode
exchange.getResponseSender().send("hi");
}
});
DefaultServer.setRootHandler(handler);
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "bzip, compress;q=0.6");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Header[] header = result.getHeaders(HEADER);
Assert.assertEquals("bzip", header[0].getValue());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "*;q=0.00");
result = client.execute(get);
Assert.assertEquals(StatusCodes.NOT_ACCEPTABLE, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "*;q=0.00 bzip");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals("bzip", header[0].getValue());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "*;q=0.00 bzip;q=0.3");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals("bzip", header[0].getValue());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "compress;q=0.1 bzip;q=0.05");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals("compress", header[0].getValue());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "compress;q=0.1, bzip;q=1.000");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals("bzip", header[0].getValue());
HttpClientUtils.readResponse(result);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class EncodingSelectionTestCase method testBasicEncodingSelect.
/**
* Tests encoding selection with no qvalue
* <p/>
* Also tests a lot of non standard formats for Accept-Encoding to make sure that
* we are liberal in what we accept
*
* @throws IOException
*/
@Test
public void testBasicEncodingSelect() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
final EncodingHandler handler = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("compress", ContentEncodingProvider.IDENTITY, 50).addEncodingHandler("bzip", ContentEncodingProvider.IDENTITY, 100)).setNext(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
//we need some content to encode
exchange.getResponseSender().send("hi");
}
});
DefaultServer.setRootHandler(handler);
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Header[] header = result.getHeaders(HEADER);
Assert.assertEquals(0, header.length);
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "bzip");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals("bzip", header[0].getValue());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "bzip compress identity someOtherEncoding");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals("bzip", header[0].getValue());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, " compress, identity, someOtherEncoding, bzip , ");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals("bzip", header[0].getValue());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "boo; compress, identity; someOtherEncoding, , ");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals("compress", header[0].getValue());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ACCEPT_ENCODING_STRING, "boo; compress; identity; someOtherEncoding, , ");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders(HEADER);
Assert.assertEquals("compress", header[0].getValue());
HttpClientUtils.readResponse(result);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class GzipContentEncodingTestCase method setup.
@BeforeClass
public static void setup() {
final EncodingHandler handler = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("gzip", new GzipEncodingProvider(), 50, Predicates.parse("max-content-size[5]"))).setNext(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, message.length() + "");
exchange.getResponseSender().send(message, IoCallback.END_EXCHANGE);
}
});
DefaultServer.setRootHandler(handler);
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class RequestContentEncodingTestCase method setup.
@BeforeClass
public static void setup() {
final ContentEncodingRepository contentEncodingRepository = new ContentEncodingRepository().addEncodingHandler("deflate", new DeflateEncodingProvider(), 50).addEncodingHandler("gzip", new GzipEncodingProvider(), 60);
final EncodingHandler encode = new EncodingHandler(contentEncodingRepository).setNext(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, message.length() + "");
exchange.getResponseSender().send(message, IoCallback.END_EXCHANGE);
}
});
final EncodingHandler wrappedEncode = new EncodingHandler(contentEncodingRepository).setNext(encode);
final HttpHandler decode = new RequestEncodingHandler(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getRequestReceiver().receiveFullBytes(new Receiver.FullBytesCallback() {
@Override
public void handle(HttpServerExchange exchange, byte[] message) {
exchange.getResponseSender().send(ByteBuffer.wrap(message));
}
});
}
}).addEncoding("deflate", InflatingStreamSourceConduit.WRAPPER).addEncoding("gzip", GzipStreamSourceConduit.WRAPPER);
final HttpHandler wrappedDecode = new RequestEncodingHandler(decode).addEncoding("deflate", InflatingStreamSourceConduit.WRAPPER).addEncoding("gzip", GzipStreamSourceConduit.WRAPPER);
PathHandler pathHandler = new PathHandler();
pathHandler.addPrefixPath("/encode", wrappedEncode);
pathHandler.addPrefixPath("/decode", wrappedDecode);
DefaultServer.setRootHandler(pathHandler);
}
Aggregations