Search in sources :

Example 21 with ParserCursor

use of org.apache.hc.core5.http.message.ParserCursor in project httpcomponents-core by apache.

the class TestBasicLineParser method testInvalidHttpVersionParsing.

@Test
public void testInvalidHttpVersionParsing() throws Exception {
    final CharArrayBuffer buffer = new CharArrayBuffer(16);
    buffer.clear();
    buffer.append("    ");
    final ParserCursor cursor1 = new ParserCursor(0, buffer.length());
    Assertions.assertThrows(ParseException.class, () -> parser.parseProtocolVersion(buffer, cursor1));
    buffer.clear();
    buffer.append("HTT");
    final ParserCursor cursor2 = new ParserCursor(0, buffer.length());
    Assertions.assertThrows(ParseException.class, () -> parser.parseProtocolVersion(buffer, cursor2));
    buffer.clear();
    buffer.append("crap");
    final ParserCursor cursor3 = new ParserCursor(0, buffer.length());
    Assertions.assertThrows(ParseException.class, () -> parser.parseProtocolVersion(buffer, cursor3));
    buffer.clear();
    buffer.append("HTTP/crap");
    final ParserCursor cursor4 = new ParserCursor(0, buffer.length());
    Assertions.assertThrows(ParseException.class, () -> parser.parseProtocolVersion(buffer, cursor4));
    buffer.clear();
    buffer.append("HTTP/1");
    final ParserCursor cursor5 = new ParserCursor(0, buffer.length());
    Assertions.assertThrows(ParseException.class, () -> parser.parseProtocolVersion(buffer, cursor5));
    buffer.clear();
    buffer.append("HTTP/1234");
    final ParserCursor cursor6 = new ParserCursor(0, buffer.length());
    Assertions.assertThrows(ParseException.class, () -> parser.parseProtocolVersion(buffer, cursor6));
    buffer.clear();
    buffer.append("HTTP/1.");
    final ParserCursor cursor7 = new ParserCursor(0, buffer.length());
    Assertions.assertThrows(ParseException.class, () -> parser.parseProtocolVersion(buffer, cursor7));
    buffer.clear();
    buffer.append("HTTP/whatever.whatever whatever");
    final ParserCursor cursor8 = new ParserCursor(0, buffer.length());
    Assertions.assertThrows(ParseException.class, () -> parser.parseProtocolVersion(buffer, cursor8));
    buffer.clear();
    buffer.append("HTTP/1.whatever whatever");
    final ParserCursor cursor9 = new ParserCursor(0, buffer.length());
    Assertions.assertThrows(ParseException.class, () -> parser.parseProtocolVersion(buffer, cursor9));
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 22 with ParserCursor

use of org.apache.hc.core5.http.message.ParserCursor in project httpcomponents-core by apache.

the class TestBasicHeaderValueParser method testNVParseEscaped.

@Test
public void testNVParseEscaped() {
    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 NameValuePair[] params = this.parser.parseParameters(buf, cursor);
    Assertions.assertEquals(3, params.length);
    Assertions.assertEquals("test1", params[0].getName());
    Assertions.assertEquals("\"stuff\"", params[0].getValue());
    Assertions.assertEquals("test2", params[1].getName());
    Assertions.assertEquals("\\", params[1].getValue());
    Assertions.assertEquals("test3", params[2].getName());
    Assertions.assertEquals("stuff; stuff", params[2].getValue());
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 23 with ParserCursor

use of org.apache.hc.core5.http.message.ParserCursor in project httpcomponents-core by apache.

the class TestBasicHeaderValueParser method testParseHEEscaped.

@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);
    Assertions.assertEquals(3, elements.length);
    Assertions.assertEquals("test1", elements[0].getName());
    Assertions.assertEquals("\"stuff\"", elements[0].getValue());
    Assertions.assertEquals("test2", elements[1].getName());
    Assertions.assertEquals("\\", elements[1].getValue());
    Assertions.assertEquals("test3", elements[2].getName());
    Assertions.assertEquals("stuff, stuff", elements[2].getValue());
}
Also used : HeaderElement(org.apache.hc.core5.http.HeaderElement) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 24 with ParserCursor

use of org.apache.hc.core5.http.message.ParserCursor in project httpcomponents-core by apache.

the class TestBasicHeaderValueParser method testNVParseAll.

