use of org.junit.Test in project jetty.project by eclipse.
the class HttpParserTest method testParseRequest.
@Test
public void testParseRequest() throws Exception {
ByteBuffer buffer = BufferUtil.toBuffer("GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Header1: value1\r\n" + "Connection: close\r\n" + "Accept-Encoding: gzip, deflated\r\n" + "Accept: unknown\r\n" + "\r\n");
HttpParser.RequestHandler handler = new Handler();
HttpParser parser = new HttpParser(handler);
parser.parseNext(buffer);
Assert.assertEquals("GET", _methodOrVersion);
Assert.assertEquals("/", _uriOrStatus);
Assert.assertEquals("HTTP/1.1", _versionOrReason);
Assert.assertEquals("Host", _hdr[0]);
Assert.assertEquals("localhost", _val[0]);
Assert.assertEquals("Connection", _hdr[2]);
Assert.assertEquals("close", _val[2]);
Assert.assertEquals("Accept-Encoding", _hdr[3]);
Assert.assertEquals("gzip, deflated", _val[3]);
Assert.assertEquals("Accept", _hdr[4]);
Assert.assertEquals("unknown", _val[4]);
}
use of org.junit.Test in project jetty.project by eclipse.
the class HttpParserTest method testHostPort.
@Test
public void testHostPort() throws Exception {
ByteBuffer buffer = BufferUtil.toBuffer("GET / HTTP/1.1\r\n" + "Host: myhost:8888\r\n" + "Connection: close\r\n" + "\r\n");
HttpParser.RequestHandler handler = new Handler();
HttpParser parser = new HttpParser(handler);
parser.parseNext(buffer);
Assert.assertEquals("myhost", _host);
Assert.assertEquals(8888, _port);
}
use of org.junit.Test in project jetty.project by eclipse.
the class HttpParserTest method testBadRequestVersion.
@Test
public void testBadRequestVersion() throws Exception {
ByteBuffer buffer = BufferUtil.toBuffer("GET / HPPT/7.7\r\n" + "Content-Length: 0\r\n" + "Connection: close\r\n" + "\r\n");
HttpParser.RequestHandler handler = new Handler();
HttpParser parser = new HttpParser(handler);
parser.parseNext(buffer);
Assert.assertEquals(null, _methodOrVersion);
Assert.assertEquals("Unknown Version", _bad);
Assert.assertFalse(buffer.hasRemaining());
Assert.assertEquals(HttpParser.State.CLOSE, parser.getState());
parser.atEOF();
parser.parseNext(BufferUtil.EMPTY_BUFFER);
Assert.assertEquals(HttpParser.State.CLOSED, parser.getState());
buffer = BufferUtil.toBuffer("GET / HTTP/1.01\r\n" + "Content-Length: 0\r\n" + "Connection: close\r\n" + "\r\n");
handler = new Handler();
parser = new HttpParser(handler);
parser.parseNext(buffer);
Assert.assertEquals(null, _methodOrVersion);
Assert.assertEquals("Unknown Version", _bad);
Assert.assertFalse(buffer.hasRemaining());
Assert.assertEquals(HttpParser.State.CLOSE, parser.getState());
parser.atEOF();
parser.parseNext(BufferUtil.EMPTY_BUFFER);
Assert.assertEquals(HttpParser.State.CLOSED, parser.getState());
}
use of org.junit.Test 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.junit.Test in project jetty.project by eclipse.
the class HTTP2CServerTest method testHTTP_1_1_Simple.
@Test
public void testHTTP_1_1_Simple() throws Exception {
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
client.getOutputStream().write("GET /one HTTP/1.1\r\nHost: localhost\r\n\r\n".getBytes(StandardCharsets.ISO_8859_1));
client.getOutputStream().write("GET /two HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n".getBytes(StandardCharsets.ISO_8859_1));
client.getOutputStream().flush();
String response = IO.toString(client.getInputStream());
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, containsString("Hello from Jetty using HTTP/1.1"));
assertThat(response, containsString("uri=/one"));
assertThat(response, containsString("uri=/two"));
}
}
Aggregations