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());
}
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);
}
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");
}
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;
}
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();
}
Aggregations