@Test
public void testNVParseAll() {
    String s = "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
    CharArrayBuffer buffer = new CharArrayBuffer(16);
    buffer.append(s);
    ParserCursor cursor = new ParserCursor(0, s.length());
    NameValuePair[] params = this.parser.parseParameters(buffer, cursor);
    Assertions.assertEquals("test", params[0].getName());
    Assertions.assertNull(params[0].getValue());
    Assertions.assertEquals("test1", params[1].getName());
    Assertions.assertEquals("stuff", params[1].getValue());
    Assertions.assertEquals("test2", params[2].getName());
    Assertions.assertEquals("stuff; stuff", params[2].getValue());
    Assertions.assertEquals("test3", params[3].getName());
    Assertions.assertEquals("stuff", params[3].getValue());
    Assertions.assertEquals(s.length(), cursor.getPos());
    Assertions.assertTrue(cursor.atEnd());
    s = "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff\",123";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    params = this.parser.parseParameters(buffer, cursor);
    Assertions.assertEquals("test", params[0].getName());
    Assertions.assertNull(params[0].getValue());
    Assertions.assertEquals("test1", params[1].getName());
    Assertions.assertEquals("stuff", params[1].getValue());
    Assertions.assertEquals("test2", params[2].getName());
    Assertions.assertEquals("stuff; stuff", params[2].getValue());
    Assertions.assertEquals("test3", params[3].getName());
    Assertions.assertEquals("stuff", params[3].getValue());
    Assertions.assertEquals(s.length() - 3, cursor.getPos());
    Assertions.assertFalse(cursor.atEnd());
    s = "  ";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    params = this.parser.parseParameters(buffer, cursor);
    Assertions.assertEquals(0, params.length);
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 25 with ParserCursor

use of org.apache.hc.core5.http.message.ParserCursor in project httpcomponents-core by apache.

the class TestBasicHeaderValueParser method testParseHeaderElements.

@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);
    // there are 3 elements
    Assertions.assertEquals(3, elements.length);
    // 1st element
    Assertions.assertEquals("name1", elements[0].getName());
    Assertions.assertEquals("value1", elements[0].getValue());
    // 1st element has 2 getParameters()
    Assertions.assertEquals(2, elements[0].getParameters().length);
    Assertions.assertEquals("name2", elements[0].getParameters()[0].getName());
    Assertions.assertNull(elements[0].getParameters()[0].getValue());
    Assertions.assertEquals("name3", elements[0].getParameters()[1].getName());
    Assertions.assertEquals("value3", elements[0].getParameters()[1].getValue());
    // 2nd element
    Assertions.assertEquals("name4", elements[1].getName());
    Assertions.assertEquals("value4", elements[1].getValue());
    // 2nd element has 1 parameter
    Assertions.assertEquals(1, elements[1].getParameters().length);
    Assertions.assertEquals("name5", elements[1].getParameters()[0].getName());
    Assertions.assertEquals("value5", elements[1].getParameters()[0].getValue());
    // 3rd element
    Assertions.assertEquals("name6", elements[2].getName());
    Assertions.assertEquals("", elements[2].getValue());
    // 3rd element has 2 getParameters()
    Assertions.assertEquals(2, elements[2].getParameters().length);
    Assertions.assertEquals("name7", elements[2].getParameters()[0].getName());
    Assertions.assertEquals("value7", elements[2].getParameters()[0].getValue());
    Assertions.assertEquals("name8", elements[2].getParameters()[1].getName());
    Assertions.assertEquals(" value8", elements[2].getParameters()[1].getValue());
}
Also used : HeaderElement(org.apache.hc.core5.http.HeaderElement) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)12 Test (org.junit.jupiter.api.Test)11 HeaderElement (org.apache.hc.core5.http.HeaderElement)8 NameValuePair (org.apache.hc.core5.http.NameValuePair)7 ArrayList (java.util.ArrayList)5 ParserCursor (org.apache.hc.core5.http.message.ParserCursor)4 ParseException (org.apache.hc.core5.http.ParseException)3 FormattedHeader (org.apache.hc.core5.http.FormattedHeader)2 Header (org.apache.hc.core5.http.Header)2 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 LinkedList (java.util.LinkedList)1 HttpVersion (org.apache.hc.core5.http.HttpVersion)1 NameValuePair (org.apache.hc.core5.http.copied.NameValuePair)1 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)1 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)1 BasicNameValuePair (org.apache.hc.core5.http.message.copied.BasicNameValuePair)1 ParserCursor (org.apache.hc.core5.http.message.copied.ParserCursor)1 Tokenizer (org.apache.hc.core5.util.Tokenizer)1