public Map execute()

in httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java [56:164]


    public Map<String, Object> execute(final String defaultURI, final Map<String, Object> request) throws Exception {
        // check the request for missing items.
        if (defaultURI == null) {
            throw new HttpException("defaultURL cannot be null");
        }
        if (request == null) {
            throw new HttpException("request cannot be null");
        }
        if (! request.containsKey(PATH)) {
            throw new HttpException("Request path should be set.");
        }
        if (! request.containsKey(METHOD)) {
            throw new HttpException("Request method should be set.");
        }

        final Timeout timeout;
        if (request.containsKey(TIMEOUT)) {
            timeout = Timeout.ofMilliseconds((long) request.get(TIMEOUT));
        } else {
            timeout = null;
        }
        final ClassicTestClient client = new ClassicTestClient(SocketConfig.custom()
                .setSoTimeout(timeout)
                .build());

        // Append the path to the defaultURI.
        String tempDefaultURI = defaultURI;
        if (! defaultURI.endsWith("/")) {
            tempDefaultURI += "/";
        }
        final URI startingURI = new URI(tempDefaultURI + request.get(PATH));
        final URI uri;

        // append each parameter in the query to the uri.
        @SuppressWarnings("unchecked")
        final Map<String, String> queryMap = (Map<String, String>) request.get(QUERY);
        if (queryMap != null) {
            final String existingQuery = startingURI.getRawQuery();
            final StringBuilder newQuery = new StringBuilder(existingQuery == null ? "" : existingQuery);

            // append each parm to the query
            for (final Entry<String, String> parm : queryMap.entrySet()) {
                newQuery.append("&").append(parm.getKey()).append("=").append(parm.getValue());
            }
            // create a uri with the new query.
            uri = new URI(
                    startingURI.getRawSchemeSpecificPart(),
                    startingURI.getRawUserInfo(),
                    startingURI.getHost(),
                    startingURI.getPort(),
                    startingURI.getRawPath(),
                    newQuery.toString(),
                    startingURI.getRawFragment());
        } else {
            uri = startingURI;
        }

        final BasicClassicHttpRequest httpRequest = new BasicClassicHttpRequest(request.get(METHOD).toString(), uri);

        if (request.containsKey(PROTOCOL_VERSION)) {
            httpRequest.setVersion((ProtocolVersion) request.get(PROTOCOL_VERSION));
        }

        // call addHeader for each header in headers.
        @SuppressWarnings("unchecked")
        final Map<String, String> headersMap = (Map<String, String>) request.get(HEADERS);
        if (headersMap != null) {
            for (final Entry<String, String> header : headersMap.entrySet()) {
                httpRequest.addHeader(header.getKey(), header.getValue());
            }
        }

        // call setEntity if a body is specified.
        final String requestBody = (String) request.get(BODY);
        if (requestBody != null) {
            final String requestContentType = (String) request.get(CONTENT_TYPE);
            final StringEntity entity = requestContentType != null ?
                                          new StringEntity(requestBody, ContentType.parse(requestContentType)) :
                                          new StringEntity(requestBody);
            httpRequest.setEntity(entity);
        }

        client.start();

        // Now start the request.
        final HttpHost host = new HttpHost(uri.getHost(), uri.getPort());
        final HttpCoreContext context = HttpCoreContext.create();
        try (final ClassicHttpResponse response = client.execute(host, httpRequest, context)) {
            // Prepare the response.  It will contain status, body, headers, and contentType.
            final HttpEntity entity = response.getEntity();
            final String body = entity == null ? null : EntityUtils.toString(entity);
            final String contentType = entity == null ? null : entity.getContentType();

            // prepare the returned information
            final Map<String, Object> ret = new HashMap<>();
            ret.put(STATUS, response.getCode());

            // convert the headers to a Map
            final Map<String, Object> headerMap = new HashMap<>();
            for (final Header header : response.getHeaders()) {
                headerMap.put(header.getName(), header.getValue());
            }
            ret.put(HEADERS, headerMap);
            ret.put(BODY, body);
            ret.put(CONTENT_TYPE, contentType);

            return ret ;
        }
    }