in httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java [111:253]
public void handle(final ClassicHttpRequest request, final ClassicHttpResponse response, final HttpContext context)
throws HttpException, IOException {
try {
/*
* Check the method against the method in the requestExpectations.
*/
final String actualMethod = request.getMethod();
final String expectedMethod = (String) requestExpectations.get(METHOD);
if (! actualMethod.equals(expectedMethod)) {
throw new TestingFrameworkException("Method not expected. " +
" expected=" + expectedMethod + "; actual=" + actualMethod);
}
/*
* Set the status to the status that is in the desiredResponse.
*/
final Object desiredStatus = desiredResponse.get(STATUS);
if (desiredStatus != null) {
response.setCode((int) desiredStatus);
}
/*
* Check the query parameters against the parameters in requestExpectations.
*/
@SuppressWarnings("unchecked")
final Map<String, String> expectedQuery = (Map<String, String>) requestExpectations.get(QUERY);
if (expectedQuery != null) {
final URI uri = request.getUri();
final URIBuilder uriBuilder = new URIBuilder(uri, StandardCharsets.UTF_8);
final List<NameValuePair> actualParams = uriBuilder.getQueryParams();
final Map<String, String> actualParamsMap = new HashMap<>();
for (final NameValuePair actualParam : actualParams) {
actualParamsMap.put(actualParam.getName(), actualParam.getValue());
}
for (final Map.Entry<String, String> expectedParam : expectedQuery.entrySet()) {
final String key = expectedParam.getKey();
if (! actualParamsMap.containsKey(key)) {
throw new TestingFrameworkException("Expected parameter not found: " + key);
}
final String actualParamValue = actualParamsMap.get(key);
final String expectedParamValue = expectedParam.getValue();
if (! actualParamValue.equals(expectedParamValue)) {
throw new TestingFrameworkException("Expected parameter value not found. " +
" Parameter=" + key + "; expected=" + expectedParamValue + "; actual=" + actualParamValue);
}
}
}
/*
* Check the headers against the headers in requestExpectations.
*/
@SuppressWarnings("unchecked")
final Map<String, String> expectedHeaders = (Map<String, String>) requestExpectations.get(HEADERS);
if (expectedHeaders != null) {
final Map<String, String> actualHeadersMap = new HashMap<>();
final Header[] actualHeaders = request.getHeaders();
for (final Header header : actualHeaders) {
actualHeadersMap.put(header.getName(), header.getValue());
}
for (final Entry<String, String> expectedHeader : expectedHeaders.entrySet()) {
final String key = expectedHeader.getKey();
if (! actualHeadersMap.containsKey(key)) {
throw new TestingFrameworkException("Expected header not found: " + key);
}
final String actualHeaderValue = actualHeadersMap.get(key);
final String expectedHeaderValue = expectedHeader.getValue();
if (! actualHeaderValue.equals(expectedHeaderValue)) {
throw new TestingFrameworkException("Expected header value not found. " +
" Name=" + key + "; expected=" + expectedHeaderValue + "; actual=" + actualHeaderValue);
}
}
}
/*
* Check the body.
*/
final String expectedBody = (String) requestExpectations.get(BODY);
if (expectedBody != null) {
final HttpEntity entity = request.getEntity();
final String data = EntityUtils.toString(entity);
if (! data.equals(expectedBody)) {
throw new TestingFrameworkException("Expected body not found. " +
" Body=" + data + "; expected=" + expectedBody);
}
}
/*
* Check the contentType of the request.
*/
final String requestContentType = (String) requestExpectations.get(CONTENT_TYPE);
if (requestContentType != null) {
final HttpEntity entity = request.getEntity();
final String contentType = entity.getContentType();
final String expectedContentType = (String) requestExpectations.get(CONTENT_TYPE);
if (! contentType.equals(expectedContentType)) {
throw new TestingFrameworkException("Expected request content type not found. " +
" Content Type=" + contentType + "; expected=" + expectedContentType);
}
}
/*
* Check the protocolVersion.
*/
if (requestExpectations.containsKey(PROTOCOL_VERSION)) {
final ProtocolVersion protocolVersion = request.getVersion();
final ProtocolVersion expectedProtocolVersion = (ProtocolVersion) requestExpectations.get(PROTOCOL_VERSION);
if (! protocolVersion.equals(expectedProtocolVersion)) {
throw new TestingFrameworkException("Expected request protocol version not found. " +
" Protocol Version=" + protocolVersion + "; expected=" + expectedProtocolVersion);
}
}
/*
* Return the body in desiredResponse using the contentType in desiredResponse.
*/
final String desiredBody = (String) desiredResponse.get(BODY);
if (desiredBody != null) {
final String desiredContentType = (String) desiredResponse.get(CONTENT_TYPE);
final StringEntity entity = desiredContentType != null ?
new StringEntity(desiredBody, ContentType.parse(desiredContentType)) :
new StringEntity(desiredBody);
response.setEntity(entity);
}
/*
* Return the headers in desiredResponse.
*/
@SuppressWarnings("unchecked")
final Map<String, String> desiredHeaders = (Map<String, String>) desiredResponse.get(HEADERS);
if (desiredHeaders != null) {
for (final Entry<String, String> entry : desiredHeaders.entrySet()) {
response.setHeader(entry.getKey(), entry.getValue());
}
}
} catch (final Throwable t) {
/*
* Save the throwable to be later retrieved by a call to assertNothingThrown().
*/
thrown = t;
}
}