io.restassured.internal.http.HttpResponseDecorator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-assured Show documentation
Show all versions of rest-assured Show documentation
Java DSL for easy testing of REST services
/*
* Copyright 2016 the original author or authors.
*
* Licensed 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.
*/
package io.restassured.internal.http;
import org.apache.http.*;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import java.util.Iterator;
import java.util.Locale;
/**
* This class is a wrapper for {@link HttpResponse}, which allows for
* simplified header access, as well as carrying the auto-parsed response data.
* (see {@link HTTPBuilder#parseResponse(HttpResponse, Object)}).
*
* @see HeadersDecorator
* @author Tom Nichols
* @since 0.5.0
*/
public class HttpResponseDecorator implements HttpResponse {
HeadersDecorator headers = null;
HttpResponse responseBase;
HttpContextDecorator context;
Object responseData;
public HttpResponseDecorator( HttpResponse base, Object parsedResponse ) {
this( base, null, parsedResponse );
}
public HttpResponseDecorator( HttpResponse base, HttpContextDecorator context, Object parsedResponse ) {
this.responseBase = base;
this.context = context;
this.responseData = parsedResponse;
}
/**
* Return a {@link HeadersDecorator}, which provides a more Groovy API for
* accessing response headers.
* @return the headers for this response
*/
public HeadersDecorator getHeaders() {
if ( headers == null ) headers = new HeadersDecorator();
return headers;
}
/**
* Quickly determine if the request resulted in an error code.
* @return true if the response code is within the range of
* {@link Status#SUCCESS}
*/
public boolean isSuccess() {
return Status.find( getStatus() ) == Status.SUCCESS;
}
/**
* Get the response status code.
* @see StatusLine#getStatusCode()
* @return the HTTP response code.
*/
public int getStatus() {
return responseBase.getStatusLine().getStatusCode();
}
/**
* Get the content-type for this response.
* @see HttpResponseContentTypeFinder#findContentType(HttpResponse)
* @return the content-type string, without any charset information.
*/
public String getContentType() {
return HttpResponseContentTypeFinder.findContentType(responseBase);
}
/**
* Return the parsed data from this response body.
* @return the parsed response object, or null
if the response
* does not contain any data.
*/
public Object getData() { return this.responseData; }
void setData( Object responseData ) { this.responseData = responseData; }
/**
* Get the execution context used during this request
* @see ExecutionContext
* @return the {@link HttpContext}
*/
public HttpContextDecorator getContext() { return this.context; }
/**
* This class is returned by {@link HttpResponseDecorator#getHeaders()}.
* It provides three "Groovy" ways to access headers:
*
* - Bracket notation
resp.headers['Content-Type']
* returns the {@link Header} instance
* - Property notation
resp.headers.'Content-Type'
* returns the {@link Header#getValue() header value}
* - Iterator methods
- Iterates over each Header:
*
resp.headers.each {
* println "${it.name} : ${it.value}"
* }
*
* @author Tom Nichols
* @since 0.5.0
*/
public final class HeadersDecorator implements Iterable