From 73cb98c7271e2acd43d3c81ab66de90caecff390 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sat, 3 Oct 2026 15:42:45 +0200 Subject: [PATCH] Removed testing framework unused and unmaintained since its contribution in 2016 (binary compatibility breakage) --- .../framework/ClassicTestClientAdapter.java | 173 --- .../ClassicTestClientTestingAdapter.java | 35 - .../testing/framework/ClientPOJOAdapter.java | 155 --- .../framework/ClientTestingAdapter.java | 151 --- .../testing/framework/FrameworkTest.java | 158 --- .../testing/framework/TestingFramework.java | 461 ------- .../framework/TestingFrameworkException.java | 81 -- .../TestingFrameworkRequestHandler.java | 254 ---- .../TestClassicTestClientAdapter.java | 186 --- .../TestClassicTestClientTestingAdapter.java | 263 ---- .../framework/TestClientPojoAdapter.java | 77 -- .../framework/TestClientTestingAdapter.java | 112 -- .../testing/framework/TestFrameworkTest.java | 110 -- .../framework/TestTestingFramework.java | 1180 ----------------- .../TestTestingFrameworkRequestHandler.java | 156 --- pom.xml | 2 + 16 files changed, 2 insertions(+), 3552 deletions(-) delete mode 100644 httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java delete mode 100644 httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientTestingAdapter.java delete mode 100644 httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClientPOJOAdapter.java delete mode 100644 httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClientTestingAdapter.java delete mode 100644 httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/FrameworkTest.java delete mode 100644 httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java delete mode 100644 httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkException.java delete mode 100644 httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java delete mode 100644 httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientAdapter.java delete mode 100644 httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientTestingAdapter.java delete mode 100644 httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClientPojoAdapter.java delete mode 100644 httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClientTestingAdapter.java delete mode 100644 httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestFrameworkTest.java delete mode 100644 httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestTestingFramework.java delete mode 100644 httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestTestingFrameworkRequestHandler.java diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java deleted file mode 100644 index 8695f3e627..0000000000 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package org.apache.hc.core5.testing.framework; - -import java.net.URI; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; - -import org.apache.hc.core5.http.ClassicHttpResponse; -import org.apache.hc.core5.http.ContentType; -import org.apache.hc.core5.http.Header; -import org.apache.hc.core5.http.HttpEntity; -import org.apache.hc.core5.http.HttpException; -import org.apache.hc.core5.http.HttpHost; -import org.apache.hc.core5.http.ProtocolVersion; -import org.apache.hc.core5.http.io.SocketConfig; -import org.apache.hc.core5.http.io.entity.EntityUtils; -import org.apache.hc.core5.http.io.entity.StringEntity; -import org.apache.hc.core5.http.message.BasicClassicHttpRequest; -import org.apache.hc.core5.http.protocol.HttpCoreContext; -import org.apache.hc.core5.testing.classic.ClassicTestClient; -import org.apache.hc.core5.util.Timeout; - -public class ClassicTestClientAdapter extends ClientPOJOAdapter { - - /** - * {@inheritDoc} - */ - @Override - public Map execute(final String defaultURI, final Map 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 queryMap = (Map) 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 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 headersMap = (Map) request.get(HEADERS); - if (headersMap != null) { - for (final Entry 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, Integer.MAX_VALUE); - final String contentType = entity == null ? null : entity.getContentType(); - - // prepare the returned information - final Map ret = new HashMap<>(); - ret.put(STATUS, response.getCode()); - - // convert the headers to a Map - final Map 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 ; - } - } - - /** - * {@inheritDoc} - */ - @Override - public String getClientName() { - return "ClassicTestClient"; - } -} diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientTestingAdapter.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientTestingAdapter.java deleted file mode 100644 index e320343215..0000000000 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientTestingAdapter.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package org.apache.hc.core5.testing.framework; - -public class ClassicTestClientTestingAdapter extends ClientTestingAdapter { - - public ClassicTestClientTestingAdapter() { - super(new ClassicTestClientAdapter()); - } -} diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClientPOJOAdapter.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClientPOJOAdapter.java deleted file mode 100644 index 946ba6c1cf..0000000000 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClientPOJOAdapter.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ -package org.apache.hc.core5.testing.framework; - -import java.util.Map; - -/** - * - *

This adapter expects a request to be made up of POJOs such as Maps and Lists. In Groovy - * the request could be expressed like this:

- * - *
- *
- * def request = [
- *                   path    : "a/path",
- *                   method  : "GET",
- *                   query   : [
- *                                parm1 : "1",
- *                                parm2 : "2",
- *                             ]
- *                   headers : [
- *                                header1 : "stuff",
- *                                header2 : "more_stuff",
- *                             ]
- *                   contentType : "application/json",
- *                   body        : '{"location" : "home" }',
- *                ]
- * 
- * - *

The adapter will translate this request into calls specific to a particular HTTP client.

- * - *

The response is then returned with POJOs with this structure:

- * - *
- *
- * def response = [
- *                    status      : 200,
- *                    headers     : [
- *                                      header1 : "response_stuff",
- *                                  ]
- *                    contentType : "application/json",
- *                    body        : '{"location" : "work" }',
- *                ]
- * 
- * @since 5.0 - */ -public abstract class ClientPOJOAdapter { - public static final String BODY = "body"; - public static final String CONTENT_TYPE = "contentType"; - public static final String HEADERS = "headers"; - public static final String METHOD = "method"; - public static final String NAME = "name"; - public static final String PATH = "path"; - public static final String PROTOCOL_VERSION = "protocolVersion"; - public static final String QUERY = "query"; - public static final String REQUEST = "request"; - public static final String RESPONSE = "response"; - public static final String STATUS = "status"; - public static final String TIMEOUT = "timeout"; - - /** - * Name of the HTTP Client that this adapter uses. - * - * @return name of the HTTP Client. - */ - public abstract String getClientName(); - - /** - * Execute an HTTP request. - * - * @param defaultURI the URI used by default. The path in the request is - * usually appended to it. - * @param request the request as specified above. - * - * @return the response to the request as specified above. - * - * @throws Exception in case of a problem - */ - public abstract Map execute(String defaultURI, Map request) throws Exception; - - /** - *

Check if a request is supported.

- * - *

Usually called directly by a testing framework. If an HTTP client does not support - * a particular request, a non-null reason should be returned. Otherwise, if - * the request is supported, return null.

- * - *

If this method is overridden, then the execute method should probably call - * assertRequestSupported() at the beginning.

- * - * @param request the request as specified above. - * - * @return null if the request is supported; Otherwise, return a reason. - */ - public String checkRequestSupport(final Map request) { - return null; - } - - /** - *

Assert that the request is supported

- * - *

Usually called by the execute method. Throws an exception if the request - * is not supported.

- * - * @param request the request as specified above. - * @throws Exception if the request is not supported. - */ - public void assertRequestSupported(final Map request) throws Exception { - final String reason = checkRequestSupport(request); - if (reason != null) { - throw new Exception(reason); - } - } - - /** - *

Modify the request.

- * - *

In a testing context, a testing framework can call this method to allow - * the adapter to change the request. The request is then given to a - * special request handler of the in-process HttpServer which will later check - * an actual HTTP request against what is expected.

- * - *

In a production context, this is called by the execute method (if at all).

- * - * @param request the request as specified above. - * @return the same request or a modification of it. - */ - public Map modifyRequest(final Map request) { - return request; - } -} diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClientTestingAdapter.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClientTestingAdapter.java deleted file mode 100644 index 3d3067165a..0000000000 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClientTestingAdapter.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package org.apache.hc.core5.testing.framework; - -import java.util.Map; - -public class ClientTestingAdapter { - /** - * This adapter will perform the HTTP request and return the response in the - * expected format. - */ - protected ClientPOJOAdapter adapter; - - /* - * The following is not expected to be changed to true, but it is to highlight - * where the execute method can call the requestHandler's assertNothingThrown() - * method if desired. Since this adapter's execute method does not check - * the response, there is no need to call it. - */ - protected boolean callAssertNothingThrown; - - public ClientTestingAdapter() { - } - - public ClientTestingAdapter(final ClientPOJOAdapter adapter) { - this.adapter = adapter; - } - - /** - * See the documentation for the same method in {@link ClientPOJOAdapter}. This - * method will typically call it. However, this method also has access to the - * test's response expectations if that is needed for some reason. Furthermore, - * this method also has access to the {@link TestingFrameworkRequestHandler} so - * it can optionally call assertNothingThrown() before checking the response - * further. It is optional because the test framework will call it later. - * - * @param defaultURI See execute method of {@link ClientPOJOAdapter}. - * @param request See execute method of {@link ClientPOJOAdapter}. - * @param requestHandler The request handler that checks the received HTTP request - * with the request that was intended. If there is a - * mismatch of expectations, then the requestHandler will - * throw an exception. If this execute method does not want - * to make further checks of the response in the case - * the responseHandler threw, then the assertNothingThrown() - * method should be called before doing further checks. - * @param responseExpectations The response expectations of the test. - * @return See return of the execute method of {@link ClientPOJOAdapter}. - * @throws TestingFrameworkException in the case of a problem. - */ - public Map execute(final String defaultURI, final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - - try { - if (adapter == null) { - throw new TestingFrameworkException("adapter cannot be null"); - } - // Call the adapter's execute method to actually make the HTTP request. - final Map response = adapter.execute(defaultURI, request); - - /* - * Adapters may call assertNothingThrown() if they would like. This would be to - * make sure the following code is not executed in the event there was something - * thrown in the request handler. - * - * Otherwise, the framework will call it when this method returns. So, it is - * optional. - */ - if (callAssertNothingThrown) { - if (requestHandler == null) { - throw new TestingFrameworkException("requestHandler cannot be null"); - } - requestHandler.assertNothingThrown(); - } - - return response; - } catch (final TestingFrameworkException e) { - throw e; - } catch (final Exception ex) { - throw new TestingFrameworkException(ex); - } - } - - /** - * See the documentation for the same method in {@link ClientPOJOAdapter}. - */ - public boolean isRequestSupported(final Map request) { - return adapter == null || adapter.checkRequestSupport(request) == null; - } - - /** - * See the documentation for the same method in {@link ClientPOJOAdapter}. - */ - public Map modifyRequest(final Map request) { - return adapter == null ? request : adapter.modifyRequest(request); - } - - /** - * Generally a test's response expectations should not need to be modified, but - * if a particular HTTP client (such as Groovy's RESTClient which uses HttpClient) - * needs to modify the response expectations, it should do so here. After this - * method returns, the {@link TestingFrameworkRequestHandler} is sent the - * expectations so the request handler will return a response that matches the - * expectations. When the HTTP response is obtained, the received response - * is matched against the expectations. - * - * @param request for the format, see the documentation for {@link ClientPOJOAdapter}. - * @param responseExpectations for the format, see the documentation for {@link ClientPOJOAdapter}. - * @return the same or modified response expectations. - */ - public Map modifyResponseExpectations(final Map request, - final Map responseExpectations) { - return responseExpectations; - } - - /** - * Getter for the {@link ClientPOJOAdapter} that is actually used to make the - * HTTP request. - * - * @return the {@link ClientPOJOAdapter}. - */ - public ClientPOJOAdapter getClientPOJOAdapter() { - return adapter; - } - -} diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/FrameworkTest.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/FrameworkTest.java deleted file mode 100644 index 9685487939..0000000000 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/FrameworkTest.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package org.apache.hc.core5.testing.framework; - -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.BODY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.CONTENT_TYPE; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.HEADERS; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.METHOD; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PATH; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PROTOCOL_VERSION; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.QUERY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.REQUEST; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.RESPONSE; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.STATUS; - -import java.net.URI; -import java.net.URISyntaxException; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.hc.core5.http.NameValuePair; -import org.apache.hc.core5.net.URIBuilder; - -public class FrameworkTest { - - private Map request = new HashMap<>(); - private Map response = new HashMap<>(); - - /** - * Constructs a test with default values. - */ - public FrameworkTest() { - this(null); - } - - /** - * Constructs a test with values that are passed in as well as defaults - * for values that are not passed in. - * - * @param test Contains a REQUEST and an expected RESPONSE. - * See {@link ClientPOJOAdapter} for details. - */ - @SuppressWarnings("unchecked") - public FrameworkTest(final Map test) { - if (test != null) { - if (test.containsKey(REQUEST)) { - request = (Map) test.get(REQUEST); - } - if (test.containsKey(RESPONSE)) { - response = (Map) test.get(RESPONSE); - } - } - } - - /** - * Returns a request with defaults for any parameter that is not specified. - * - * @return a REQUEST map. - * @throws TestingFrameworkException a problem such as an invalid URL - */ - public Map initRequest() throws TestingFrameworkException { - // initialize to some helpful defaults - final Map ret = new HashMap<>(); - ret.put(PATH, TestingFramework.DEFAULT_REQUEST_PATH); - ret.put(BODY, TestingFramework.DEFAULT_REQUEST_BODY); - ret.put(CONTENT_TYPE, TestingFramework.DEFAULT_REQUEST_CONTENT_TYPE); - ret.put(QUERY, new HashMap<>(TestingFramework.DEFAULT_REQUEST_QUERY)); - ret.put(HEADERS, new HashMap<>(TestingFramework.DEFAULT_REQUEST_HEADERS)); - ret.put(PROTOCOL_VERSION, TestingFramework.DEFAULT_REQUEST_PROTOCOL_VERSION); - - // GET is the default method. - if (! request.containsKey(METHOD)) { - request.put(METHOD, "GET"); - } - ret.putAll(request); - - moveAnyParametersInPathToQuery(ret); - - return ret; - } - - private void moveAnyParametersInPathToQuery(final Map request) throws TestingFrameworkException { - try { - final String path = (String) request.get(PATH); - if (path != null) { - final URI uri = path.startsWith("/") ? new URI("http://localhost:8080" + path) : - new URI("http://localhost:8080/"); - final URIBuilder uriBuilder = new URIBuilder(uri, StandardCharsets.UTF_8); - final List params = uriBuilder.getQueryParams(); - @SuppressWarnings("unchecked") - final Map queryMap = (Map) request.get(QUERY); - for (final NameValuePair param : params) { - queryMap.put(param.getName(), param.getValue()); - } - if (! params.isEmpty()) { - request.put(PATH, uri.getPath()); - } - } - } catch (final URISyntaxException e) { - throw new TestingFrameworkException(e); - } - } - - /** - * Returns an expected response with defaults for any parameter that is not specified. - * - * @return the RESPONSE map. - */ - public Map initResponseExpectations() { - // 200 is the default status. - if (! response.containsKey(STATUS)) { - response.put(STATUS, 200); - } - - final Map responseExpectations = new HashMap<>(); - // initialize to some helpful defaults - responseExpectations.put(BODY, TestingFramework.DEFAULT_RESPONSE_BODY); - responseExpectations.put(CONTENT_TYPE, TestingFramework.DEFAULT_RESPONSE_CONTENT_TYPE); - responseExpectations.put(HEADERS, new HashMap<>(TestingFramework.DEFAULT_RESPONSE_HEADERS)); - - // Now override any defaults with what is requested. - responseExpectations.putAll(response); - - return responseExpectations; - } - - @Override - public String toString() { - return "request: " + request + "\nresponse: " + response; - } -} diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java deleted file mode 100644 index 74da026eeb..0000000000 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java +++ /dev/null @@ -1,461 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package org.apache.hc.core5.testing.framework; - -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.BODY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.CONTENT_TYPE; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.HEADERS; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.METHOD; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.REQUEST; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.RESPONSE; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.STATUS; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.net.InetAddress; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -import org.apache.hc.core5.http.HttpVersion; -import org.apache.hc.core5.http.ProtocolVersion; -import org.apache.hc.core5.http.impl.bootstrap.HttpServer; -import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap; -import org.apache.hc.core5.http.impl.routing.RequestRouter; -import org.apache.hc.core5.http.io.HttpRequestHandler; -import org.apache.hc.core5.http.io.SocketConfig; -import org.apache.hc.core5.io.CloseMode; - -public class TestingFramework { - - /** - * Use the ALL_METHODS list to conveniently cycle through all HTTP methods. - */ - public static final List ALL_METHODS = Arrays.asList("HEAD", "GET", "DELETE", "POST", "PUT", "PATCH"); - - /** - * If an {@link ClassicTestClientTestingAdapter} is unable to return a response in - * the format this testing framework is needing, then it will need to check the - * item in the response (such as body, status, headers, or contentType) itself and set - * the returned value of the item as ALREADY_CHECKED. - */ - public static final Object ALREADY_CHECKED = new Object(); - - /** - * If a test does not specify a path, this one is used. - */ - public static final String DEFAULT_REQUEST_PATH = "a/path"; - - /** - * If a test does not specify a body, this one is used. - */ - public static final String DEFAULT_REQUEST_BODY = "{\"location\":\"home\"}"; - - /** - * If a test does not specify a request contentType, this one is used. - */ - public static final String DEFAULT_REQUEST_CONTENT_TYPE = "application/json"; - - /** - * If a test does not specify query parameters, these are used. - */ - public static final Map DEFAULT_REQUEST_QUERY; - - /** - * If a test does not specify a request headers, these are used. - */ - public static final Map DEFAULT_REQUEST_HEADERS; - - /** - * If a test does not specify a protocol version, this one is used. - */ - public static final ProtocolVersion DEFAULT_REQUEST_PROTOCOL_VERSION = HttpVersion.HTTP_1_1; - - /** - * If a test does not specify an expected response status, this one is used. - */ - public static final int DEFAULT_RESPONSE_STATUS = 200; - - /** - * If a test does not specify an expected response body, this one is used. - */ - public static final String DEFAULT_RESPONSE_BODY = "{\"location\":\"work\"}"; - - /** - * If a test does not specify an expected response contentType, this one is used. - */ - public static final String DEFAULT_RESPONSE_CONTENT_TYPE = "application/json"; - - /** - * If a test does not specify expected response headers, these are used. - */ - public static final Map DEFAULT_RESPONSE_HEADERS; - - static { - final Map request = new HashMap<>(); - request.put("p1", "this"); - request.put("p2", "that"); - DEFAULT_REQUEST_QUERY = Collections.unmodifiableMap(request); - - Map headers = new HashMap<>(); - headers.put("header1", "stuff"); - headers.put("header2", "more stuff"); - DEFAULT_REQUEST_HEADERS = Collections.unmodifiableMap(headers); - - headers = new HashMap<>(); - headers.put("header3", "header_three"); - headers.put("header4", "header_four"); - DEFAULT_RESPONSE_HEADERS = Collections.unmodifiableMap(headers); - } - - private ClientTestingAdapter adapter; - private TestingFrameworkRequestHandler requestHandler = new TestingFrameworkRequestHandler(); - private List tests = new ArrayList<>(); - - private HttpServer server; - private int port; - - public TestingFramework() throws TestingFrameworkException { - this(null); - } - - public TestingFramework(final ClientTestingAdapter adapter) throws TestingFrameworkException { - this.adapter = adapter; - - /* - * By default, a set of tests that will exercise each HTTP method are pre-loaded. - */ - for (final String method : ALL_METHODS) { - final List statusList = Arrays.asList(200, 201); - for (final Integer status : statusList) { - final Map request = new HashMap<>(); - request.put(METHOD, method); - - final Map response = new HashMap<>(); - response.put(STATUS, status); - - final Map test = new HashMap<>(); - test.put(REQUEST, request); - test.put(RESPONSE, response); - - addTest(test); - } - } - } - - /** - * This is not likely to be used except during the testing of this class. - * It is used to inject a mocked request handler. - * - * @param requestHandler - */ - public void setRequestHandler(final TestingFrameworkRequestHandler requestHandler) { - this.requestHandler = requestHandler; - } - - /** - * Run the tests that have been previously added. First, an in-process {@link HttpServer} is - * started. Then, all the tests are completed by passing each test to the adapter - * which will make the HTTP request. - * - * @throws TestingFrameworkException if there is a test failure or unexpected problem. - */ - public void runTests() throws TestingFrameworkException { - if (adapter == null) { - throw new TestingFrameworkException("adapter should not be null"); - } - - startServer(); - - try { - for (final FrameworkTest test : tests) { - try { - callAdapter(test); - } catch (final Throwable t) { - processThrowable(t, test); - } - } - } finally { - stopServer(); - } - } - - private void processThrowable(final Throwable t, final FrameworkTest test) throws TestingFrameworkException { - final TestingFrameworkException e; - if (t instanceof TestingFrameworkException) { - e = (TestingFrameworkException) t; - } else { - e = new TestingFrameworkException(t); - } - e.setAdapter(adapter); - e.setTest(test); - throw e; - } - - private void startServer() throws TestingFrameworkException { - /* - * Start an in-process server and handle all HTTP requests - * with the requestHandler. - */ - final SocketConfig socketConfig = SocketConfig.custom() - .setSoTimeout(15000, TimeUnit.MILLISECONDS) - .build(); - - final ServerBootstrap serverBootstrap = ServerBootstrap.bootstrap() - .setLocalAddress(InetAddress.getLoopbackAddress()) - .setSocketConfig(socketConfig) - .setRequestRouter(RequestRouter.builder() - .addRoute(RequestRouter.LOCAL_AUTHORITY, "*", requestHandler) - .resolveAuthority(RequestRouter.LOCAL_AUTHORITY_RESOLVER) - .build()); - - server = serverBootstrap.create(); - try { - server.start(); - } catch (final IOException e) { - throw new TestingFrameworkException(e); - } - - port = server.getLocalPort(); - } - - private void stopServer() { - final HttpServer local = this.server; - this.server = null; - if (local != null) { - local.close(CloseMode.IMMEDIATE); - } - } - - private void callAdapter(final FrameworkTest test) throws TestingFrameworkException { - Map request = test.initRequest(); - - /* - * If the adapter does not support the particular request, skip the test. - */ - if (! adapter.isRequestSupported(request)) { - return; - } - - /* - * Allow the adapter to modify the request before the request expectations - * are given to the requestHandler. Typically, adapters should not have - * to modify the request. - */ - request = adapter.modifyRequest(request); - - // Tell the request handler what to expect in the request. - requestHandler.setRequestExpectations(request); - - Map responseExpectations = test.initResponseExpectations(); - /* - * Allow the adapter to modify the response expectations before the handler - * is told what to return. Typically, adapters should not have to modify - * the response expectations. - */ - responseExpectations = adapter.modifyResponseExpectations(request, responseExpectations); - - // Tell the request handler what response to return. - requestHandler.setDesiredResponse(responseExpectations); - - /* - * Use the adapter to make the HTTP call. Make sure the responseExpectations are not changed - * since they have already been sent to the request handler and they will later be used - * to check the response. - */ - final String defaultURI = getDefaultURI(); - final Map response = adapter.execute( - defaultURI, - request, - requestHandler, - Collections.unmodifiableMap(responseExpectations)); - /* - * The adapter is welcome to call assertNothingThrown() earlier, but we will - * do it here to make sure it is done. If the handler threw any exception - * while checking the request it received, it will be re-thrown here. - */ - requestHandler.assertNothingThrown(); - - assertResponseMatchesExpectation(request.get(METHOD), response, responseExpectations); - } - - @SuppressWarnings("unchecked") - private void assertResponseMatchesExpectation(final Object method, final Map actualResponse, - final Map expectedResponse) - throws TestingFrameworkException { - if (actualResponse == null) { - throw new TestingFrameworkException("response should not be null"); - } - /* - * Now check the items in the response unless the adapter says they - * already checked something. - */ - if (actualResponse.get(STATUS) != TestingFramework.ALREADY_CHECKED) { - assertStatusMatchesExpectation(actualResponse.get(STATUS), expectedResponse.get(STATUS)); - } - if (! method.equals("HEAD")) { - if (actualResponse.get(BODY) != TestingFramework.ALREADY_CHECKED) { - assertBodyMatchesExpectation(actualResponse.get(BODY), expectedResponse.get(BODY)); - } - if (actualResponse.get(CONTENT_TYPE) != TestingFramework.ALREADY_CHECKED) { - assertContentTypeMatchesExpectation(actualResponse.get(CONTENT_TYPE), expectedResponse.get(CONTENT_TYPE)); - } - } - if (actualResponse.get(HEADERS) != TestingFramework.ALREADY_CHECKED) { - assertHeadersMatchExpectation((Map) actualResponse.get(HEADERS), - (Map) expectedResponse.get(HEADERS)); - } - } - - private void assertStatusMatchesExpectation(final Object actualStatus, final Object expectedStatus) - throws TestingFrameworkException { - if (actualStatus == null) { - throw new TestingFrameworkException("Returned status is null."); - } - if (expectedStatus != null && !actualStatus.equals(expectedStatus)) { - throw new TestingFrameworkException("Expected status not found. expected=" - + expectedStatus + "; actual=" + actualStatus); - } - } - - private void assertBodyMatchesExpectation(final Object actualBody, final Object expectedBody) - throws TestingFrameworkException { - if (actualBody == null) { - throw new TestingFrameworkException("Returned body is null."); - } - if (expectedBody != null && !actualBody.equals(expectedBody)) { - throw new TestingFrameworkException("Expected body not found. expected=" - + expectedBody + "; actual=" + actualBody); - } - } - - private void assertContentTypeMatchesExpectation(final Object actualContentType, final Object expectedContentType) - throws TestingFrameworkException { - if (expectedContentType != null) { - if (actualContentType == null) { - throw new TestingFrameworkException("Returned contentType is null."); - } - if (! actualContentType.equals(expectedContentType)) { - throw new TestingFrameworkException("Expected content type not found. expected=" - + expectedContentType + "; actual=" + actualContentType); - } - } - } - - private void assertHeadersMatchExpectation(final Map actualHeaders, - final Map expectedHeaders) - throws TestingFrameworkException { - if (expectedHeaders == null) { - return; - } - for (final Map.Entry expectedHeader : expectedHeaders.entrySet()) { - final String expectedHeaderName = expectedHeader.getKey(); - if (! actualHeaders.containsKey(expectedHeaderName)) { - throw new TestingFrameworkException("Expected header not found: name=" + expectedHeaderName); - } - if (! actualHeaders.get(expectedHeaderName).equals(expectedHeaders.get(expectedHeaderName))) { - throw new TestingFrameworkException("Header value not expected: name=" + expectedHeaderName - + "; expected=" + expectedHeaders.get(expectedHeaderName) - + "; actual=" + actualHeaders.get(expectedHeaderName)); - } - } - } - - private String getDefaultURI() { - return "http://localhost:" + port + "/"; - } - - /** - * Sets the {@link ClientTestingAdapter}. - * - * @param adapter - */ - public void setAdapter(final ClientTestingAdapter adapter) { - this.adapter = adapter; - } - - /** - * Deletes all tests. - */ - public void deleteTests() { - tests = new ArrayList<>(); - } - - /** - * Call to add a test with defaults. - * - * @throws TestingFrameworkException - */ - public void addTest() throws TestingFrameworkException { - addTest(null); - } - - /** - * Call to add a test. The test is a map with a REQUEST and a RESPONSE key. - * See {@link ClientPOJOAdapter} for details on the format of the request and response. - * - * @param test Map with a REQUEST and a RESPONSE key. - * @throws TestingFrameworkException - */ - @SuppressWarnings("unchecked") - public void addTest(final Map test) throws TestingFrameworkException { - final Map testCopy = (Map) deepcopy(test); - - tests.add(new FrameworkTest(testCopy)); - } - - /** - * Used to make a "deep" copy of an object. This testing framework makes deep copies - * of tests that are added as well as requestExpectations Maps and response Maps. - * - * @param orig a serializable object. - * @return a deep copy of the orig object. - * @throws TestingFrameworkException - */ - public static Object deepcopy(final Object orig) throws TestingFrameworkException { - try { - // this is from http://stackoverflow.com/questions/13155127/deep-copy-map-in-groovy - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - final ObjectOutputStream oos = new ObjectOutputStream(bos); - oos.writeObject(orig); - oos.flush(); - final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); - final ObjectInputStream ois = new ObjectInputStream(bin); - return ois.readObject(); - } catch (final ClassNotFoundException | IOException ex) { - throw new TestingFrameworkException(ex); - } - } -} diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkException.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkException.java deleted file mode 100644 index 584d7c35ad..0000000000 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkException.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package org.apache.hc.core5.testing.framework; - -public class TestingFrameworkException extends Exception { - public static final String NO_HTTP_CLIENT = "none"; - - private ClientTestingAdapter adapter; - - private FrameworkTest test; - - /** - * - */ - private static final long serialVersionUID = -1010516169283589675L; - - /** - * Creates a WebServerTestingFrameworkException with the specified detail message. - */ - public TestingFrameworkException(final String message) { - super(message); - } - - public TestingFrameworkException(final Throwable cause) { - super(cause); - } - - @Override - public String getMessage() { - String message = super.getMessage(); - if (adapter != null) { - final ClientPOJOAdapter pojoAdapter = adapter.getClientPOJOAdapter(); - final String tempHttpClient = pojoAdapter == null ? null : pojoAdapter.getClientName(); - final String httpClient = tempHttpClient == null ? NO_HTTP_CLIENT : tempHttpClient; - if (message == null) { - message = "null"; - } - message += "\nHTTP Client=" + httpClient; - } - if (test != null) { - if (message == null) { - message = "null"; - } - message += "\ntest:\n" + test; - } - return message; - } - - public void setAdapter(final ClientTestingAdapter adapter) { - this.adapter = adapter; - } - - public void setTest(final FrameworkTest test) { - this.test = test; - } -} diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java deleted file mode 100644 index 9a811e348f..0000000000 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java +++ /dev/null @@ -1,254 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package org.apache.hc.core5.testing.framework; - -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.BODY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.CONTENT_TYPE; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.HEADERS; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.METHOD; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PROTOCOL_VERSION; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.QUERY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.STATUS; - -import java.io.IOException; -import java.net.URI; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import org.apache.hc.core5.http.ClassicHttpRequest; -import org.apache.hc.core5.http.ClassicHttpResponse; -import org.apache.hc.core5.http.ContentType; -import org.apache.hc.core5.http.Header; -import org.apache.hc.core5.http.HttpEntity; -import org.apache.hc.core5.http.HttpException; -import org.apache.hc.core5.http.NameValuePair; -import org.apache.hc.core5.http.ProtocolVersion; -import org.apache.hc.core5.http.io.HttpRequestHandler; -import org.apache.hc.core5.http.io.entity.EntityUtils; -import org.apache.hc.core5.http.io.entity.StringEntity; -import org.apache.hc.core5.http.protocol.HttpContext; -import org.apache.hc.core5.net.URIBuilder; - -public class TestingFrameworkRequestHandler implements HttpRequestHandler { - protected Throwable thrown; - protected Map requestExpectations; - protected Map desiredResponse; - - /** - * Sets the request expectations. - * - * @param requestExpectations the expected values of the request. - * @throws TestingFrameworkException - */ - @SuppressWarnings("unchecked") - public void setRequestExpectations(final Map requestExpectations) throws TestingFrameworkException { - this.requestExpectations = (Map) TestingFramework.deepcopy(requestExpectations); - } - - /** - * Sets the desired response. The handler will return a response that matches this. - * - * @param desiredResponse the desired response. - * @throws TestingFrameworkException - */ - @SuppressWarnings("unchecked") - public void setDesiredResponse(final Map desiredResponse) throws TestingFrameworkException { - this.desiredResponse = (Map) TestingFramework.deepcopy(desiredResponse); - } - - /** - * After the handler returns the response, any exception or failed assertion will be - * in the member called "thrown". A testing framework can later call this method - * which will rethrow the exception that was thrown before. - * - * @throws TestingFrameworkException - */ - public void assertNothingThrown() throws TestingFrameworkException { - if (thrown != null) { - final TestingFrameworkException e = thrown instanceof TestingFrameworkException ? - (TestingFrameworkException) thrown : - new TestingFrameworkException(thrown); - thrown = null; - throw e; - } - } - - /** - *

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.

- * - *

Also, a response will be returned that matches the desiredResponse.

- */ - @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 expectedQuery = (Map) requestExpectations.get(QUERY); - if (expectedQuery != null) { - final URI uri = request.getUri(); - final URIBuilder uriBuilder = new URIBuilder(uri, StandardCharsets.UTF_8); - final List actualParams = uriBuilder.getQueryParams(); - final Map actualParamsMap = new HashMap<>(); - for (final NameValuePair actualParam : actualParams) { - actualParamsMap.put(actualParam.getName(), actualParam.getValue()); - } - for (final Map.Entry 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 expectedHeaders = (Map) requestExpectations.get(HEADERS); - if (expectedHeaders != null) { - final Map actualHeadersMap = new HashMap<>(); - final Header[] actualHeaders = request.getHeaders(); - for (final Header header : actualHeaders) { - actualHeadersMap.put(header.getName(), header.getValue()); - } - for (final Entry 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, 1024); - 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 desiredHeaders = (Map) desiredResponse.get(HEADERS); - if (desiredHeaders != null) { - for (final Entry 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; - } - } -} diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientAdapter.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientAdapter.java deleted file mode 100644 index a3910fe673..0000000000 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientAdapter.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ -package org.apache.hc.core5.testing.framework; - -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.BODY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.CONTENT_TYPE; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.HEADERS; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.METHOD; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PATH; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PROTOCOL_VERSION; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.QUERY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.STATUS; - -import java.net.URI; -import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -import org.apache.hc.core5.http.ContentType; -import org.apache.hc.core5.http.HttpHost; -import org.apache.hc.core5.http.HttpVersion; -import org.apache.hc.core5.http.Method; -import org.apache.hc.core5.http.io.SocketConfig; -import org.apache.hc.core5.http.io.entity.EntityUtils; -import org.apache.hc.core5.http.io.entity.StringEntity; -import org.apache.hc.core5.io.CloseMode; -import org.apache.hc.core5.testing.classic.ClassicTestServer; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class TestClassicTestClientAdapter { - - private ClassicTestServer server; - - @BeforeEach - void initServer() { - this.server = new ClassicTestServer(SocketConfig.custom() - .setSoTimeout(5, TimeUnit.SECONDS) - .build()); - } - - @AfterEach - void shutDownServer() { - if (this.server != null) { - this.server.shutdown(CloseMode.IMMEDIATE); - } - } - - @Test - void executeBuildsRequestAndParsesResponse() throws Exception { - final Map captured = new HashMap<>(); - server.register("*", (request, response, context) -> { - captured.put("method", request.getMethod()); - captured.put("version", request.getVersion()); - final URI requestUri; - try { - requestUri = request.getUri(); - } catch (final URISyntaxException ex) { - captured.put("uri-error", ex); - response.setEntity(new StringEntity("bad uri", ContentType.TEXT_PLAIN)); - return; - } - captured.put("path", requestUri.getPath()); - captured.put("query", requestUri.getQuery()); - captured.put("header", request.getFirstHeader("X-Test") != null - ? request.getFirstHeader("X-Test").getValue() - : null); - if (request.getEntity() != null) { - captured.put("content-type", request.getEntity().getContentType()); - captured.put("body", EntityUtils.toString(request.getEntity(), Integer.MAX_VALUE)); - } else { - captured.put("content-type", null); - captured.put("body", null); - } - - response.addHeader("X-Reply", "ok"); - response.setEntity(new StringEntity("pong", ContentType.TEXT_PLAIN)); - }); - - server.start(); - final String defaultURI = new HttpHost("localhost", server.getPort()).toString(); - - final Map request = new HashMap<>(); - request.put(PATH, "echo"); - request.put(METHOD, Method.POST.name()); - request.put(BODY, "ping"); - request.put(CONTENT_TYPE, ContentType.TEXT_PLAIN.toString()); - request.put(PROTOCOL_VERSION, HttpVersion.HTTP_1_0); - final Map headers = new HashMap<>(); - headers.put("X-Test", "value"); - request.put(HEADERS, headers); - final Map query = new HashMap<>(); - query.put("a", "b"); - query.put("c", "d"); - request.put(QUERY, query); - - final ClassicTestClientAdapter adapter = new ClassicTestClientAdapter(); - final Map response = adapter.execute(defaultURI, request); - - Assertions.assertNull(captured.get("uri-error")); - Assertions.assertEquals(Method.POST.name(), captured.get("method")); - final Object version = captured.get("version"); - Assertions.assertTrue(HttpVersion.HTTP_1_0.equals(version) || HttpVersion.HTTP_1_1.equals(version)); - final String path = (String) captured.get("path"); - Assertions.assertNotNull(path); - Assertions.assertTrue(path.endsWith("/echo")); - final String queryString = (String) captured.get("query"); - Assertions.assertNotNull(queryString); - Assertions.assertTrue(queryString.contains("a=b")); - Assertions.assertTrue(queryString.contains("c=d")); - Assertions.assertEquals("value", captured.get("header")); - final String capturedContentType = (String) captured.get("content-type"); - Assertions.assertNotNull(capturedContentType); - Assertions.assertTrue(capturedContentType.contains("text/plain")); - Assertions.assertEquals("ping", captured.get("body")); - - Assertions.assertEquals(200, response.get(STATUS)); - Assertions.assertEquals("pong", response.get(BODY)); - final String contentType = (String) response.get(CONTENT_TYPE); - Assertions.assertNotNull(contentType); - Assertions.assertTrue(contentType.contains("text/plain")); - @SuppressWarnings("unchecked") - final Map responseHeaders = (Map) response.get(HEADERS); - Assertions.assertEquals("ok", responseHeaders.get("X-Reply")); - } - - @Test - void executeWithoutEntityReturnsNullBody() throws Exception { - server.register("/empty", (request, response, context) -> { - Assertions.assertNull(request.getEntity()); - response.addHeader("X-Empty", "yes"); - response.setCode(204); - }); - - server.start(); - final String defaultURI = new HttpHost("localhost", server.getPort()).toString(); - - final Map request = new HashMap<>(); - request.put(PATH, "empty"); - request.put(METHOD, Method.GET.name()); - - final ClassicTestClientAdapter adapter = new ClassicTestClientAdapter(); - final Map response = adapter.execute(defaultURI, request); - - Assertions.assertEquals(204, response.get(STATUS)); - Assertions.assertNull(response.get(BODY)); - Assertions.assertNull(response.get(CONTENT_TYPE)); - @SuppressWarnings("unchecked") - final Map responseHeaders = (Map) response.get(HEADERS); - Assertions.assertEquals("yes", responseHeaders.get("X-Empty")); - } - - @Test - void getClientName() { - final ClassicTestClientAdapter adapter = new ClassicTestClientAdapter(); - Assertions.assertEquals("ClassicTestClient", adapter.getClientName()); - } - -} diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientTestingAdapter.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientTestingAdapter.java deleted file mode 100644 index a6856ab350..0000000000 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientTestingAdapter.java +++ /dev/null @@ -1,263 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package org.apache.hc.core5.testing.framework; - -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.BODY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.HEADERS; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.METHOD; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PATH; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.STATUS; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -import org.apache.hc.core5.http.ClassicHttpRequest; -import org.apache.hc.core5.http.ClassicHttpResponse; -import org.apache.hc.core5.http.HttpException; -import org.apache.hc.core5.http.HttpHost; -import org.apache.hc.core5.http.Method; -import org.apache.hc.core5.http.io.SocketConfig; -import org.apache.hc.core5.http.protocol.HttpContext; -import org.apache.hc.core5.io.CloseMode; -import org.apache.hc.core5.testing.classic.ClassicTestServer; -import org.apache.hc.core5.testing.classic.EchoHandler; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mockito; - -class TestClassicTestClientTestingAdapter { - private static final String ECHO_PATH = "echo/something"; - private static final String CUSTOM_PATH = "custom/something"; - - private ClassicTestServer server; - - @BeforeEach - void initServer() { - this.server = new ClassicTestServer(SocketConfig.custom() - .setSoTimeout(5, TimeUnit.SECONDS).build()); - } - - @AfterEach - void shutDownServer() { - if (this.server != null) { - this.server.shutdown(CloseMode.IMMEDIATE); - } - } - - @Test - void nullDefaultURI() { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final String defaultURI = null; - final Map request = new HashMap<>(); - final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class); - final Map responseExpectations = new HashMap<>(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> - adapter.execute(defaultURI, request, requestHandler, responseExpectations)); - } - - @Test - void nullRequest() { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final String defaultURI = ""; - final Map request = null; - final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class); - final Map responseExpectations = new HashMap<>(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> - adapter.execute(defaultURI, request, requestHandler, responseExpectations)); - } - - @Test - void nullRequestHandler() { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final String defaultURI = ""; - final Map request = new HashMap<>(); - final TestingFrameworkRequestHandler requestHandler = null; - final Map responseExpectations = new HashMap<>(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> - adapter.execute(defaultURI, request, requestHandler, responseExpectations)); - } - - @Test - void nullResponseExpectations() { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final String defaultURI = ""; - final Map request = new HashMap<>(); - final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class); - final Map responseExpectations = null; - - Assertions.assertThrows(TestingFrameworkException.class, () -> - adapter.execute(defaultURI, request, requestHandler, responseExpectations)); - } - - @Test - void noPath() { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final String defaultURI = ""; - final Map request = new HashMap<>(); - final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class); - final Map responseExpectations = new HashMap<>(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> - adapter.execute(defaultURI, request, requestHandler, responseExpectations)); - } - - @Test - void noMethod() { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final String defaultURI = ""; - - final Map request = new HashMap<>(); - request.put(PATH, ECHO_PATH); - - final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class); - final Map responseExpectations = new HashMap<>(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> - adapter.execute(defaultURI, request, requestHandler, responseExpectations)); - } - - @Test - void invalidMethod() { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final String defaultURI = ""; - - final Map request = new HashMap<>(); - request.put(PATH, ECHO_PATH); - request.put(METHOD, "JUNK"); - - final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class); - final Map responseExpectations = new HashMap<>(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> - adapter.execute(defaultURI, request, requestHandler, responseExpectations)); - } - - @Test - void withLiveServerEcho() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - // Initialize the server-side request handler - server.register("/echo/*", new EchoHandler()); - - - this.server.start(); - final HttpHost target = new HttpHost("localhost", this.server.getPort()); - - final String defaultURI = target.toString(); - final Map request = new HashMap<>(); - request.put(PATH, ECHO_PATH); - request.put(METHOD, Method.POST.name()); - final String body = "mybody"; - request.put(BODY, body); - - final Map responseExpectations = new HashMap<>(); - - final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class); - final Map response = adapter.execute(defaultURI, request, requestHandler, responseExpectations); - - Assertions.assertNotNull(response, "response should not be null"); - Assertions.assertEquals(200, response.get(STATUS), "status unexpected"); - - @SuppressWarnings("unchecked") - final Map headers = (Map) response.get(HEADERS); - Assertions.assertNotNull(headers, "headers should be in the response"); - Assertions.assertFalse(headers.isEmpty()); - - final String returnedBody = (String) response.get(BODY); - Assertions.assertNotNull(returnedBody, "body should be in the response"); - Assertions.assertEquals(body, returnedBody, "Body should be echoed"); - - } - - @Test - void withLiveServerCustomRequestHandler() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final TestingFrameworkRequestHandler requestHandler = new TestingFrameworkRequestHandler() { - @Override - public void handle(final ClassicHttpRequest request, final ClassicHttpResponse response, final HttpContext context) - throws HttpException, IOException { - try { - Assertions.assertEquals("junk", request.getMethod(), "method not expected"); - } catch (final Throwable t) { - thrown = t; - } - } - }; - server.register("/custom/*", requestHandler); - - this.server.start(); - final HttpHost target = new HttpHost("localhost", this.server.getPort()); - final String defaultURI = target.toString(); - final Map responseExpectations = new HashMap<>(); - - final Map request = new HashMap<>(); - request.put(PATH, CUSTOM_PATH); - - for (final String method : TestingFramework.ALL_METHODS) { - request.put(METHOD, method); - - adapter.execute(defaultURI, request, requestHandler, responseExpectations); - } - } - - @Test - void modifyRequest() { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final Map request = new HashMap<>(); - final Map returnedRequest = adapter.modifyRequest(request); - - Assertions.assertSame(request, returnedRequest, "Same request was not returned as expected."); - } - - @Test - void modifyResponseExpectations() { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final Map responseExpectations = new HashMap<>(); - final Map returnedResponseExpectations = adapter.modifyResponseExpectations(null, responseExpectations); - - Assertions.assertSame(responseExpectations, returnedResponseExpectations, "Same response expectations were not returned as expected."); - } - -} diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClientPojoAdapter.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClientPojoAdapter.java deleted file mode 100644 index d703a1c273..0000000000 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClientPojoAdapter.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ -package org.apache.hc.core5.testing.framework; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class TestClientPojoAdapter { - @Test - void modifyRequest() { - final ClientPOJOAdapter adapter = new ClassicTestClientAdapter(); - final Map request = new HashMap<>(); - final Map request2 = adapter.modifyRequest(request); - - Assertions.assertSame(request, request2, "request should have been returned"); - } - - @Test - void checkRequestSupport() throws Exception { - final ClientPOJOAdapter adapter = new ClassicTestClientAdapter(); - final String reason = adapter.checkRequestSupport(null); - - Assertions.assertNull(reason, "reason should be null"); - - adapter.assertRequestSupported(null); - } - - @Test - void checkRequestSupportThrows() { - final ClientPOJOAdapter adapter = new ClientPOJOAdapter() { - - @Override - public Map execute(final String defaultURI, final Map request) throws Exception { - return null; - } - - @Override - public String checkRequestSupport(final java.util.Map request) { - return "A reason this request is not supported."; - } - - @Override - public String getClientName() { - return null; - } - }; - - Assertions.assertThrows(Exception.class, () -> adapter.assertRequestSupported(null)); - } -} diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClientTestingAdapter.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClientTestingAdapter.java deleted file mode 100644 index c5fd26f978..0000000000 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClientTestingAdapter.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ -package org.apache.hc.core5.testing.framework; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class TestClientTestingAdapter { - - @Test - void getHttpClientPOJOAdapter() { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final ClientPOJOAdapter pojoAdapter = adapter.getClientPOJOAdapter(); - - Assertions.assertNotNull(pojoAdapter, "pojoAdapter should not be null"); - } - - @Test - void isRequestSupported() { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final Map request = null; - Assertions.assertTrue(adapter.isRequestSupported(request), "isRequestSupported should return true"); - - } - - @Test - void executeCallsAssertNothingThrownWhenEnabled() throws Exception { - final ClientPOJOAdapter pojoAdapter = new ClientPOJOAdapter() { - @Override - public Map execute(final String defaultURI, final Map request) { - return new HashMap<>(); - } - - @Override - public String getClientName() { - return "test"; - } - }; - final ClientTestingAdapter adapter = new ClientTestingAdapter(pojoAdapter) { - { - callAssertNothingThrown = true; - } - }; - - final TestingFrameworkRequestHandler requestHandler = new TestingFrameworkRequestHandler() { - }; - - adapter.execute("http://localhost", new HashMap<>(), requestHandler, new HashMap<>()); - - Assertions.assertDoesNotThrow(requestHandler::assertNothingThrown); - } - - @Test - void executeThrowsWhenHandlerNullAndAssertEnabled() { - final ClientPOJOAdapter pojoAdapter = new ClientPOJOAdapter() { - @Override - public Map execute(final String defaultURI, final Map request) { - return new HashMap<>(); - } - - @Override - public String getClientName() { - return "test"; - } - }; - final ClientTestingAdapter adapter = new ClientTestingAdapter(pojoAdapter) { - { - callAssertNothingThrown = true; - } - }; - - Assertions.assertThrows(TestingFrameworkException.class, () -> - adapter.execute("http://localhost", new HashMap<>(), null, new HashMap<>())); - } - - @Test - void executeThrowsWhenAdapterNull() { - final ClientTestingAdapter adapter = new ClientTestingAdapter(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> - adapter.execute("http://localhost", new HashMap<>(), new TestingFrameworkRequestHandler(), new HashMap<>())); - } -} diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestFrameworkTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestFrameworkTest.java deleted file mode 100644 index 8a18f49e9a..0000000000 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestFrameworkTest.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package org.apache.hc.core5.testing.framework; - -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.BODY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.CONTENT_TYPE; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.HEADERS; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.METHOD; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PROTOCOL_VERSION; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.QUERY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.REQUEST; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.RESPONSE; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.STATUS; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class TestFrameworkTest { - @Test - void defaults() throws Exception { - final FrameworkTest test = new FrameworkTest(); - final Map request = test.initRequest(); - - Assertions.assertNotNull(request, "request should not be null"); - Assertions.assertEquals("GET", request.get(METHOD), "Default method should be GET"); - - Assertions.assertEquals(TestingFramework.DEFAULT_REQUEST_BODY, - request.get(BODY), "Default request body expected."); - - Assertions.assertEquals(TestingFramework.DEFAULT_REQUEST_CONTENT_TYPE, - request.get(CONTENT_TYPE), "Default request content type expected."); - - Assertions.assertEquals(TestingFramework.DEFAULT_REQUEST_QUERY, - request.get(QUERY), "Default request query parameters expected."); - - Assertions.assertEquals(TestingFramework.DEFAULT_REQUEST_HEADERS, - request.get(HEADERS), "Default request headers expected."); - - Assertions.assertEquals(TestingFramework.DEFAULT_REQUEST_PROTOCOL_VERSION, - request.get(PROTOCOL_VERSION), "Default protocol version expected."); - - final Map responseExpectations = test.initResponseExpectations(); - Assertions.assertNotNull(responseExpectations, "responseExpectations should not be null"); - Assertions.assertEquals(TestingFramework.DEFAULT_RESPONSE_STATUS, - responseExpectations.get(STATUS), "Default status expected."); - - Assertions.assertEquals(TestingFramework.DEFAULT_RESPONSE_BODY, - responseExpectations.get(BODY), "Default body expected."); - - Assertions.assertEquals(TestingFramework.DEFAULT_RESPONSE_CONTENT_TYPE, - responseExpectations.get(CONTENT_TYPE), "Default response content type expected."); - - Assertions.assertEquals(TestingFramework.DEFAULT_RESPONSE_HEADERS, - responseExpectations.get(HEADERS), "Default headers expected."); - } - - @Test - void changeStatus() { - final Map testMap = new HashMap<>(); - final Map response = new HashMap<>(); - testMap.put(RESPONSE, response); - response.put(STATUS, 201); - - final FrameworkTest test = new FrameworkTest(testMap); - final Map responseExpectations = test.initResponseExpectations(); - - Assertions.assertEquals(201, responseExpectations.get(STATUS), "Status unexpected."); - } - - @Test - void changeMethod() throws Exception { - final Map testMap = new HashMap<>(); - final Map request = new HashMap<>(); - testMap.put(REQUEST, request); - request.put(METHOD, "POST"); - - final FrameworkTest test = new FrameworkTest(testMap); - final Map requestExpectations = test.initRequest(); - - Assertions.assertEquals("POST", requestExpectations.get(METHOD), "Method unexpected."); - } -} diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestTestingFramework.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestTestingFramework.java deleted file mode 100644 index 8487be8561..0000000000 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestTestingFramework.java +++ /dev/null @@ -1,1180 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package org.apache.hc.core5.testing.framework; - -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.BODY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.CONTENT_TYPE; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.HEADERS; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.METHOD; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.NAME; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PATH; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.QUERY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.REQUEST; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.RESPONSE; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.STATUS; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import org.apache.hc.core5.http.ContentType; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentMatchers; -import org.mockito.Mockito; - -class TestTestingFramework { - - @Test - void ensureDefaultMapsUnmodifiable() { - assertUnmodifiable(TestingFramework.DEFAULT_REQUEST_QUERY); - assertUnmodifiable(TestingFramework.DEFAULT_RESPONSE_HEADERS); - } - - private void assertUnmodifiable(final Map map) { - final String aKey = (String) map.keySet().toArray()[0]; - Assertions.assertThrows(UnsupportedOperationException.class, () -> map.remove(aKey)); - } - - private TestingFramework newWebServerTestingFramework(final ClientTestingAdapter adapter) - throws TestingFrameworkException { - final TestingFramework framework = new TestingFramework(adapter); - // get rid of the default tests. - framework.deleteTests(); - - return framework; - } - - private TestingFramework newWebServerTestingFramework() throws TestingFrameworkException { - return newWebServerTestingFramework(null); // null adapter - } - - @Test - void runTestsWithoutSettingAdapterThrows() throws Exception { - final TestingFramework framework = newWebServerTestingFramework(); - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void nullAdapterThrows() throws Exception { - final ClientTestingAdapter adapter = null; - - final TestingFramework framework = newWebServerTestingFramework(adapter); - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void nullSetAdapterThrows() throws Exception { - final ClientTestingAdapter adapter = null; - - final TestingFramework framework = newWebServerTestingFramework(adapter); - framework.setAdapter(adapter); - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void goodAdapterWithConstructor() throws Exception { - final ClientTestingAdapter adapter = Mockito.mock(ClientTestingAdapter.class); - - // Have isRequestSupported() return false so no test will run. - Mockito.when(adapter.isRequestSupported(ArgumentMatchers.anyMap())) - .thenReturn(false); - - final TestingFramework framework = newWebServerTestingFramework(adapter); - - framework.runTests(); - - // since there are no tests, callMethod should not be called. - verifyCallMethodNeverCalled(adapter); - } - - private void verifyCallMethodNeverCalled(final ClientTestingAdapter adapter) throws Exception { - Mockito.verify(adapter, Mockito.never()).execute(ArgumentMatchers.anyString(), ArgumentMatchers.anyMap(), - ArgumentMatchers.any(TestingFrameworkRequestHandler.class), ArgumentMatchers.anyMap()); - } - - private TestingFramework newFrameworkAndSetAdapter(final ClientTestingAdapter adapter) - throws TestingFrameworkException { - final TestingFramework framework = new TestingFramework(); - framework.setAdapter(adapter); - - // get rid of the default tests. - framework.deleteTests(); - - return framework; - } - - @Test - void goodAdapterWithSetter() throws Exception { - final ClientTestingAdapter adapter = Mockito.mock(ClientTestingAdapter.class); - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.runTests(); - - // since there are no tests, callMethod should not be called. - verifyCallMethodNeverCalled(adapter); - } - - @Test - void addTest() throws Exception { - final TestingFrameworkRequestHandler mockRequestHandler = Mockito.mock(TestingFrameworkRequestHandler.class); - - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - assertMatchesDefaultURI(defaultURI); - - Assertions.assertNotNull(request, "request should not be null"); - - // The request should be equal to the default request. - final Map defaultRequest = new FrameworkTest().initRequest(); - Assertions.assertEquals(defaultRequest, request, "The request does not match the default"); - - Assertions.assertSame(mockRequestHandler, requestHandler, "The request handler should have been passed to the adapter"); - - // The responseExpectations should be equal to the default. - final Map defaultResponseExpectations = new FrameworkTest().initResponseExpectations(); - Assertions.assertEquals(defaultResponseExpectations, responseExpectations, "The responseExpectations do not match the defaults"); - - final Map response = new HashMap<>(); - response.put(STATUS, responseExpectations.get(STATUS)); - response.put(BODY, responseExpectations.get(BODY)); - response.put(CONTENT_TYPE, responseExpectations.get(CONTENT_TYPE)); - response.put(HEADERS, responseExpectations.get(HEADERS)); - return response; - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - framework.setRequestHandler(mockRequestHandler); - - framework.addTest(); - - framework.runTests(); - - // assertNothingThrown() should have been called. - Mockito.verify(mockRequestHandler).assertNothingThrown(); - } - - private void assertMatchesDefaultURI(final String defaultURI) { - final String regex = "http://localhost:\\d+/"; - Assertions.assertTrue(defaultURI.matches(regex), "Default URI should match regex=" + regex); - } - - @Test - void statusCheck() throws Exception { - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) { - - Assertions.assertEquals(200, responseExpectations.get(STATUS)); - - // return a different status than expected. - final Map response = new HashMap<>(); - response.put(STATUS, 201); - return response; - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - private Map alreadyCheckedResponse() { - // return an indication that the response has already been checked. - final Map response = new HashMap<>(); - response.put(STATUS, TestingFramework.ALREADY_CHECKED); - response.put(BODY, TestingFramework.ALREADY_CHECKED); - response.put(CONTENT_TYPE, TestingFramework.ALREADY_CHECKED); - response.put(HEADERS, TestingFramework.ALREADY_CHECKED); - return response; - } - - @Test - void responseAlreadyChecked() throws Exception { - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) { - - return alreadyCheckedResponse(); - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - framework.runTests(); - } - - @Test - void bodyCheck() throws Exception { - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) { - - Assertions.assertEquals(TestingFramework.DEFAULT_RESPONSE_BODY, responseExpectations.get(BODY)); - - final Map response = new HashMap<>(); - response.put(STATUS, TestingFramework.ALREADY_CHECKED); - - // return a different body than expected. - response.put(BODY, TestingFramework.DEFAULT_RESPONSE_BODY + "junk"); - return response; - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void responseContentTypeCheck() throws Exception { - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) { - - Assertions.assertEquals(TestingFramework.DEFAULT_RESPONSE_CONTENT_TYPE, responseExpectations.get(CONTENT_TYPE)); - - final Map response = new HashMap<>(); - response.put(STATUS, TestingFramework.ALREADY_CHECKED); - response.put(HEADERS, TestingFramework.ALREADY_CHECKED); - - // return the expected body - response.put(BODY, TestingFramework.DEFAULT_RESPONSE_BODY); - // return a different content type than expected. - response.put(CONTENT_TYPE, ContentType.DEFAULT_TEXT); - return response; - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void deepcopy() throws Exception { - // save a copy of the headers to make sure they haven't changed at the end of this test. - @SuppressWarnings("unchecked") - final Map headersCopy = (Map) TestingFramework.deepcopy(TestingFramework.DEFAULT_RESPONSE_HEADERS); - Assertions.assertEquals(TestingFramework.DEFAULT_RESPONSE_HEADERS, headersCopy); - - final Map deepMap = new HashMap<>(); - deepMap.put(HEADERS, TestingFramework.DEFAULT_RESPONSE_HEADERS); - - @SuppressWarnings("unchecked") - final Map deepMapCopy = (Map) TestingFramework.deepcopy(deepMap); - Assertions.assertEquals(deepMap, deepMapCopy); - - @SuppressWarnings("unchecked") - final Map headersMap = (Map) deepMapCopy.get(HEADERS); - Assertions.assertEquals(headersCopy, headersMap); - - // now make sure the default headers have not changed for some unexpected reason. - Assertions.assertEquals(TestingFramework.DEFAULT_RESPONSE_HEADERS, headersCopy); - } - - @Test - void removedHeaderCheck() throws Exception { - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) { - - Assertions.assertEquals(TestingFramework.DEFAULT_RESPONSE_HEADERS, responseExpectations.get(HEADERS)); - - @SuppressWarnings("unchecked") - final Map headersCopy = (Map) deepcopy(responseExpectations.get(HEADERS)); - - // remove a header to force an error - final String headerName = (String) headersCopy.keySet().toArray()[0]; - headersCopy.remove(headerName); - - final Map response = new HashMap<>(); - response.put(STATUS, TestingFramework.ALREADY_CHECKED); - response.put(BODY, TestingFramework.ALREADY_CHECKED); - - // return different headers than expected. - response.put(HEADERS, headersCopy); - return response; - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void changedHeaderCheck() throws Exception { - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) { - - Assertions.assertEquals(TestingFramework.DEFAULT_RESPONSE_HEADERS, responseExpectations.get(HEADERS)); - - @SuppressWarnings("unchecked") - final Map headersCopy = (Map) deepcopy(responseExpectations.get(HEADERS)); - - // change a header to force an error - final String headerName = (String) headersCopy.keySet().toArray()[0]; - headersCopy.put(headerName, headersCopy.get(headerName) + "junk"); - - final Map response = new HashMap<>(); - response.put(STATUS, TestingFramework.ALREADY_CHECKED); - response.put(BODY, TestingFramework.ALREADY_CHECKED); - - // return different headers than expected. - response.put(HEADERS, headersCopy); - return response; - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - private Object deepcopy(final Object obj) { - try { - return TestingFramework.deepcopy(obj); - } catch (final Exception e) { - Assertions.fail("deepcopy failed: " + e.getMessage()); - return null; - } - } - - @Test - void requestMethodUnexpected() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - // change the request from what is expected. - Assertions.assertEquals("GET", request.get(METHOD)); - request.put(METHOD, "POST"); - return super.execute(defaultURI, request, requestHandler, responseExpectations); - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - final Map test = new HashMap<>(); - final Map request = new HashMap<>(); - test.put(REQUEST, request); - request.put(NAME, "MyName"); - - framework.addTest(test); - - final TestingFrameworkException exception = Assertions.assertThrows(TestingFrameworkException.class, () -> - framework.runTests()); - // make sure the HTTP Client name is in the message. - final String message = exception.getMessage(); - final ClientPOJOAdapter pojoAdapter = adapter.getClientPOJOAdapter(); - final String httpClientName = pojoAdapter == null ? - TestingFrameworkException.NO_HTTP_CLIENT : - pojoAdapter.getClientName(); - Assertions.assertTrue(message.contains(httpClientName), "Message should contain httpClientName of " + httpClientName + "; message=" + message); - - Assertions.assertTrue(message.contains("MyName"), "Message should contain the test. message=" + message); - } - - @Test - void status201() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - final Map test = new HashMap<>(); - final Map response = new HashMap<>(); - test.put(RESPONSE, response); - response.put(STATUS, 201); - - framework.addTest(test); - - framework.runTests(); - } - - @Test - void deepcopyOfTest() throws Exception { - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) - throws TestingFrameworkException { - Assertions.assertEquals(201, responseExpectations.get(STATUS)); - return alreadyCheckedResponse(); - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - final Map test = new HashMap<>(); - final Map response = new HashMap<>(); - test.put(RESPONSE, response); - response.put(STATUS, 201); - - framework.addTest(test); - - // Make sure the framework makes a copy of the test for itself. - // This put should be ignored by the framework. - response.put(STATUS, 300); - - framework.runTests(); - } - - @Test - void removeParameter() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - // change the request from what is expected. - @SuppressWarnings("unchecked") - final Map query = (Map) request.get(QUERY); - Assertions.assertTrue(query.containsKey("p1")); - query.remove("p1"); - return super.execute(defaultURI, request, requestHandler, responseExpectations); - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void changeParameter() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - // change the request from what is expected. - @SuppressWarnings("unchecked") - final Map query = (Map) request.get(QUERY); - Assertions.assertTrue(query.containsKey("p1")); - query.put("p1", query.get("p1") + "junk"); - return super.execute(defaultURI, request, requestHandler, responseExpectations); - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void removeHeader() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - // change the request from what is expected. - @SuppressWarnings("unchecked") - final Map headers = (Map) request.get(HEADERS); - Assertions.assertTrue(headers.containsKey("header1")); - headers.remove("header1"); - return super.execute(defaultURI, request, requestHandler, responseExpectations); - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void changeHeader() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - // change the request from what is expected. - @SuppressWarnings("unchecked") - final Map headers = (Map) request.get(HEADERS); - Assertions.assertTrue(headers.containsKey("header1")); - headers.put("header1", headers.get("header1") + "junk"); - return super.execute(defaultURI, request, requestHandler, responseExpectations); - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void changeBody() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - // change the request from what is expected. - final String body = (String) request.get(BODY); - Assertions.assertNotNull(body); - request.put(BODY, request.get(BODY) + "junk"); - return super.execute(defaultURI, request, requestHandler, responseExpectations); - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void changeContentType() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - // change the request from what is expected. - final String contentType = (String) request.get(CONTENT_TYPE); - Assertions.assertNotNull(contentType); - request.put(CONTENT_TYPE, request.get(CONTENT_TYPE) + "junk"); - return super.execute(defaultURI, request, requestHandler, responseExpectations); - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void changeResponseExpectationsFails() throws Exception { - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - - /* - * The adapter should change the responseExpectations in the modifyResponseExpectations() - * method before they are sent to the request handler. The expectations should not - * be changed here. - */ - // the next command should throw. - responseExpectations.put(STATUS, 201); - return null; - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - Assertions.assertThrows(TestingFrameworkException.class, () -> framework.runTests()); - } - - @Test - void changeResponseStatus() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - // change the responseExpectations from what is expected. The change should be ignored - // by the request handler, and a 200 should actually be returned. - Assertions.assertEquals(200, responseExpectations.get(STATUS)); - - // The next line is needed because we have to make a copy of the responseExpectations. - // It is an unmodifiable map. - final Map tempResponseExpectations = new HashMap<>(responseExpectations); - tempResponseExpectations.put(STATUS, 201); - final Map response = super.execute(defaultURI, request, requestHandler, tempResponseExpectations); - Assertions.assertEquals(200, response.get(STATUS)); - - return response; - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - framework.runTests(); - } - - @Test - void modifyRequestCalled() throws Exception { - final TestingFrameworkRequestHandler mockRequestHandler = Mockito.mock(TestingFrameworkRequestHandler.class); - final String UNLIKELY_ITEM = "something_unlikely_to_be_in_a_real_request"; - - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - // make sure the modifyRequest method was called by seeing if the request was modified. - Assertions.assertTrue(request.containsKey(UNLIKELY_ITEM), "modifyRequest should have been called."); - - final Map response = new HashMap<>(); - response.put(STATUS, responseExpectations.get(STATUS)); - response.put(BODY, responseExpectations.get(BODY)); - response.put(CONTENT_TYPE, responseExpectations.get(CONTENT_TYPE)); - response.put(HEADERS, responseExpectations.get(HEADERS)); - return response; - } - - @Override - public Map modifyRequest(final Map request) { - // let the adapter change the request if needed. - request.put(UNLIKELY_ITEM, new Object()); - return super.modifyRequest(request); - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - framework.setRequestHandler(mockRequestHandler); - - framework.addTest(); - - framework.runTests(); - - // assertNothingThrown() should have been called. - Mockito.verify(mockRequestHandler).assertNothingThrown(); - } - - @Test - void modifyResponseExpectationsCalled() throws Exception { - final TestingFrameworkRequestHandler mockRequestHandler = Mockito.mock(TestingFrameworkRequestHandler.class); - final String UNLIKELY_ITEM = "something_unlikely_to_be_in_a_real_response"; - - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - // make sure the modifyRequest method was called by seeing if the request was modified. - Assertions.assertTrue(responseExpectations.containsKey(UNLIKELY_ITEM), "modifyResponseExpectations should have been called."); - - final Map response = new HashMap<>(); - response.put(STATUS, responseExpectations.get(STATUS)); - response.put(BODY, responseExpectations.get(BODY)); - response.put(CONTENT_TYPE, responseExpectations.get(CONTENT_TYPE)); - response.put(HEADERS, responseExpectations.get(HEADERS)); - return response; - } - - @Override - public Map modifyResponseExpectations( - final Map request, - final Map responseExpectations) { - // let the adapter change the request if needed. - responseExpectations.put(UNLIKELY_ITEM, new Object()); - return super.modifyResponseExpectations(request, responseExpectations); - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - framework.setRequestHandler(mockRequestHandler); - - framework.addTest(); - - framework.runTests(); - - // assertNothingThrown() should have been called. - Mockito.verify(mockRequestHandler).assertNothingThrown(); - } - - @Test - void adapterDoesNotSupport() throws Exception { - - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - - Assertions.fail("callMethod should not have been called"); - return null; - } - - @Override - public boolean isRequestSupported(final Map request) { - return false; - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - framework.addTest(); - - framework.runTests(); - } - - @Test - void defaultTestsWithMockedAdapter() throws Exception { - final Set calledMethodSet = new HashSet<>(); - - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute( - final String defaultURI, - final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - - calledMethodSet.add((String) request.get(METHOD)); - return alreadyCheckedResponse(); - } - }; - - // create the framework without deleting the default tests. - final TestingFramework framework = new TestingFramework(); - framework.setAdapter(adapter); - - framework.runTests(); - - for (final String method : TestingFramework.ALL_METHODS) { - Assertions.assertTrue(calledMethodSet.contains(method), "Method not in default tests. method=" + method); - } - } - - @Test - void defaultTests() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter(); - - // create the framework without deleting the default tests. - final TestingFramework framework = new TestingFramework(); - framework.setAdapter(adapter); - - framework.runTests(); - - } - - @Test - void addTestNoMocks() throws TestingFrameworkException { - - final TestingFramework framework = new TestingFramework(new ClassicTestClientTestingAdapter()); - -// The following test could be constructed in Groovy/Spock like this: -// -// HttpServerTestingFramework.ALL_METHODS.each { method -> -// framework.addTest( -// request: [ -// path: '/stuff', -// method:method, -// query: [param : 'something'], -// headers: [header1:'stuff', header2:'more-stuff'], -// contentType: 'text/plain; charset=us-ascii', -// body: 'What is the meaning of life?', -// ], -// response: [ -// status:201, -// headers: [header3:'header_stuff',], -// contentType: 'text/html; charset=us-ascii', -// body: responseBody, -// ], -// ) - - final Map test = new HashMap<>(); - - // Add request. - final Map request = new HashMap<>(); - test.put(REQUEST, request); - - request.put(PATH, "stuff"); - - final Map queryMap = new HashMap<>(); - request.put(QUERY, queryMap); - - queryMap.put("param", "something"); - - final Map requestHeadersMap = new HashMap<>(); - request.put(HEADERS, requestHeadersMap); - - requestHeadersMap.put("header1", "stuff"); - requestHeadersMap.put("header2", "more-stuff"); - - request.put(CONTENT_TYPE, "text/plain; charset=us-ascii"); - request.put(BODY, "What is the meaning of life?"); - - // Response - final Map response = new HashMap<>(); - test.put(RESPONSE, response); - - response.put(STATUS, 201); - - final Map responseHeadersMap = new HashMap<>(); - response.put(HEADERS, responseHeadersMap); - - responseHeadersMap.put("header3", "header_stuff"); - - response.put(CONTENT_TYPE, "text/html; charset=us-ascii"); - response.put(BODY, "42"); - - for (final String method : TestingFramework.ALL_METHODS) { - request.put(METHOD, method); - - framework.addTest(test); - } - framework.runTests(); - } - - @Test - void nulls() throws TestingFrameworkException { - - final TestingFramework framework = new TestingFramework(new ClassicTestClientTestingAdapter()); - -// The following test could be constructed in Groovy/Spock like this: -// -// WebServerTestingFramework.ALL_METHODS.each { method -> -// framework.addTest( -// request: [ -// path: null, -// method:method, -// query: null, -// headers: null, -// contentType: null, -// body: null, -// ], -// response: [ -// status:null, -// headers: null, -// contentType: null, -// body: null, -// ], -// ) - - final Map test = new HashMap<>(); - - // Add request. - final Map request = new HashMap<>(); - test.put(REQUEST, request); - - request.put(PATH, null); - - request.put(QUERY, null); - - - request.put(HEADERS, null); - - request.put(CONTENT_TYPE, null); - request.put(BODY, null); - - // Response - final Map response = new HashMap<>(); - test.put(RESPONSE, response); - - response.put(STATUS, null); - - response.put(HEADERS, null); - - response.put(CONTENT_TYPE, null); - response.put(BODY, null); - - for (final String method : TestingFramework.ALL_METHODS) { - request.put(METHOD, method); - - framework.addTest(test); - } - framework.runTests(); - } - - @Test - void parameterInPath() throws Exception { - final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter() { - @Override - public Map execute(final String defaultURI, final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - @SuppressWarnings("unchecked") - final Map query = (Map) request.get(QUERY); - Assertions.assertTrue(query.containsKey("stuffParm"), "Parameters appended to the path should have been put in the query."); - - Assertions.assertTrue(query.containsKey("stuffParm2")); - Assertions.assertEquals("stuff", query.get("stuffParm")); - Assertions.assertEquals("stuff2", query.get("stuffParm2")); - - Assertions.assertEquals("/stuff", request.get(PATH)); - return alreadyCheckedResponse(); - } - }; - - final TestingFramework framework = newFrameworkAndSetAdapter(adapter); - - final Map test = new HashMap<>(); - - // Add request. - final Map request = new HashMap<>(); - test.put(REQUEST, request); - - request.put(PATH, "/stuff?stuffParm=stuff&stuffParm2=stuff2"); - - framework.addTest(test); - - framework.runTests(); - } - - @Test - void deepcopyRejectsNonSerializable() { - Assertions.assertThrows(TestingFrameworkException.class, () -> TestingFramework.deepcopy(new Object())); - } - - @Test - void addTestUsesDeepCopyAndLocksExpectations() throws Exception { - final Map test = new HashMap<>(); - final Map request = new HashMap<>(); - request.put(METHOD, "POST"); - request.put(PATH, "copy"); - test.put(REQUEST, request); - - final Map response = new HashMap<>(); - response.put(STATUS, 200); - response.put(BODY, "ok"); - response.put(CONTENT_TYPE, "text/plain"); - final Map headers = new HashMap<>(); - headers.put("X-A", "v1"); - response.put(HEADERS, headers); - test.put(RESPONSE, response); - - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute(final String defaultURI, final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) throws TestingFrameworkException { - Assertions.assertEquals("POST", request.get(METHOD)); - Assertions.assertEquals("copy", request.get(PATH)); - Assertions.assertEquals(200, responseExpectations.get(STATUS)); - Assertions.assertEquals("ok", responseExpectations.get(BODY)); - Assertions.assertEquals("text/plain", responseExpectations.get(CONTENT_TYPE)); - @SuppressWarnings("unchecked") - final Map expectedHeaders = (Map) responseExpectations.get(HEADERS); - Assertions.assertEquals("v1", expectedHeaders.get("X-A")); - Assertions.assertThrows(UnsupportedOperationException.class, () -> responseExpectations.put("x", "y")); - - final Map actual = new HashMap<>(); - actual.put(STATUS, responseExpectations.get(STATUS)); - actual.put(BODY, responseExpectations.get(BODY)); - actual.put(CONTENT_TYPE, responseExpectations.get(CONTENT_TYPE)); - actual.put(HEADERS, responseExpectations.get(HEADERS)); - return actual; - } - }; - - final TestingFramework framework = newWebServerTestingFramework(adapter); - framework.addTest(test); - - request.put(METHOD, "GET"); - response.put(STATUS, 500); - - framework.runTests(); - } - - @Test - void headSkipsBodyAndContentTypeChecks() throws Exception { - final Map test = new HashMap<>(); - final Map request = new HashMap<>(); - request.put(METHOD, "HEAD"); - request.put(PATH, "head"); - test.put(REQUEST, request); - - final Map response = new HashMap<>(); - response.put(STATUS, 200); - response.put(BODY, "ignored"); - response.put(CONTENT_TYPE, "text/plain"); - final Map headers = new HashMap<>(); - headers.put("X-Ok", "yes"); - response.put(HEADERS, headers); - test.put(RESPONSE, response); - - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute(final String defaultURI, final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) { - final Map actual = new HashMap<>(); - actual.put(STATUS, 200); - actual.put(HEADERS, responseExpectations.get(HEADERS)); - return actual; - } - }; - - final TestingFramework framework = newWebServerTestingFramework(adapter); - framework.addTest(test); - framework.runTests(); - } - - @Test - void missingHeaderThrows() throws Exception { - final Map test = new HashMap<>(); - final Map request = new HashMap<>(); - request.put(METHOD, "GET"); - request.put(PATH, "header-check"); - test.put(REQUEST, request); - - final Map response = new HashMap<>(); - response.put(STATUS, 200); - final Map headers = new HashMap<>(); - headers.put("X-Need", "value"); - response.put(HEADERS, headers); - test.put(RESPONSE, response); - - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute(final String defaultURI, final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) { - final Map actual = new HashMap<>(); - actual.put(STATUS, 200); - actual.put(HEADERS, new HashMap()); - return actual; - } - }; - - final TestingFramework framework = newWebServerTestingFramework(adapter); - framework.addTest(test); - Assertions.assertThrows(TestingFrameworkException.class, framework::runTests); - } - - @Test - void nullResponseThrows() throws Exception { - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute(final String defaultURI, final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) { - return null; - } - }; - - final TestingFramework framework = newWebServerTestingFramework(adapter); - framework.addTest(); - Assertions.assertThrows(TestingFrameworkException.class, framework::runTests); - } - - @Test - void nullStatusThrows() throws Exception { - final ClientTestingAdapter adapter = new ClientTestingAdapter() { - @Override - public Map execute(final String defaultURI, final Map request, - final TestingFrameworkRequestHandler requestHandler, - final Map responseExpectations) { - final Map response = new HashMap<>(); - response.put(STATUS, null); - return response; - } - }; - - final TestingFramework framework = newWebServerTestingFramework(adapter); - framework.addTest(); - Assertions.assertThrows(TestingFrameworkException.class, framework::runTests); - } - -} \ No newline at end of file diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestTestingFrameworkRequestHandler.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestTestingFrameworkRequestHandler.java deleted file mode 100644 index f50a34ef72..0000000000 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestTestingFrameworkRequestHandler.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package org.apache.hc.core5.testing.framework; - -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.BODY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.CONTENT_TYPE; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.HEADERS; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.METHOD; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PROTOCOL_VERSION; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.QUERY; -import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.STATUS; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import org.apache.hc.core5.http.ClassicHttpRequest; -import org.apache.hc.core5.http.ClassicHttpResponse; -import org.apache.hc.core5.http.ContentType; -import org.apache.hc.core5.http.HttpException; -import org.apache.hc.core5.http.HttpVersion; -import org.apache.hc.core5.http.Method; -import org.apache.hc.core5.http.io.entity.EntityUtils; -import org.apache.hc.core5.http.io.entity.StringEntity; -import org.apache.hc.core5.http.message.BasicClassicHttpRequest; -import org.apache.hc.core5.http.message.BasicClassicHttpResponse; -import org.apache.hc.core5.http.protocol.HttpContext; -import org.apache.hc.core5.http.protocol.HttpCoreContext; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class TestTestingFrameworkRequestHandler { - @Test - void assertNothingThrown() throws Exception { - final TestingFrameworkRequestHandler handler = new TestingFrameworkRequestHandler() { - - @Override - public void handle(final ClassicHttpRequest request, final ClassicHttpResponse response, final HttpContext context) - throws HttpException, IOException { - } - }; - - handler.assertNothingThrown(); - } - - @Test - void assertNothingThrownThrows() throws Exception { - final String errorMessage = "thrown intentionally"; - - final TestingFrameworkRequestHandler handler = new TestingFrameworkRequestHandler() { - - @Override - public void handle(final ClassicHttpRequest request, final ClassicHttpResponse response, final HttpContext context) - throws HttpException, IOException { - thrown = new TestingFrameworkException(errorMessage); - } - }; - - handler.handle(null, null, null); - final TestingFrameworkException exception = Assertions.assertThrows(TestingFrameworkException.class, - () -> handler.assertNothingThrown()); - Assertions.assertEquals(errorMessage, exception.getMessage(), "Unexpected message"); - // a second call should not throw - handler.assertNothingThrown(); - } - - @Test - void handleValidatesRequestAndBuildsResponse() throws Exception { - final TestingFrameworkRequestHandler handler = new TestingFrameworkRequestHandler(); - - final Map requestExpectations = new HashMap<>(); - requestExpectations.put(METHOD, Method.POST.name()); - requestExpectations.put(BODY, "ping"); - requestExpectations.put(CONTENT_TYPE, ContentType.TEXT_PLAIN.toString()); - requestExpectations.put(PROTOCOL_VERSION, HttpVersion.HTTP_1_1); - final Map query = new HashMap<>(); - query.put("q", "1"); - query.put("r", "2"); - requestExpectations.put(QUERY, query); - final Map headers = new HashMap<>(); - headers.put("X-Test", "value"); - requestExpectations.put(HEADERS, headers); - handler.setRequestExpectations(requestExpectations); - - final Map desiredResponse = new HashMap<>(); - desiredResponse.put(STATUS, 201); - desiredResponse.put(BODY, "pong"); - desiredResponse.put(CONTENT_TYPE, ContentType.TEXT_PLAIN.toString()); - final Map responseHeaders = new HashMap<>(); - responseHeaders.put("X-Reply", "ok"); - desiredResponse.put(HEADERS, responseHeaders); - handler.setDesiredResponse(desiredResponse); - - final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.POST, "/echo?q=1&r=2"); - request.setVersion(HttpVersion.HTTP_1_1); - request.addHeader("X-Test", "value"); - request.setEntity(new StringEntity("ping", ContentType.TEXT_PLAIN)); - final ClassicHttpResponse response = new BasicClassicHttpResponse(200); - - handler.handle(request, response, HttpCoreContext.create()); - handler.assertNothingThrown(); - - Assertions.assertEquals(201, response.getCode()); - Assertions.assertEquals("pong", EntityUtils.toString(response.getEntity(), 1024)); - Assertions.assertTrue(response.getEntity().getContentType().contains("text/plain")); - Assertions.assertEquals("ok", response.getFirstHeader("X-Reply").getValue()); - } - - @Test - void handleStoresThrowableOnMismatch() throws Exception { - final TestingFrameworkRequestHandler handler = new TestingFrameworkRequestHandler(); - - final Map requestExpectations = new HashMap<>(); - requestExpectations.put(METHOD, Method.GET.name()); - final Map headers = new HashMap<>(); - headers.put("X-Needed", "true"); - requestExpectations.put(HEADERS, headers); - handler.setRequestExpectations(requestExpectations); - - final Map desiredResponse = new HashMap<>(); - desiredResponse.put(STATUS, 200); - handler.setDesiredResponse(desiredResponse); - - final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/"); - final ClassicHttpResponse response = new BasicClassicHttpResponse(200); - - handler.handle(request, response, HttpCoreContext.create()); - Assertions.assertThrows(TestingFrameworkException.class, handler::assertNothingThrown); - } - -} diff --git a/pom.xml b/pom.xml index a6439c9fcd..6340fc527e 100644 --- a/pom.xml +++ b/pom.xml @@ -273,6 +273,7 @@ @org.apache.hc.core5.annotation.Internal org.apache.hc.core5.testing.reactive.ReactiveTestUtils + org.apache.hc.core5.testing.framework.* true false @@ -420,6 +421,7 @@ @org.apache.hc.core5.annotation.Internal org.apache.hc.core5.testing.reactive.ReactiveTestUtils + org.apache.hc.core5.testing.framework.* true