org.mockserver.model
Class HttpResponse

java.lang.Object
  extended by org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString
      extended by org.mockserver.model.ObjectWithJsonToString
          extended by org.mockserver.model.Action
              extended by org.mockserver.model.HttpResponse

public class HttpResponse
extends Action

Author:
jamesdbloom

Nested Class Summary
 
Nested classes/interfaces inherited from class org.mockserver.model.Action
Action.Type
 
Field Summary
 
Fields inherited from class org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString
logger
 
Constructor Summary
HttpResponse()
           
 
Method Summary
 HttpResponse applyDelay()
           
 Body getBody()
           
 String getBodyAsString()
           
 ConnectionOptions getConnectionOptions()
           
 List<Cookie> getCookies()
           
 Delay getDelay()
           
 String getFirstHeader(String name)
           
 List<String> getHeader(String name)
           
 List<Header> getHeaders()
           
 Integer getStatusCode()
           
 Action.Type getType()
           
static HttpResponse notFoundResponse()
           
static HttpResponse response()
           
static HttpResponse response(String body)
           
 HttpResponse updateHeader(Header header)
          Update header to return as a Header object, if a header with the same name already exists it will be modified
 HttpResponse updateHeader(String name, String... values)
          Update header to return as a Header object, if a header with the same name already exists it will be modified
 HttpResponse withBody(Body body)
          Set the body to return for example:

