Search in sources :

Example 56 with NameValuePair

use of org.apache.hc.core5.http.copied.NameValuePair 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 57 with NameValuePair

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

the class TestBasicHeaderValueFormatter method testParamsFormatting.

@Test
public void testParamsFormatting() 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[] params = new NameValuePair[] { param1, param2, param3 };
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    buf.clear();
    this.formatter.formatParameters(buf, params, false);
    Assertions.assertEquals("param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\"", buf.toString());
    buf.clear();
    this.formatter.formatParameters(buf, params, true);
    Assertions.assertEquals("param=\"regular_stuff\"; param=\"this\\\\that\"; param=\"this,that\"", buf.toString());
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 58 with NameValuePair

use of org.apache.hc.core5.http.copied.NameValuePair 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 59 with NameValuePair

use of org.apache.hc.core5.http.copied.NameValuePair 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 60 with NameValuePair

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

the class TestURIBuilder method createParameterList.

private List<NameValuePair> createParameterList() {
    final List<NameValuePair> parameters = new ArrayList<>();
    parameters.add(new BasicNameValuePair("parameter1", "value1"));
    parameters.add(new BasicNameValuePair("parameter2", "\"1\u00aa position\""));
    parameters.add(new BasicNameValuePair("parameter3", "Jos\u00e9 Abra\u00e3o"));
    return parameters;
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList)

Aggregations

NameValuePair (org.apache.hc.core5.http.NameValuePair)56 Test (org.junit.jupiter.api.Test)26 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)25 ArrayList (java.util.ArrayList)15 URI (java.net.URI)13 UrlEncodedFormEntity (org.apache.hc.client5.http.entity.UrlEncodedFormEntity)12 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)11 Map (java.util.Map)10 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)9 IOException (java.io.IOException)7 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)7 URIBuilder (org.apache.hc.core5.net.URIBuilder)7 URISyntaxException (java.net.URISyntaxException)6 HashMap (java.util.HashMap)5 HeaderElement (org.apache.hc.core5.http.HeaderElement)5 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)4 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)4 List (java.util.List)3 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)3 HttpEntity (org.apache.hc.core5.http.HttpEntity)3