use of org.junit.Test in project jetty.project by eclipse.
the class HttpGeneratorServerTest method testConnectionKeepAliveWithAdditionalCustomValue.
@Test
public void testConnectionKeepAliveWithAdditionalCustomValue() throws Exception {
HttpGenerator generator = new HttpGenerator();
HttpFields fields = new HttpFields();
fields.put(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE);
String customValue = "test";
fields.add(HttpHeader.CONNECTION, customValue);
MetaData.Response info = new MetaData.Response(HttpVersion.HTTP_1_0, 200, "OK", fields, -1);
ByteBuffer header = BufferUtil.allocate(4096);
HttpGenerator.Result result = generator.generateResponse(info, false, header, null, null, true);
Assert.assertSame(HttpGenerator.Result.FLUSH, result);
String headers = BufferUtil.toString(header);
Assert.assertTrue(headers.contains(HttpHeaderValue.KEEP_ALIVE.asString()));
Assert.assertTrue(headers.contains(customValue));
}
use of org.junit.Test in project jetty.project by eclipse.
the class HttpGeneratorServerTest method testComplexChars.
@Test
public void testComplexChars() throws Exception {
ByteBuffer header = BufferUtil.allocate(8096);
ByteBuffer content = BufferUtil.toBuffer("0123456789");
HttpGenerator gen = new HttpGenerator();
HttpGenerator.Result result = gen.generateResponse(null, false, null, null, content, true);
assertEquals(HttpGenerator.Result.NEED_INFO, result);
assertEquals(HttpGenerator.State.START, gen.getState());
MetaData.Response info = new MetaData.Response(HttpVersion.HTTP_1_1, 200, null, new HttpFields(), 10);
info.getFields().add("Content-Type", "test/data;\r\nextra=value");
info.getFields().add("Last-Modified", DateGenerator.__01Jan1970);
result = gen.generateResponse(info, false, null, null, content, true);
assertEquals(HttpGenerator.Result.NEED_HEADER, result);
result = gen.generateResponse(info, false, header, null, content, true);
assertEquals(HttpGenerator.Result.FLUSH, result);
assertEquals(HttpGenerator.State.COMPLETING, gen.getState());
String response = BufferUtil.toString(header);
BufferUtil.clear(header);
response += BufferUtil.toString(content);
BufferUtil.clear(content);
result = gen.generateResponse(null, false, null, null, content, false);
assertEquals(HttpGenerator.Result.DONE, result);
assertEquals(HttpGenerator.State.END, gen.getState());
assertEquals(10, gen.getContentPrepared());
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, containsString("Last-Modified: Thu, 01 Jan 1970 00:00:00 GMT"));
assertThat(response, containsString("Content-Type: test/data; extra=value"));
assertThat(response, containsString("Content-Length: 10"));
assertThat(response, containsString("\r\n0123456789"));
}
use of org.junit.Test in project jetty.project by eclipse.
the class HttpGeneratorServerTest method testResponseWithChunkedContent.
@Test
public void testResponseWithChunkedContent() throws Exception {
ByteBuffer header = BufferUtil.allocate(4096);
ByteBuffer chunk = BufferUtil.allocate(HttpGenerator.CHUNK_SIZE);
ByteBuffer content0 = BufferUtil.toBuffer("Hello World! ");
ByteBuffer content1 = BufferUtil.toBuffer("The quick brown fox jumped over the lazy dog. ");
HttpGenerator gen = new HttpGenerator();
HttpGenerator.Result result = gen.generateResponse(null, false, null, null, content0, false);
assertEquals(HttpGenerator.Result.NEED_INFO, result);
assertEquals(HttpGenerator.State.START, gen.getState());
MetaData.Response info = new MetaData.Response(HttpVersion.HTTP_1_1, 200, null, new HttpFields(), -1);
info.getFields().add("Last-Modified", DateGenerator.__01Jan1970);
result = gen.generateResponse(info, false, null, null, content0, false);
assertEquals(HttpGenerator.Result.NEED_HEADER, result);
assertEquals(HttpGenerator.State.START, gen.getState());
result = gen.generateResponse(info, false, header, null, content0, false);
assertEquals(HttpGenerator.Result.FLUSH, result);
assertEquals(HttpGenerator.State.COMMITTED, gen.getState());
String out = BufferUtil.toString(header);
BufferUtil.clear(header);
out += BufferUtil.toString(content0);
BufferUtil.clear(content0);
result = gen.generateResponse(null, false, null, chunk, content1, false);
assertEquals(HttpGenerator.Result.FLUSH, result);
assertEquals(HttpGenerator.State.COMMITTED, gen.getState());
out += BufferUtil.toString(chunk);
BufferUtil.clear(chunk);
out += BufferUtil.toString(content1);
BufferUtil.clear(content1);
result = gen.generateResponse(null, false, null, chunk, null, true);
assertEquals(HttpGenerator.Result.CONTINUE, result);
assertEquals(HttpGenerator.State.COMPLETING, gen.getState());
result = gen.generateResponse(null, false, null, chunk, null, true);
assertEquals(HttpGenerator.Result.FLUSH, result);
assertEquals(HttpGenerator.State.COMPLETING, gen.getState());
out += BufferUtil.toString(chunk);
BufferUtil.clear(chunk);
result = gen.generateResponse(null, false, null, chunk, null, true);
assertEquals(HttpGenerator.Result.DONE, result);
assertEquals(HttpGenerator.State.END, gen.getState());
assertThat(out, containsString("HTTP/1.1 200 OK"));
assertThat(out, containsString("Last-Modified: Thu, 01 Jan 1970 00:00:00 GMT"));
assertThat(out, not(containsString("Content-Length")));
assertThat(out, containsString("Transfer-Encoding: chunked"));
assertThat(out, endsWith("\r\n\r\nD\r\n" + "Hello World! \r\n" + "2E\r\n" + "The quick brown fox jumped over the lazy dog. \r\n" + "0\r\n" + "\r\n"));
}
use of org.junit.Test in project jetty.project by eclipse.
the class HttpParserTest method testCaseInsensitive.
@Test
public void testCaseInsensitive() throws Exception {
ByteBuffer buffer = BufferUtil.toBuffer("get / http/1.0\r\n" + "HOST: localhost\r\n" + "cOnNeCtIoN: ClOsE\r\n" + "\r\n");
HttpParser.RequestHandler handler = new Handler();
HttpParser parser = new HttpParser(handler, -1, HttpCompliance.RFC7230);
parseAll(parser, buffer);
Assert.assertNull(_bad);
Assert.assertEquals("GET", _methodOrVersion);
Assert.assertEquals("/", _uriOrStatus);
Assert.assertEquals("HTTP/1.0", _versionOrReason);
Assert.assertEquals("Host", _hdr[0]);
Assert.assertEquals("localhost", _val[0]);
Assert.assertEquals("Connection", _hdr[1]);
Assert.assertEquals("close", _val[1]);
Assert.assertEquals(1, _headers);
Assert.assertNull(_complianceViolation);
}
use of org.junit.Test in project jetty.project by eclipse.
the class HttpParserTest method testNoColon7230.
@Test
public void testNoColon7230() throws Exception {
ByteBuffer buffer = BufferUtil.toBuffer("GET / HTTP/1.0\r\n" + "Host: localhost\r\n" + "Name\r\n" + "\r\n");
HttpParser.RequestHandler handler = new Handler();
HttpParser parser = new HttpParser(handler, HttpCompliance.RFC7230);
parseAll(parser, buffer);
Assert.assertThat(_bad, Matchers.containsString("Illegal character"));
Assert.assertNull(_complianceViolation);
}
Aggregations