Class MockLowLevelHttpResponse
- java.lang.Object
-
- com.google.api.client.http.LowLevelHttpResponse
-
- com.google.api.client.testing.http.MockLowLevelHttpResponse
-
@Beta public class MockLowLevelHttpResponse extends LowLevelHttpResponse
- Since:
- 1.3
-
-
Field Summary
Fields Modifier and Type Field Description private java.io.InputStreamcontentInput stream content of HTTP response ornullby default.private java.lang.StringcontentEncodingContent encoding ornullfor none.private longcontentLengthContent length or-1if unknown.private java.lang.StringcontentTypeContent type of HTTP response ornullby default.private java.util.List<java.lang.String>headerNamesList of header names of HTTP response (empty array list by default).private java.util.List<java.lang.String>headerValuesList of header values of HTTP response (empty array list by default).private booleanisDisconnectedWhetherdisconnect()has been called.private java.lang.StringreasonPhraseHTTP reason phrase ornullfor none.private intstatusCodeStatus code of HTTP response or200by default.
-
Constructor Summary
Constructors Constructor Description MockLowLevelHttpResponse()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MockLowLevelHttpResponseaddHeader(java.lang.String name, java.lang.String value)Adds a header to the response.voiddisconnect()Default implementation does nothing, but subclasses may override to attempt to abort the connection or release allocated system resources for this connection.java.io.InputStreamgetContent()Returns the HTTP response content input stream ornullfor none.java.lang.StringgetContentEncoding()Returns the content encoding (for example"gzip") ornullfor none.longgetContentLength()Returns the content length or0for none.java.lang.StringgetContentType()Returns the content type ornullfor none.intgetHeaderCount()Returns the number of HTTP response headers.java.lang.StringgetHeaderName(int index)Returns the HTTP response header name at the given zero-based index.java.util.List<java.lang.String>getHeaderNames()Returns the list of header names of HTTP response.java.lang.StringgetHeaderValue(int index)Returns the HTTP response header value at the given zero-based index.java.util.List<java.lang.String>getHeaderValues()Returns the list of header values of HTTP response.java.lang.StringgetReasonPhrase()Returns the HTTP reason phrase ornullfor none.intgetStatusCode()Returns the response status code or<=0for none.java.lang.StringgetStatusLine()Returns the response status line ornullfor none.booleanisDisconnected()Returns whetherdisconnect()has been called.MockLowLevelHttpResponsesetContent(byte[] byteContent)Sets the response content to the given byte array.MockLowLevelHttpResponsesetContent(java.io.InputStream content)Sets the input stream content of HTTP response ornullfor none.MockLowLevelHttpResponsesetContent(java.lang.String stringContent)Sets the response content to the given content string.MockLowLevelHttpResponsesetContentEncoding(java.lang.String contentEncoding)Sets the content encoding ornullfor none.MockLowLevelHttpResponsesetContentLength(long contentLength)Sets the content length or-1for unknown.MockLowLevelHttpResponsesetContentType(java.lang.String contentType)Sets the content type of HTTP response ornullfor none.MockLowLevelHttpResponsesetHeaderNames(java.util.List<java.lang.String> headerNames)Sets the list of header names of HTTP response.MockLowLevelHttpResponsesetHeaderValues(java.util.List<java.lang.String> headerValues)Sets the list of header values of HTTP response.MockLowLevelHttpResponsesetReasonPhrase(java.lang.String reasonPhrase)Sets the HTTP reason phrase ornullfor none.MockLowLevelHttpResponsesetStatusCode(int statusCode)Sets the status code of HTTP response.MockLowLevelHttpResponsesetZeroContent()Sets the content tonulland the content length to 0.
-
-
-
Field Detail
-
content
private java.io.InputStream content
Input stream content of HTTP response ornullby default.
-
contentType
private java.lang.String contentType
Content type of HTTP response ornullby default.
-
statusCode
private int statusCode
Status code of HTTP response or200by default.
-
reasonPhrase
private java.lang.String reasonPhrase
HTTP reason phrase ornullfor none.
-
headerNames
private java.util.List<java.lang.String> headerNames
List of header names of HTTP response (empty array list by default).
-
headerValues
private java.util.List<java.lang.String> headerValues
List of header values of HTTP response (empty array list by default).
-
contentEncoding
private java.lang.String contentEncoding
Content encoding ornullfor none.
-
contentLength
private long contentLength
Content length or-1if unknown.
-
isDisconnected
private boolean isDisconnected
Whetherdisconnect()has been called.
-
-
Method Detail
-
addHeader
public MockLowLevelHttpResponse addHeader(java.lang.String name, java.lang.String value)
Adds a header to the response.- Parameters:
name- header namevalue- header value
-
setContent
public MockLowLevelHttpResponse setContent(java.lang.String stringContent)
Sets the response content to the given content string.If the input string is
null, it will set the content tonull. Else, it will useTestableByteArrayInputStreamwith the UTF-8 encoded string content.- Parameters:
stringContent- content string ornullfor none
-
setContent
public MockLowLevelHttpResponse setContent(byte[] byteContent)
Sets the response content to the given byte array.- Parameters:
byteContent- content byte array, ornullfor none.If the byte array is
null, the method invokessetZeroContent(). Otherwise,byteContentis wrapped in aTestableByteArrayInputStreamand becomes thisMockLowLevelHttpResponse's contents.- Since:
- 1.18
-
setZeroContent
public MockLowLevelHttpResponse setZeroContent()
Sets the content tonulland the content length to 0. Note that the result will have a content length header whose value is 0.- Since:
- 1.18
-
getContent
public java.io.InputStream getContent() throws java.io.IOExceptionDescription copied from class:LowLevelHttpResponseReturns the HTTP response content input stream ornullfor none.- Specified by:
getContentin classLowLevelHttpResponse- Throws:
java.io.IOException- I/O exception
-
getContentEncoding
public java.lang.String getContentEncoding()
Description copied from class:LowLevelHttpResponseReturns the content encoding (for example"gzip") ornullfor none.- Specified by:
getContentEncodingin classLowLevelHttpResponse
-
getContentLength
public long getContentLength()
Description copied from class:LowLevelHttpResponseReturns the content length or0for none.- Specified by:
getContentLengthin classLowLevelHttpResponse
-
getContentType
public final java.lang.String getContentType()
Description copied from class:LowLevelHttpResponseReturns the content type ornullfor none.- Specified by:
getContentTypein classLowLevelHttpResponse
-
getHeaderCount
public int getHeaderCount()
Description copied from class:LowLevelHttpResponseReturns the number of HTTP response headers.Note that multiple headers of the same name need to be supported, in which case each header value is treated as a separate header.
- Specified by:
getHeaderCountin classLowLevelHttpResponse
-
getHeaderName
public java.lang.String getHeaderName(int index)
Description copied from class:LowLevelHttpResponseReturns the HTTP response header name at the given zero-based index.- Specified by:
getHeaderNamein classLowLevelHttpResponse
-
getHeaderValue
public java.lang.String getHeaderValue(int index)
Description copied from class:LowLevelHttpResponseReturns the HTTP response header value at the given zero-based index.- Specified by:
getHeaderValuein classLowLevelHttpResponse
-
getReasonPhrase
public java.lang.String getReasonPhrase()
Description copied from class:LowLevelHttpResponseReturns the HTTP reason phrase ornullfor none.- Specified by:
getReasonPhrasein classLowLevelHttpResponse
-
getStatusCode
public int getStatusCode()
Description copied from class:LowLevelHttpResponseReturns the response status code or<=0for none.- Specified by:
getStatusCodein classLowLevelHttpResponse
-
getStatusLine
public java.lang.String getStatusLine()
Description copied from class:LowLevelHttpResponseReturns the response status line ornullfor none.- Specified by:
getStatusLinein classLowLevelHttpResponse
-
getHeaderNames
public final java.util.List<java.lang.String> getHeaderNames()
Returns the list of header names of HTTP response.- Since:
- 1.5
-
setHeaderNames
public MockLowLevelHttpResponse setHeaderNames(java.util.List<java.lang.String> headerNames)
Sets the list of header names of HTTP response.Default value is an empty list.
- Since:
- 1.5
-
getHeaderValues
public final java.util.List<java.lang.String> getHeaderValues()
Returns the list of header values of HTTP response.Default value is an empty list.
- Since:
- 1.5
-
setHeaderValues
public MockLowLevelHttpResponse setHeaderValues(java.util.List<java.lang.String> headerValues)
Sets the list of header values of HTTP response.- Since:
- 1.5
-
setContent
public MockLowLevelHttpResponse setContent(java.io.InputStream content)
Sets the input stream content of HTTP response ornullfor none.- Since:
- 1.5
-
setContentType
public MockLowLevelHttpResponse setContentType(java.lang.String contentType)
Sets the content type of HTTP response ornullfor none.- Since:
- 1.5
-
setContentEncoding
public MockLowLevelHttpResponse setContentEncoding(java.lang.String contentEncoding)
Sets the content encoding ornullfor none.- Since:
- 1.5
-
setContentLength
public MockLowLevelHttpResponse setContentLength(long contentLength)
Sets the content length or-1for unknown.By default it is
-1.- Since:
- 1.5
-
setStatusCode
public MockLowLevelHttpResponse setStatusCode(int statusCode)
Sets the status code of HTTP response.Default value is
200.- Since:
- 1.5
-
setReasonPhrase
public MockLowLevelHttpResponse setReasonPhrase(java.lang.String reasonPhrase)
Sets the HTTP reason phrase ornullfor none.- Since:
- 1.6
-
disconnect
public void disconnect() throws java.io.IOExceptionDescription copied from class:LowLevelHttpResponseDefault implementation does nothing, but subclasses may override to attempt to abort the connection or release allocated system resources for this connection.- Overrides:
disconnectin classLowLevelHttpResponse- Throws:
java.io.IOException- I/O exception
-
isDisconnected
public boolean isDisconnected()
Returns whetherdisconnect()has been called.- Since:
- 1.14
-
-