use of org.apache.hc.core5.http.copied.NameValuePair in project httpcomponents-core by apache.
the class TestNameValuePair method testNullValueNotEqual.
@Test
public void testNullValueNotEqual() throws Exception {
final NameValuePair NameValuePair = new BasicNameValuePair("name", null);
final NameValuePair NameValuePair2 = new BasicNameValuePair("name", "value");
Assertions.assertNotEquals(NameValuePair, NameValuePair2);
}
use of org.apache.hc.core5.http.copied.NameValuePair in project httpcomponents-core by apache.
the class TestNameValuePair method testConstructor.
@Test
public void testConstructor() {
final NameValuePair param = new BasicNameValuePair("name", "value");
Assertions.assertEquals("name", param.getName());
Assertions.assertEquals("value", param.getValue());
}
use of org.apache.hc.core5.http.copied.NameValuePair in project httpcomponents-core by apache.
the class TestingFrameworkRequestHandler method handle.
/**
* <p>Checks the HTTP request against the requestExpectations that it was previously given.
* If there is a mismatch, an exception will be saved in the "thrown" member.</p>
*
* <p>Also, a response will be returned that matches the desiredResponse.</p>
*/
@Override
public void handle(final ClassicHttpRequest request, final ClassicHttpResponse response, final HttpContext context) throws HttpException, IOException {
try {
/*
* Check the method against the method in the requestExpectations.
*/
final String actualMethod = request.getMethod();
final String expectedMethod = (String) requestExpectations.get(METHOD);
if (!actualMethod.equals(expectedMethod)) {
throw new TestingFrameworkException("Method not expected. " + " expected=" + expectedMethod + "; actual=" + actualMethod);
}
/*
* Set the status to the status that is in the desiredResponse.
*/
final Object desiredStatus = desiredResponse.get(STATUS);
if (desiredStatus != null) {
response.setCode((int) desiredStatus);
}
/*
* Check the query parameters against the parameters in requestExpectations.
*/
@SuppressWarnings("unchecked") final Map<String, String> expectedQuery = (Map<String, String>) requestExpectations.get(QUERY);
if (expectedQuery != null) {
final URI uri = request.getUri();
final URIBuilder uriBuilder = new URIBuilder(uri, StandardCharsets.UTF_8);
final List<NameValuePair> actualParams = uriBuilder.getQueryParams();
final Map<String, String> actualParamsMap = new HashMap<>();
for (final NameValuePair actualParam : actualParams) {
actualParamsMap.put(actualParam.getName(), actualParam.getValue());
}
for (final Map.Entry<String, String> expectedParam : expectedQuery.entrySet()) {
final String key = expectedParam.getKey();
if (!actualParamsMap.containsKey(key)) {
throw new TestingFrameworkException("Expected parameter not found: " + key);
}
final String actualParamValue = actualParamsMap.get(key);
final String expectedParamValue = expectedParam.getValue();
if (!actualParamValue.equals(expectedParamValue)) {
throw new TestingFrameworkException("Expected parameter value not found. " + " Parameter=" + key + "; expected=" + expectedParamValue + "; actual=" + actualParamValue);
}
}
}
/*
* Check the headers against the headers in requestExpectations.
*/
@SuppressWarnings("unchecked") final Map<String, String> expectedHeaders = (Map<String, String>) requestExpectations.get(HEADERS);
if (expectedHeaders != null) {
final Map<String, String> actualHeadersMap = new HashMap<>();
final Header[] actualHeaders = request.getHeaders();
for (final Header header : actualHeaders) {
actualHeadersMap.put(header.getName(), header.getValue());
}
for (final Entry<String, String> expectedHeader : expectedHeaders.entrySet()) {
final String key = expectedHeader.getKey();
if (!actualHeadersMap.containsKey(key)) {
throw new TestingFrameworkException("Expected header not found: " + key);
}
final String actualHeaderValue = actualHeadersMap.get(key);
final String expectedHeaderValue = expectedHeader.getValue();
if (!actualHeaderValue.equals(expectedHeaderValue)) {
throw new TestingFrameworkException("Expected header value not found. " + " Name=" + key + "; expected=" + expectedHeaderValue + "; actual=" + actualHeaderValue);
}
}
}
/*
* Check the body.
*/
final String expectedBody = (String) requestExpectations.get(BODY);
if (expectedBody != null) {
final HttpEntity entity = request.getEntity();
final String data = EntityUtils.toString(entity);
if (!data.equals(expectedBody)) {
throw new TestingFrameworkException("Expected body not found. " + " Body=" + data + "; expected=" + expectedBody);
}
}
/*
* Check the contentType of the request.
*/
final String requestContentType = (String) requestExpectations.get(CONTENT_TYPE);
if (requestContentType != null) {
final HttpEntity entity = request.getEntity();
final String contentType = entity.getContentType();
final String expectedContentType = (String) requestExpectations.get(CONTENT_TYPE);
if (!contentType.equals(expectedContentType)) {
throw new TestingFrameworkException("Expected request content type not found. " + " Content Type=" + contentType + "; expected=" + expectedContentType);
}
}
/*
* Check the protocolVersion.
*/
if (requestExpectations.containsKey(PROTOCOL_VERSION)) {
final ProtocolVersion protocolVersion = request.getVersion();
final ProtocolVersion expectedProtocolVersion = (ProtocolVersion) requestExpectations.get(PROTOCOL_VERSION);
if (!protocolVersion.equals(expectedProtocolVersion)) {
throw new TestingFrameworkException("Expected request protocol version not found. " + " Protocol Version=" + protocolVersion + "; expected=" + expectedProtocolVersion);
}
}
/*
* Return the body in desiredResponse using the contentType in desiredResponse.
*/
final String desiredBody = (String) desiredResponse.get(BODY);
if (desiredBody != null) {
final String desiredContentType = (String) desiredResponse.get(CONTENT_TYPE);
final StringEntity entity = desiredContentType != null ? new StringEntity(desiredBody, ContentType.parse(desiredContentType)) : new StringEntity(desiredBody);
response.setEntity(entity);
}
/*
* Return the headers in desiredResponse.
*/
@SuppressWarnings("unchecked") final Map<String, String> desiredHeaders = (Map<String, String>) desiredResponse.get(HEADERS);
if (desiredHeaders != null) {
for (final Entry<String, String> entry : desiredHeaders.entrySet()) {
response.setHeader(entry.getKey(), entry.getValue());
}
}
} catch (final Throwable t) {
/*
* Save the throwable to be later retrieved by a call to assertNothingThrown().
*/
thrown = t;
}
}
use of org.apache.hc.core5.http.copied.NameValuePair in project httpcomponents-core by apache.
the class TestBasicHeaderValueFormatter method testNVPFormatting.
@Test
public void testNVPFormatting() 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", "quote marks (\") must be escaped");
final NameValuePair param5 = new BasicNameValuePair("param", "back slash (\\) must be escaped too");
final NameValuePair param6 = new BasicNameValuePair("param", "values with\tblanks must always be quoted");
final NameValuePair param7 = new BasicNameValuePair("param", null);
final CharArrayBuffer buf = new CharArrayBuffer(64);
buf.clear();
this.formatter.formatNameValuePair(buf, param1, false);
Assertions.assertEquals("param=regular_stuff", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param2, false);
Assertions.assertEquals("param=\"this\\\\that\"", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param3, false);
Assertions.assertEquals("param=\"this,that\"", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param4, false);
Assertions.assertEquals("param=\"quote marks (\\\") must be escaped\"", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param5, false);
Assertions.assertEquals("param=\"back slash (\\\\) must be escaped too\"", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param6, false);
Assertions.assertEquals("param=\"values with\tblanks must always be quoted\"", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param7, false);
Assertions.assertEquals("param", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param1, true);
Assertions.assertEquals("param=\"regular_stuff\"", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param2, true);
Assertions.assertEquals("param=\"this\\\\that\"", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param3, true);
Assertions.assertEquals("param=\"this,that\"", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param4, true);
Assertions.assertEquals("param=\"quote marks (\\\") must be escaped\"", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param5, true);
Assertions.assertEquals("param=\"back slash (\\\\) must be escaped too\"", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param6, true);
Assertions.assertEquals("param=\"values with\tblanks must always be quoted\"", buf.toString());
buf.clear();
this.formatter.formatNameValuePair(buf, param7, true);
Assertions.assertEquals("param", buf.toString());
}
use of org.apache.hc.core5.http.copied.NameValuePair 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));
}
Aggregations