Package net.jadler.stubbing
Class Stubbing
- java.lang.Object
-
- net.jadler.AbstractRequestMatching<RequestStubbing>
-
- net.jadler.stubbing.Stubbing
-
- All Implemented Interfaces:
RequestMatching<RequestStubbing>,RequestStubbing,ResponseStubbing
public class Stubbing extends AbstractRequestMatching<RequestStubbing> implements RequestStubbing, ResponseStubbing
Internal class for defining http stubs in a fluid fashion. You shouldn't create instances of this class on your own, please seeJadler.onRequest()for more information on creating instances of this class.
-
-
Field Summary
-
Fields inherited from class net.jadler.AbstractRequestMatching
predicates
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description HttpStubcreateRule()Creates aHttpStubinstance from this Stubbing instance.ResponseStubbingrespond()Finishes the WHEN part of this stubbing and starts the THEN part.voidrespondUsing(Responder responder)Finishes the WHEN part of this stubbing and allows to define the THEN part in a dynamic way.ResponseStubbingthenRespond()Starts a definition of a subsequent stub response.ResponseStubbingwithBody(byte[] responseBody)Sets the stub http response body as an array of bytes.ResponseStubbingwithBody(InputStream is)Sets the stub http response body as the content of the given input stream.ResponseStubbingwithBody(Reader reader)Sets the stub http response body as the content of the given reader.ResponseStubbingwithBody(String responseBody)Sets the stub http response body as a string.ResponseStubbingwithContentType(String contentType)Sets the content type of the http stub response.ResponseStubbingwithDelay(long delayValue, TimeUnit delayUnit)Sets the response delay.ResponseStubbingwithEncoding(Charset encoding)Sets the character encoding of the http stub response.ResponseStubbingwithHeader(String name, String value)Adds a stub http response header.ResponseStubbingwithStatus(int status)Sets the http stub response status.-
Methods inherited from class net.jadler.AbstractRequestMatching
havingBody, havingBodyEqualTo, havingHeader, havingHeader, havingHeaderEqualTo, havingHeaders, havingMethod, havingMethodEqualTo, havingParameter, havingParameter, havingParameterEqualTo, havingParameters, havingPath, havingPathEqualTo, havingQueryString, havingQueryStringEqualTo, havingRawBodyEqualTo, that
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.jadler.RequestMatching
havingBody, havingBodyEqualTo, havingHeader, havingHeader, havingHeaderEqualTo, havingHeaders, havingMethod, havingMethodEqualTo, havingParameter, havingParameter, havingParameterEqualTo, havingParameters, havingPath, havingPathEqualTo, havingQueryString, havingQueryStringEqualTo, havingRawBodyEqualTo, that
-
-
-
-
Method Detail
-
respond
public ResponseStubbing respond()
Finishes the WHEN part of this stubbing and starts the THEN part.- Specified by:
respondin interfaceRequestStubbing- Returns:
- response stubbing instance to continue this stubbing
-
thenRespond
public ResponseStubbing thenRespond()
Starts a definition of a subsequent stub response.- Specified by:
thenRespondin interfaceResponseStubbing- Returns:
- this ongoing stubbing
-
respondUsing
public void respondUsing(Responder responder)
Finishes the WHEN part of this stubbing and allows to define the THEN part in a dynamic way.- Specified by:
respondUsingin interfaceRequestStubbing- Parameters:
responder-Responderinstance (usually in a form of an anonymous inner class) which dynamically creates an http response to be returned when an incoming http request matches the WHEN part.
-
withContentType
public ResponseStubbing withContentType(String contentType)
Sets the content type of the http stub response. Calling this method overrides any previous calls. The content type information is communicated via the http Content-Type response header.This method either adds this header or overwrites any existing Content-Type header ensuring at most one such header will be present in the stub response.
- Specified by:
withContentTypein interfaceResponseStubbing- Parameters:
contentType- response content type- Returns:
- this ongoing stubbing
-
withEncoding
public ResponseStubbing withEncoding(Charset encoding)
Sets the character encoding of the http stub response. Calling this method overrides any previous calls or the default encoding (set byJadler.OngoingConfiguration.respondsWithDefaultEncoding(java.nio.charset.Charset)).Please note this method doesn't set the Content-Type header charset part,
ResponseStubbing.withContentType(java.lang.String)must be called to do so. You can even set different stub response body encoding and Content-Type if your testing scenario requires it.- Specified by:
withEncodingin interfaceResponseStubbing- Parameters:
encoding- response body encoding- Returns:
- this ongoing stubbing
-
withBody
public ResponseStubbing withBody(String responseBody)
Sets the stub http response body as a string. This string will be encoded using an encoding set byResponseStubbing.withEncoding(java.nio.charset.Charset)Calling this method overrides any previous calls of this or any other withBody method.- Specified by:
withBodyin interfaceResponseStubbing- Parameters:
responseBody- response body- Returns:
- this ongoing stubbing
-
withBody
public ResponseStubbing withBody(Reader reader)
Sets the stub http response body as the content of the given reader. The string retrieved from the given reader will be encoded using an encoding set byResponseStubbing.withEncoding(java.nio.charset.Charset)Calling this method overrides any previous calls of this or any other withBody method.- Specified by:
withBodyin interfaceResponseStubbing- Parameters:
reader- response body source- Returns:
- this ongoing stubbing
-
withBody
public ResponseStubbing withBody(InputStream is)
Sets the stub http response body as the content of the given input stream. The source input stream is copied to the stub response body as-is, it is not affected by the encoding set byResponseStubbing.withEncoding(java.nio.charset.Charset)in any way. Calling this method overrides any previous calls of this or any other withBody method.- Specified by:
withBodyin interfaceResponseStubbing- Parameters:
is- response body source- Returns:
- this ongoing stubbing
-
withBody
public ResponseStubbing withBody(byte[] responseBody)
Sets the stub http response body as an array of bytes. The given array of bytes is used as the stub response body as-is, it is not affected by the encoding set byResponseStubbing.withEncoding(java.nio.charset.Charset)in any way. Calling this method overrides any previous calls of this or any other withBody method.- Specified by:
withBodyin interfaceResponseStubbing- Parameters:
responseBody- response body- Returns:
- this ongoing stubbing
-
withHeader
public ResponseStubbing withHeader(String name, String value)
Adds a stub http response header. This method can be called multiple times for the same header name. The response will contain a header with all (multiple) values.- Specified by:
withHeaderin interfaceResponseStubbing- Parameters:
name- header namevalue- header value- Returns:
- this ongoing stubbing
-
withStatus
public ResponseStubbing withStatus(int status)
Sets the http stub response status. Calling this method overrides any previous calls or the default status (set byJadler.OngoingConfiguration.respondsWithDefaultStatus(int)).- Specified by:
withStatusin interfaceResponseStubbing- Parameters:
status- http status code- Returns:
- this ongoing stubbing
-
withDelay
public ResponseStubbing withDelay(long delayValue, TimeUnit delayUnit)
Sets the response delay. The stub http response is returned after the specified amount of time. Calling this method overrides any previous calls of this method.- Specified by:
withDelayin interfaceResponseStubbing- Parameters:
delayValue- a delay (in units defined by thedelayUnitparameter) this stub response will be returned afterdelayUnit- unit of the delay parameter- Returns:
- this ongoing stubbing
-
-