org.apache.hc.core5.testing.framework.ClientPOJOAdapter Maven / Gradle / Ivy
Show all versions of httpcore5-testing Show documentation
/*
* ====================================================================
* 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;
}
}