Search in sources :

Example 16 with HeaderElement

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

the class TestBasicHeaderValueFormatter method testInvalidArguments.

@Test
public void testInvalidArguments() throws Exception {
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    final NameValuePair param = new BasicNameValuePair("param", "regular_stuff");
    final NameValuePair[] params = new NameValuePair[] { param };
    final HeaderElement element = new BasicHeaderElement("name1", "value1", null);
    final HeaderElement[] elements = new HeaderElement[] { element };
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatNameValuePair(null, param, false));
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatNameValuePair(buf, null, false));
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatParameters(null, params, false));
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatParameters(buf, null, false));
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatHeaderElement(null, element, false));
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatHeaderElement(buf, null, false));
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatElements(null, elements, false));
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatElements(buf, null, false));
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) HeaderElement(org.apache.hc.core5.http.HeaderElement) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 17 with HeaderElement

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

the class TestBasicHeaderValueFormatter method testElementsFormatting.

@Test
public void testElementsFormatting() throws Exception {
    final NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff");
    final NameValuePair param2 = new BasicNameValuePair("param", "this\\that");
    final NameValuePair param3 = new BasicNameValuePair("param", "this,that");
    final NameValuePair param4 = new BasicNameValuePair("param", null);
    final HeaderElement element1 = new BasicHeaderElement("name1", "value1", new NameValuePair[] { param1 });
    final HeaderElement element2 = new BasicHeaderElement("name2", "value2", new NameValuePair[] { param2 });
    final HeaderElement element3 = new BasicHeaderElement("name3", "value3", new NameValuePair[] { param3 });
    final HeaderElement element4 = new BasicHeaderElement("name4", "value4", new NameValuePair[] { param4 });
    final HeaderElement element5 = new BasicHeaderElement("name5", null);
    final HeaderElement[] elements = new HeaderElement[] { element1, element2, element3, element4, element5 };
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    this.formatter.formatElements(buf, elements, false);
    Assertions.assertEquals("name1=value1; param=regular_stuff, name2=value2; " + "param=\"this\\\\that\", name3=value3; param=\"this,that\", " + "name4=value4; param, name5", buf.toString());
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) HeaderElement(org.apache.hc.core5.http.HeaderElement) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 18 with HeaderElement

use of org.apache.hc.core5.http.HeaderElement 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 19 with HeaderElement

use of org.apache.hc.core5.http.HeaderElement 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

HeaderElement (org.apache.hc.core5.http.HeaderElement)17 Test (org.junit.jupiter.api.Test)13 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)9 NameValuePair (org.apache.hc.core5.http.NameValuePair)5 Header (org.apache.hc.core5.http.Header)3 ParserCursor (org.apache.hc.core5.http.message.ParserCursor)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)1 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)1 HttpRequest (org.apache.hc.core5.http.HttpRequest)1 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)1 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)1 TimeValue (org.apache.hc.core5.util.TimeValue)1 Option (org.eclipse.californium.core.coap.Option)1 OptionSet (org.eclipse.californium.core.coap.OptionSet)1