Fixing tests on http2-hpack

+ HttpField.nameHashCode() fixed to actually be US-ASCII case
  insensitive per documentation
+ Since removal of MetaData equals/hashcode, the comparison
  of the MetaData and MetaData.Response is now done entirely
  within the HpackTest
This commit is contained in:
Joakim Erdfelt 2015-02-20 10:16:03 -07:00
parent f61cfb7016
commit 08b4bd439e
3 changed files with 62 additions and 28 deletions

View File

@ -20,9 +20,6 @@ package org.eclipse.jetty.http;
import java.util.ArrayList;
import org.eclipse.jetty.util.StringUtil;
/* ------------------------------------------------------------ */
/** A HTTP Field
*/
public class HttpField
@ -383,13 +380,16 @@ public class HttpField
private int nameHashCode()
{
int hash=13;
int hash = 13;
int len = _name.length();
for (int i = 0; i < len; i++)
{
// simple case insensitive hash
char c = _name.charAt(i);
hash = 15 * hash + 0x0F&c;
// assuming us-ascii (per last paragraph on http://tools.ietf.org/html/rfc7230#section-3.2.4)
if ((c >= 'a' && c <= 'z'))
c -= 0x20;
hash = 15 * hash + c;
}
return hash;
}

View File

@ -18,18 +18,14 @@
package org.eclipse.jetty.http;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import java.nio.ByteBuffer;
import org.eclipse.jetty.util.BufferUtil;
import org.junit.Test;
/**
*
*/
public class HttpFieldTest
{
@ -46,6 +42,24 @@ public class HttpFieldTest
assertFalse(field.contains(null));
}
@Test
public void testCaseInsensitiveHashcode_KnownField() throws Exception
{
HttpField fieldFoo1 = new HttpField("Cookie","foo");
HttpField fieldFoo2 = new HttpField("cookie","foo");
assertThat("Field hashcodes are case insensitive", fieldFoo1.hashCode(), is(fieldFoo2.hashCode()));
}
@Test
public void testCaseInsensitiveHashcode_UnknownField() throws Exception
{
HttpField fieldFoo1 = new HttpField("X-Foo","bar");
HttpField fieldFoo2 = new HttpField("x-foo","bar");
assertThat("Field hashcodes are case insensitive", fieldFoo1.hashCode(), is(fieldFoo2.hashCode()));
}
@Test
public void testContainsList() throws Exception
{

View File

@ -16,10 +16,10 @@
// ========================================================================
//
package org.eclipse.jetty.http2.hpack;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.nio.ByteBuffer;
@ -37,7 +37,6 @@ import org.eclipse.jetty.util.BufferUtil;
import org.junit.Assert;
import org.junit.Test;
public class HpackTest
{
final static HttpField ServerJetty = new PreEncodedHttpField(HttpHeader.SERVER,"jetty");
@ -66,7 +65,7 @@ public class HpackTest
BufferUtil.flipToFlush(buffer,0);
Response decoded0 = (Response)decoder.decode(buffer);
Assert.assertEquals(original0,decoded0);
assertMetadataSame(original0,decoded0);
// Same again?
BufferUtil.clearToFill(buffer);
@ -74,7 +73,7 @@ public class HpackTest
BufferUtil.flipToFlush(buffer,0);
Response decoded0b = (Response)decoder.decode(buffer);
Assert.assertEquals(original0,decoded0b);
assertMetadataSame(original0,decoded0b);
HttpFields fields1 = new HttpFields();
fields1.add(HttpHeader.CONTENT_TYPE,"text/plain");
@ -91,12 +90,10 @@ public class HpackTest
BufferUtil.flipToFlush(buffer,0);
Response decoded1 = (Response)decoder.decode(buffer);
Assert.assertEquals(original1,decoded1);
assertMetadataSame(original1,decoded1);
Assert.assertEquals("custom-key",decoded1.getFields().getField("Custom-Key").getName());
}
@Test
public void encodeDecodeTooLargeTest()
{
@ -114,7 +111,7 @@ public class HpackTest
BufferUtil.flipToFlush(buffer,0);
MetaData decoded0 = (MetaData)decoder.decode(buffer);
Assert.assertEquals(original0,decoded0);
assertMetadataSame(original0,decoded0);
HttpFields fields1 = new HttpFields();
fields1.add("1234567890","1234567890123456789012345678901234567890");
@ -134,12 +131,7 @@ public class HpackTest
{
assertEquals(HttpStatus.REQUEST_ENTITY_TOO_LARGE_413,e.getCode());
}
}
@Test
public void evictReferencedFieldTest()
@ -163,8 +155,7 @@ public class HpackTest
assertEquals("123456789012345678901234567890123456788901234567890",encoder.getHpackContext().get(HpackContext.STATIC_TABLE.length+1).getHttpField().getName());
assertEquals("foo",encoder.getHpackContext().get(HpackContext.STATIC_TABLE.length+0).getHttpField().getName());
assertEquals(original0,decoded0);
assertMetadataSame(original0,decoded0);
HttpFields fields1 = new HttpFields();
fields1.add("123456789012345678901234567890123456788901234567890","other_value");
@ -175,7 +166,7 @@ public class HpackTest
encoder.encode(buffer,original1);
BufferUtil.flipToFlush(buffer,0);
MetaData decoded1 = (MetaData)decoder.decode(buffer);
assertEquals(original1,decoded1);
assertMetadataSame(original1,decoded1);
assertEquals(2,encoder.getHpackContext().size());
assertEquals(2,decoder.getHpackContext().size());
@ -183,4 +174,33 @@ public class HpackTest
assertEquals("foo",encoder.getHpackContext().get(HpackContext.STATIC_TABLE.length+1).getHttpField().getName());
}
private void assertMetadataSame(MetaData.Response expected, MetaData.Response actual)
{
assertThat("Response.status", actual.getStatus(), is(expected.getStatus()));
assertThat("Response.reason", actual.getReason(), is(expected.getReason()));
assertMetadataSame((MetaData)expected,(MetaData)actual);
}
private void assertMetadataSame(MetaData expected, MetaData actual)
{
assertThat("Metadata.contentLength",actual.getContentLength(),is(expected.getContentLength()));
assertThat("Metadata.version" + ".version", actual.getVersion(), is(expected.getVersion()));
assertHttpFieldsSame("Metadata.fields",expected.getFields(),actual.getFields());
}
private void assertHttpFieldsSame(String msg, HttpFields expected, HttpFields actual)
{
assertThat(msg + ".size", actual.size(), is(expected.size()));
for (HttpField actualField : actual)
{
if ("DATE".equalsIgnoreCase(actualField.getName()))
{
// skip comparison on Date, as these values can often differ by 1 second
// during testing.
continue;
}
assertThat(msg + ".contains(" + actualField + ")",expected.contains(actualField),is(true));
}
}
}