BasicHeaderValueParser implements HeaderValueParser { @Override public HeaderElement[] parseElements(final CharSequence buffer, final ParserCursor cursor) { Args.notNull(buffer, "Char sequence"); Args.notNull(cursor, "Parser cursor"); final List<HeaderElement> elements = new ArrayList<>(); while (!cursor.atEnd()) { final HeaderElement element = parseHeaderElement(buffer, cursor); if (!(element.getName().isEmpty() && element.getValue() == null)) { elements.add(element); } } return elements.toArray(new HeaderElement[elements.size()]); } BasicHeaderValueParser(); @Override HeaderElement[] parseElements(final CharSequence buffer, final ParserCursor cursor); @Override HeaderElement parseHeaderElement(final CharSequence buffer, final ParserCursor cursor); @Override NameValuePair[] parseParameters(final CharSequence buffer, final ParserCursor cursor); @Override NameValuePair parseNameValuePair(final CharSequence buffer, final ParserCursor cursor); final static BasicHeaderValueParser INSTANCE; }
@Test public void testParseHeaderElements() throws Exception { final String headerValue = "name1 = value1; name2; name3=\"value3\" , name4=value4; " + "name5=value5, name6= ; name7 = value7; name8 = \" value8\""; final CharArrayBuffer buf = new CharArrayBuffer(64); buf.append(headerValue); final ParserCursor cursor = new ParserCursor(0, buf.length()); final HeaderElement[] elements = this.parser.parseElements(buf, cursor); Assert.assertEquals(3,elements.length); Assert.assertEquals("name1",elements[0].getName()); Assert.assertEquals("value1",elements[0].getValue()); Assert.assertEquals(2,elements[0].getParameters().length); Assert.assertEquals("name2",elements[0].getParameters()[0].getName()); Assert.assertEquals(null, elements[0].getParameters()[0].getValue()); Assert.assertEquals("name3",elements[0].getParameters()[1].getName()); Assert.assertEquals("value3",elements[0].getParameters()[1].getValue()); Assert.assertEquals("name4",elements[1].getName()); Assert.assertEquals("value4",elements[1].getValue()); Assert.assertEquals(1,elements[1].getParameters().length); Assert.assertEquals("name5",elements[1].getParameters()[0].getName()); Assert.assertEquals("value5",elements[1].getParameters()[0].getValue()); Assert.assertEquals("name6",elements[2].getName()); Assert.assertEquals("",elements[2].getValue()); Assert.assertEquals(2,elements[2].getParameters().length); Assert.assertEquals("name7",elements[2].getParameters()[0].getName()); Assert.assertEquals("value7",elements[2].getParameters()[0].getValue()); Assert.assertEquals("name8",elements[2].getParameters()[1].getName()); Assert.assertEquals(" value8",elements[2].getParameters()[1].getValue()); }
@Test public void testParseHEEscaped() { final String headerValue = "test1 = \"\\\"stuff\\\"\", test2= \"\\\\\", test3 = \"stuff, stuff\""; final CharArrayBuffer buf = new CharArrayBuffer(64); buf.append(headerValue); final ParserCursor cursor = new ParserCursor(0, buf.length()); final HeaderElement[] elements = this.parser.parseElements(buf, cursor); Assert.assertEquals(3, elements.length); Assert.assertEquals("test1", elements[0].getName()); Assert.assertEquals("\"stuff\"", elements[0].getValue()); Assert.assertEquals("test2", elements[1].getName()); Assert.assertEquals("\\", elements[1].getValue()); Assert.assertEquals("test3", elements[2].getName()); Assert.assertEquals("stuff, stuff", elements[2].getValue()); }
@Test public void testHEFringeCase1() throws Exception { final String headerValue = "name1 = value1,"; final CharArrayBuffer buf = new CharArrayBuffer(64); buf.append(headerValue); final ParserCursor cursor = new ParserCursor(0, buf.length()); final HeaderElement[] elements = this.parser.parseElements(buf, cursor); Assert.assertEquals("Number of elements", 1, elements.length); }
@Test public void testHEFringeCase2() throws Exception { final String headerValue = "name1 = value1, "; final CharArrayBuffer buf = new CharArrayBuffer(64); buf.append(headerValue); final ParserCursor cursor = new ParserCursor(0, buf.length()); final HeaderElement[] elements = this.parser.parseElements(buf, cursor); Assert.assertEquals("Number of elements", 1, elements.length); }
@Test public void testHEFringeCase3() throws Exception { final String headerValue = ",, ,, ,"; final CharArrayBuffer buf = new CharArrayBuffer(64); buf.append(headerValue); final ParserCursor cursor = new ParserCursor(0, buf.length()); final HeaderElement[] elements = this.parser.parseElements(buf, cursor); Assert.assertEquals("Number of elements", 0, elements.length); }