use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.
the class HttpFieldsTest method testCachedPut.
@Test
public void testCachedPut() throws Exception {
HttpFields header = new HttpFields();
header.put("Connection", "Keep-Alive");
header.put("tRansfer-EncOding", "CHUNKED");
header.put("CONTENT-ENCODING", "gZIP");
ByteBuffer buffer = BufferUtil.allocate(1024);
BufferUtil.flipToFill(buffer);
HttpGenerator.putTo(header, buffer);
BufferUtil.flipToFlush(buffer, 0);
String out = BufferUtil.toString(buffer).toLowerCase(Locale.ENGLISH);
Assert.assertThat(out, Matchers.containsString((HttpHeader.CONNECTION + ": " + HttpHeaderValue.KEEP_ALIVE).toLowerCase(Locale.ENGLISH)));
Assert.assertThat(out, Matchers.containsString((HttpHeader.TRANSFER_ENCODING + ": " + HttpHeaderValue.CHUNKED).toLowerCase(Locale.ENGLISH)));
Assert.assertThat(out, Matchers.containsString((HttpHeader.CONTENT_ENCODING + ": " + HttpHeaderValue.GZIP).toLowerCase(Locale.ENGLISH)));
}
use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.
the class HTTP2CServerTest method testHTTP_2_0_DirectWithoutH2C.
@Test
public void testHTTP_2_0_DirectWithoutH2C() throws Exception {
AtomicLong fills = new AtomicLong();
// Remove "h2c", leaving only "http/1.1".
connector.clearConnectionFactories();
HttpConnectionFactory connectionFactory = new HttpConnectionFactory() {
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
HttpConnection connection = new HttpConnection(getHttpConfiguration(), connector, endPoint, getHttpCompliance(), isRecordHttpComplianceViolations()) {
@Override
public void onFillable() {
fills.incrementAndGet();
super.onFillable();
}
};
return configure(connection, connector, endPoint);
}
};
connector.addConnectionFactory(connectionFactory);
connector.setDefaultProtocol(connectionFactory.getProtocol());
// Now send a HTTP/2 direct request, which
// will have the PRI * HTTP/2.0 preface.
byteBufferPool = new MappedByteBufferPool();
generator = new Generator(byteBufferPool);
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
// We sent a HTTP/2 preface, but the server has no "h2c" connection
// factory so it does not know how to handle this request.
InputStream input = client.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
String responseLine = reader.readLine();
Assert.assertThat(responseLine, Matchers.containsString(" 426 "));
while (true) {
if (reader.read() < 0)
break;
}
}
// Make sure we did not spin.
Thread.sleep(1000);
Assert.assertThat(fills.get(), Matchers.lessThan(5L));
}
use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.
the class HttpServerTestBase method testInterruptedRequest.
@Test
public void testInterruptedRequest() throws Exception {
final AtomicBoolean fourBytesRead = new AtomicBoolean(false);
final AtomicBoolean earlyEOFException = new AtomicBoolean(false);
configureServer(new AbstractHandler.ErrorDispatchHandler() {
@Override
public void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
int contentLength = request.getContentLength();
ServletInputStream inputStream = request.getInputStream();
for (int i = 0; i < contentLength; i++) {
try {
inputStream.read();
} catch (EofException e) {
earlyEOFException.set(true);
throw new QuietServletException(e);
}
if (i == 3)
fourBytesRead.set(true);
}
}
});
StringBuffer request = new StringBuffer("GET / HTTP/1.0\n");
request.append("Host: localhost\n");
request.append("Content-length: 6\n\n");
request.append("foo");
Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
OutputStream os = client.getOutputStream();
os.write(request.toString().getBytes());
os.flush();
client.shutdownOutput();
String response = readResponse(client);
client.close();
assertThat("response contains 500", response, Matchers.containsString(" 500 "));
assertThat("The 4th byte (-1) has not been passed to the handler", fourBytesRead.get(), is(false));
assertThat("EofException has been caught", earlyEOFException.get(), is(true));
}
use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.
the class HttpServerTestBase method testFullURI.
/*
* Feed a full header method
*/
@Test
public void testFullURI() throws Exception {
configureServer(new HelloWorldHandler());
try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
StacklessLogging stackless = new StacklessLogging(HttpConnection.class)) {
((AbstractLogger) Log.getLogger(HttpConnection.class)).info("expect URI is too large, then ISE extra data ...");
OutputStream os = client.getOutputStream();
byte[] buffer = new byte[64 * 1024];
buffer[0] = 'G';
buffer[1] = 'E';
buffer[2] = 'T';
buffer[3] = ' ';
buffer[4] = '/';
Arrays.fill(buffer, 5, buffer.length - 1, (byte) 'A');
os.write(buffer);
os.flush();
// Read the response.
String response = readResponse(client);
Assert.assertThat(response, Matchers.containsString("HTTP/1.1 414 "));
}
}
use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.
the class HttpServerTestBase method testExceptionThrownInHandler.
@Test
public void testExceptionThrownInHandler() throws Exception {
configureServer(new AbstractHandler.ErrorDispatchHandler() {
@Override
public void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
throw new QuietServletException("TEST handler exception");
}
});
StringBuffer request = new StringBuffer("GET / HTTP/1.0\r\n");
request.append("Host: localhost\r\n\r\n");
Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
OutputStream os = client.getOutputStream();
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
Log.getLogger(HttpChannel.class).info("Expecting ServletException: TEST handler exception...");
os.write(request.toString().getBytes());
os.flush();
String response = readResponse(client);
assertThat(response, Matchers.containsString(" 500 "));
}
}
Aggregations