Search in sources :

Example 51 with NameValuePair

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);
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) Test(org.junit.jupiter.api.Test)

Example 52 with NameValuePair

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());
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) Test(org.junit.jupiter.api.Test)

Example 53 with NameValuePair

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;
    }
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) HttpEntity(org.apache.hc.core5.http.HttpEntity) HashMap(java.util.HashMap) ProtocolVersion(org.apache.hc.core5.http.ProtocolVersion) URI(java.net.URI) URIBuilder(org.apache.hc.core5.net.URIBuilder) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) Header(org.apache.hc.core5.http.Header) HashMap(java.util.HashMap) Map(java.util.Map)

Example 54 with NameValuePair

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());
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 55 with NameValuePair

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));
}
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)

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