string body: - exact("

a simple string body
");

or

- new StringBody("

a simple string body
")

binary body: - binary(IOUtils.readFully(getClass().getClassLoader().getResourceAsStream("example.pdf"), 1024));

or

- new BinaryBody(IOUtils.readFully(getClass().getClassLoader().getResourceAsStream("example.pdf"), 1024));

 HttpResponse withBody(byte[] body)
          Set response body to return as binary such as a pdf or image
 HttpResponse withBody(String body)
          Set response body to return as a string response body.
 HttpResponse withBody(String body, Charset charset)
          Set response body to return a string response body with the specified encoding.
 HttpResponse withConnectionOptions(ConnectionOptions connectionOptions)
          The connection options for override the default connection behaviour, this allows full control of headers such as "Connection" or "Content-Length" or controlling whether the socket is closed after the response has been sent
 HttpResponse withCookie(Cookie cookie)
          Add cookie to return as Set-Cookie header
 HttpResponse withCookie(String name, String value)
          Add cookie to return as Set-Cookie header
 HttpResponse withCookies(Cookie... cookies)
          The cookies to return as Set-Cookie headers as a varargs of Cookie objects
 HttpResponse withCookies(List<Cookie> cookies)
          The cookies to return as Set-Cookie headers as a list of Cookie objects
 HttpResponse withDelay(Delay delay)
          The delay before responding with this request as a Delay object, for example new Delay(TimeUnit.SECONDS, 3)
 HttpResponse withDelay(TimeUnit timeUnit, long value)
          The delay before responding with this request as a Delay object, for example new Delay(TimeUnit.SECONDS, 3)
 HttpResponse withHeader(Header header)
          Add a header to return as a Header object, if a header with the same name already exists this will NOT be modified but two headers will exist
 HttpResponse withHeader(String name, String... values)
          Add a header to return as a Header object, if a header with the same name already exists this will NOT be modified but two headers will exist
 HttpResponse withHeaders(Header... headers)
          The headers to return as a varargs of Header objects
 HttpResponse withHeaders(List<Header> headers)
          The headers to return as a list of Header objects
 HttpResponse withStatusCode(Integer statusCode)
          The status code to return, such as 200, 404, the status code specified here will result in the default status message for this status code for example for 200 the status message "OK" is used
 
Methods inherited from class org.mockserver.model.ObjectWithJsonToString
toString
 
Methods inherited from class org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString
addFieldsExcludedFromEqualsAndHashCode, equals, fieldsExcludedFromEqualsAndHashCode, hashCode
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HttpResponse

public HttpResponse()
Method Detail

response

public static HttpResponse response()

response

public static HttpResponse response(String body)

notFoundResponse

public static HttpResponse notFoundResponse()

withStatusCode

public HttpResponse withStatusCode(Integer statusCode)
The status code to return, such as 200, 404, the status code specified here will result in the default status message for this status code for example for 200 the status message "OK" is used

Parameters:
statusCode - an integer such as 200 or 404

getStatusCode

public Integer getStatusCode()

withBody

public HttpResponse withBody(String body)
Set response body to return as a string response body. The character set will be determined by the Content-Type header on the response. To force the character set, use withBody(String, Charset).

Parameters:
body - a string

withBody

public HttpResponse withBody(String body,
                             Charset charset)
Set response body to return a string response body with the specified encoding. Note: The character set of the response will be forced to the specified charset, even if the Content-Type header specifies otherwise.

Parameters:
body - a string
charset - character set the string will be encoded in

withBody

public HttpResponse withBody(byte[] body)
Set response body to return as binary such as a pdf or image

Parameters:
body - a byte array

withBody

public HttpResponse withBody(Body body)
Set the body to return for example:

string body: - exact("

a simple string body
");

or

- new StringBody("

a simple string body
")

binary body: - binary(IOUtils.readFully(getClass().getClassLoader().getResourceAsStream("example.pdf"), 1024));

or

- new BinaryBody(IOUtils.readFully(getClass().getClassLoader().getResourceAsStream("example.pdf"), 1024));

Parameters:
body - an instance of one of the Body subclasses including StringBody or BinaryBody

getBody

public Body getBody()

getBodyAsString

public String getBodyAsString()

withHeaders

public HttpResponse withHeaders(List<Header> headers)
The headers to return as a list of Header objects

Parameters:
headers - a list of Header objects

withHeaders

public HttpResponse withHeaders(Header... headers)
The headers to return as a varargs of Header objects

Parameters:
headers - a varargs of Header objects

withHeader

public HttpResponse withHeader(Header header)
Add a header to return as a Header object, if a header with the same name already exists this will NOT be modified but two headers will exist

Parameters:
header - a Header objects

withHeader

public HttpResponse withHeader(String name,
                               String... values)
Add a header to return as a Header object, if a header with the same name already exists this will NOT be modified but two headers will exist

Parameters:
name - the header name
values - the header values which can be a varags of strings or regular expressions

updateHeader

public HttpResponse updateHeader(Header header)
Update header to return as a Header object, if a header with the same name already exists it will be modified

Parameters:
header - a Header objects

updateHeader

public HttpResponse updateHeader(String name,
                                 String... values)
Update header to return as a Header object, if a header with the same name already exists it will be modified

Parameters:
name - the header name
values - the header values which can be a varags of strings or regular expressions

getHeaders

public List<Header> getHeaders()

getHeader

public List<String> getHeader(String name)

getFirstHeader

public String getFirstHeader(String name)

withCookies

public HttpResponse withCookies(List<Cookie> cookies)
The cookies to return as Set-Cookie headers as a list of Cookie objects

Parameters:
cookies - a list of Cookie objects

withCookies

public HttpResponse withCookies(Cookie... cookies)
The cookies to return as Set-Cookie headers as a varargs of Cookie objects

Parameters:
cookies - a varargs of Cookie objects

withCookie

public HttpResponse withCookie(Cookie cookie)
Add cookie to return as Set-Cookie header

Parameters:
cookie - a Cookie object

withCookie

public HttpResponse withCookie(String name,
                               String value)
Add cookie to return as Set-Cookie header

Parameters:
name - the cookies name
value - the cookies value which can be a string or regular expression

getCookies

public List<Cookie> getCookies()

withDelay

public HttpResponse withDelay(Delay delay)
The delay before responding with this request as a Delay object, for example new Delay(TimeUnit.SECONDS, 3)

Parameters:
delay - a Delay object, for example new Delay(TimeUnit.SECONDS, 3)

withDelay

public HttpResponse withDelay(TimeUnit timeUnit,
                              long value)
The delay before responding with this request as a Delay object, for example new Delay(TimeUnit.SECONDS, 3)

Parameters:
timeUnit - a the time unit, for example TimeUnit.SECONDS
value - a the number of time units to delay the response

getDelay

public Delay getDelay()

withConnectionOptions

public HttpResponse withConnectionOptions(ConnectionOptions connectionOptions)
The connection options for override the default connection behaviour, this allows full control of headers such as "Connection" or "Content-Length" or controlling whether the socket is closed after the response has been sent

Parameters:
connectionOptions - the connection options for override the default connection behaviour

getConnectionOptions

public ConnectionOptions getConnectionOptions()

applyDelay

public HttpResponse applyDelay()

getType

public Action.Type getType()
Specified by:
getType in class Action


Copyright © 2015. All rights reserved.