{
@Test
public void testUnknownFieldsContextManagement()
{
HpackEncoder encoder = new HpackEncoder(38*5);
HttpFields fields = new HttpFields();
HttpField[] field =
{
new HttpField("fo0","b0r"),
new HttpField("fo1","b1r"),
new HttpField("fo2","b2r"),
new HttpField("fo3","b3r"),
new HttpField("fo4","b4r"),
new HttpField("fo5","b5r"),
new HttpField("fo6","b6r"),
new HttpField("fo7","b7r"),
new HttpField("fo8","b8r"),
new HttpField("fo9","b9r"),
new HttpField("foA","bAr"),
};
// Add 4 entries
for (int i=0;i<=3;i++)
fields.add(field[i]);
// encode them
ByteBuffer buffer = BufferUtil.allocate(4096);
int pos = BufferUtil.flipToFill(buffer);
encoder.encode(buffer,new MetaData(HttpVersion.HTTP_2,fields));
BufferUtil.flipToFlush(buffer,pos);
// something was encoded!
assertThat(buffer.remaining(),Matchers.greaterThan(0));
// All are in the header table
Assert.assertEquals(4,encoder.getContext().size());
// encode exact same fields again!
BufferUtil.clearToFill(buffer);
encoder.encode(buffer,new MetaData(HttpVersion.HTTP_2,fields));
BufferUtil.flipToFlush(buffer,0);
// All are in the header table
Assert.assertEquals(4,encoder.getContext().size());
// Add 4 more fields
for (int i=4;i<=7;i++)
fields.add(field[i]);
// encode
BufferUtil.clearToFill(buffer);
encoder.encode(buffer,new MetaData(HttpVersion.HTTP_2,fields));
BufferUtil.flipToFlush(buffer,0);
// something was encoded!
assertThat(buffer.remaining(),Matchers.greaterThan(0));
// max header table size reached
Assert.assertEquals(5,encoder.getContext().size());
// remove some fields
for (int i=0;i<=7;i+=2)
fields.remove(field[i].getName());
// encode
BufferUtil.clearToFill(buffer);
encoder.encode(buffer,new MetaData(HttpVersion.HTTP_2,fields));
BufferUtil.flipToFlush(buffer,0);
// something was encoded!
assertThat(buffer.remaining(),Matchers.greaterThan(0));
// max header table size reached
Assert.assertEquals(5,encoder.getContext().size());
// remove another fields
fields.remove(field[1].getName());
// encode
BufferUtil.clearToFill(buffer);
encoder.encode(buffer,new MetaData(HttpVersion.HTTP_2,fields));
BufferUtil.flipToFlush(buffer,0);
// something was encoded!
assertThat(buffer.remaining(),Matchers.greaterThan(0));
// max header table size reached
Assert.assertEquals(5,encoder.getContext().size());
// re add the field
fields.add(field[1]);
// encode
BufferUtil.clearToFill(buffer);
encoder.encode(buffer,new MetaData(HttpVersion.HTTP_2,fields));
BufferUtil.flipToFlush(buffer,0);