Search in sources :

Example 16 with NameValuePair

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

the class TestBasicHeaderValueParser method testNVParse.

@Test
public void testNVParse() {
    String s = "test";
    CharArrayBuffer buffer = new CharArrayBuffer(64);
    buffer.append(s);
    ParserCursor cursor = new ParserCursor(0, s.length());
    NameValuePair param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertNull(param.getValue());
    Assertions.assertEquals(s.length(), cursor.getPos());
    Assertions.assertTrue(cursor.atEnd());
    s = "test;";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertNull(param.getValue());
    Assertions.assertEquals(s.length(), cursor.getPos());
    Assertions.assertTrue(cursor.atEnd());
    s = "test  ,12";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertNull(param.getValue());
    Assertions.assertEquals(s.length() - 2, cursor.getPos());
    Assertions.assertFalse(cursor.atEnd());
    s = "test=stuff";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertEquals("stuff", param.getValue());
    Assertions.assertEquals(s.length(), cursor.getPos());
    Assertions.assertTrue(cursor.atEnd());
    s = "   test  =   stuff ";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertEquals("stuff", param.getValue());
    Assertions.assertEquals(s.length(), cursor.getPos());
    Assertions.assertTrue(cursor.atEnd());
    s = "   test  =   stuff ;1234";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertEquals("stuff", param.getValue());
    Assertions.assertEquals(s.length() - 4, cursor.getPos());
    Assertions.assertFalse(cursor.atEnd());
    s = "test  = \"stuff\"";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertEquals("stuff", param.getValue());
    s = "test  = \"  stuff\\\"\"";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertEquals("  stuff\"", param.getValue());
    s = "  test";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertNull(param.getValue());
    s = "  ";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("", param.getName());
    Assertions.assertNull(param.getValue());
    s = " = stuff ";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("", param.getName());
    Assertions.assertEquals("stuff", param.getValue());
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 17 with NameValuePair

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

the class TestURIBuilder method testSetParametersWithNullArrayArg.

@Test
public void testSetParametersWithNullArrayArg() throws Exception {
    final URI uri = new URI("http", null, "localhost", 80, "/test", "param=test", null);
    final URIBuilder uribuilder = new URIBuilder(uri).setParameters((NameValuePair[]) null);
    final URI result = uribuilder.build();
    Assertions.assertEquals(new URI("http://localhost:80/test"), result);
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 18 with NameValuePair

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

the class TestURIBuilder method testSchemeSpecificPartNameValuePairListByRFC6068Sample.

/**
 * Common use case: mailto: scheme. See https://tools.ietf.org/html/rfc6068#section-2
 */
@Test
public void testSchemeSpecificPartNameValuePairListByRFC6068Sample() throws Exception {
    final List<NameValuePair> parameters = new ArrayList<>();
    parameters.add(new BasicNameValuePair("subject", "mail subject"));
    final URIBuilder uribuilder = new URIBuilder().setScheme("mailto").setSchemeSpecificPart("my@email.server", parameters);
    final String result = uribuilder.build().toString();
    Assertions.assertTrue(result.contains("my@email.server"), "mail address as scheme specific part expected");
    Assertions.assertTrue(result.contains("mail%20subject"), "correct parameter encoding expected for that scheme");
}
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) Test(org.junit.jupiter.api.Test)

Example 19 with NameValuePair

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

the class BasicRequestBuilder method build.

@Override
public BasicHttpRequest build() {
    String path = getPath();
    final List<NameValuePair> parameters = getParameters();
    if (parameters != null && !parameters.isEmpty()) {
        try {
            final URI uri = new URIBuilder(path).setCharset(getCharset()).addParameters(parameters).build();
            path = uri.toASCIIString();
        } catch (final URISyntaxException ex) {
        // should never happen
        }
    }
    final BasicHttpRequest result = new BasicHttpRequest(getMethod(), getScheme(), getAuthority(), path);
    result.setVersion(getVersion());
    result.setHeaders(getHeaders());
    result.setAbsoluteRequestUri(isAbsoluteRequestUri());
    return result;
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) URIBuilder(org.apache.hc.core5.net.URIBuilder)

Example 20 with NameValuePair

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

the class URLEncodedUtils method format.

/**
 * Returns a String that is suitable for use as an {@code application/x-www-form-urlencoded}
 * list of parameters in an HTTP PUT or HTTP POST.
 *
 * @param parameters  The parameters to include.
 * @param parameterSeparator The parameter separator, by convention, {@code '&'} or {@code ';'}.
 * @param charset The encoding to use.
 * @return An {@code application/x-www-form-urlencoded} string
 *
 * @since 4.3
 */
public static String format(final Iterable<? extends NameValuePair> parameters, final char parameterSeparator, final Charset charset) {
    Args.notNull(parameters, "Parameters");
    final StringBuilder buf = new StringBuilder();
    int i = 0;
    for (final NameValuePair parameter : parameters) {
        if (i > 0) {
            buf.append(parameterSeparator);
        }
        PercentCodec.encode(buf, parameter.getName(), charset, URL_ENCODER, true);
        if (parameter.getValue() != null) {
            buf.append('=');
            PercentCodec.encode(buf, parameter.getValue(), charset, URL_ENCODER, true);
        }
        i++;
    }
    return buf.toString();
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair)

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