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.InputStream
content
Input stream content of HTTP response ornull
by default.private java.lang.String
contentEncoding
Content encoding ornull
for none.private long
contentLength
Content length or-1
if unknown.private java.lang.String
contentType
Content type of HTTP response ornull
by default.private java.util.List<java.lang.String>
headerNames
List of header names of HTTP response (empty array list by default).private java.util.List<java.lang.String>
headerValues
List of header values of HTTP response (empty array list by default).private boolean
isDisconnected
Whetherdisconnect()
has been called.private java.lang.String
reasonPhrase
HTTP reason phrase ornull
for none.private int
statusCode
Status code of HTTP response or200
by default.
-
Constructor Summary
Constructors Constructor Description MockLowLevelHttpResponse()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MockLowLevelHttpResponse
addHeader(java.lang.String name, java.lang.String value)
Adds a header to the response.void
disconnect()
Default implementation does nothing, but subclasses may override to attempt to abort the connection or release allocated system resources for this connection.java.io.InputStream
getContent()
Returns the HTTP response content input stream ornull
for none.java.lang.String
getContentEncoding()
Returns the content encoding (for example"gzip"
) ornull
for none.long
getContentLength()
Returns the content length or0
for none.java.lang.String
getContentType()
Returns the content type ornull
for none.int
getHeaderCount()
Returns the number of HTTP response headers.java.lang.String
getHeaderName(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.String
getHeaderValue(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.String
getReasonPhrase()
Returns the HTTP reason phrase ornull
for none.int
getStatusCode()
Returns the response status code or<=0
for none.java.lang.String
getStatusLine()
Returns the response status line ornull
for none.boolean
isDisconnected()
Returns whetherdisconnect()
has been called.MockLowLevelHttpResponse
setContent(byte[] byteContent)
Sets the response content to the given byte array.MockLowLevelHttpResponse
setContent(java.io.InputStream content)
Sets the input stream content of HTTP response ornull
for none.MockLowLevelHttpResponse
setContent(java.lang.String stringContent)
Sets the response content to the given content string.MockLowLevelHttpResponse
setContentEncoding(java.lang.String contentEncoding)
Sets the content encoding ornull
for none.MockLowLevelHttpResponse
setContentLength(long contentLength)
Sets the content length or-1
for unknown.MockLowLevelHttpResponse
setContentType(java.lang.String contentType)
Sets the content type of HTTP response ornull
for none.MockLowLevelHttpResponse
setHeaderNames(java.util.List<java.lang.String> headerNames)
Sets the list of header names of HTTP response.MockLowLevelHttpResponse
setHeaderValues(java.util.List<java.lang.String> headerValues)
Sets the list of header values of HTTP response.MockLowLevelHttpResponse
setReasonPhrase(java.lang.String reasonPhrase)
Sets the HTTP reason phrase ornull
for none.MockLowLevelHttpResponse
setStatusCode(int statusCode)
Sets the status code of HTTP response.MockLowLevelHttpResponse
setZeroContent()
Sets the content tonull
and the content length to 0.
-
-
-
Field Detail
-
content
private java.io.InputStream content
Input stream content of HTTP response ornull
by default.
-
contentType
private java.lang.String contentType
Content type of HTTP response ornull
by default.
-
statusCode
private int statusCode
Status code of HTTP response or200
by default.
-
reasonPhrase
private java.lang.String reasonPhrase
HTTP reason phrase ornull
for 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 ornull
for none.
-
contentLength
private long contentLength
Content length or-1
if 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 useTestableByteArrayInputStream
with the UTF-8 encoded string content.- Parameters:
stringContent
- content string ornull
for none
-
setContent
public MockLowLevelHttpResponse setContent(byte[] byteContent)
Sets the response content to the given byte array.- Parameters:
byteContent
- content byte array, ornull
for none.If the byte array is
null
, the method invokessetZeroContent()
. Otherwise,byteContent
is wrapped in aTestableByteArrayInputStream
and becomes thisMockLowLevelHttpResponse
's contents.- Since:
- 1.18
-
setZeroContent
public MockLowLevelHttpResponse setZeroContent()
Sets the content tonull
and 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.IOException
Description copied from class:LowLevelHttpResponse
Returns the HTTP response content input stream ornull
for none.- Specified by:
getContent
in classLowLevelHttpResponse
- Throws:
java.io.IOException
- I/O exception
-
getContentEncoding
public java.lang.String getContentEncoding()
Description copied from class:LowLevelHttpResponse
Returns the content encoding (for example"gzip"
) ornull
for none.- Specified by:
getContentEncoding
in classLowLevelHttpResponse
-
getContentLength
public long getContentLength()
Description copied from class:LowLevelHttpResponse
Returns the content length or0
for none.- Specified by:
getContentLength
in classLowLevelHttpResponse
-
getContentType
public final java.lang.String getContentType()
Description copied from class:LowLevelHttpResponse
Returns the content type ornull
for none.- Specified by:
getContentType
in classLowLevelHttpResponse
-
getHeaderCount
public int getHeaderCount()
Description copied from class:LowLevelHttpResponse
Returns 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:
getHeaderCount
in classLowLevelHttpResponse
-
getHeaderName
public java.lang.String getHeaderName(int index)
Description copied from class:LowLevelHttpResponse
Returns the HTTP response header name at the given zero-based index.- Specified by:
getHeaderName
in classLowLevelHttpResponse
-
getHeaderValue
public java.lang.String getHeaderValue(int index)
Description copied from class:LowLevelHttpResponse
Returns the HTTP response header value at the given zero-based index.- Specified by:
getHeaderValue
in classLowLevelHttpResponse
-
getReasonPhrase
public java.lang.String getReasonPhrase()
Description copied from class:LowLevelHttpResponse
Returns the HTTP reason phrase ornull
for none.- Specified by:
getReasonPhrase
in classLowLevelHttpResponse
-
getStatusCode
public int getStatusCode()
Description copied from class:LowLevelHttpResponse
Returns the response status code or<=0
for none.- Specified by:
getStatusCode
in classLowLevelHttpResponse
-
getStatusLine
public java.lang.String getStatusLine()
Description copied from class:LowLevelHttpResponse
Returns the response status line ornull
for none.- Specified by:
getStatusLine
in 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 ornull
for none.- Since:
- 1.5
-
setContentType
public MockLowLevelHttpResponse setContentType(java.lang.String contentType)
Sets the content type of HTTP response ornull
for none.- Since:
- 1.5
-
setContentEncoding
public MockLowLevelHttpResponse setContentEncoding(java.lang.String contentEncoding)
Sets the content encoding ornull
for none.- Since:
- 1.5
-
setContentLength
public MockLowLevelHttpResponse setContentLength(long contentLength)
Sets the content length or-1
for 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 ornull
for none.- Since:
- 1.6
-
disconnect
public void disconnect() throws java.io.IOException
Description copied from class:LowLevelHttpResponse
Default implementation does nothing, but subclasses may override to attempt to abort the connection or release allocated system resources for this connection.- Overrides:
disconnect
in classLowLevelHttpResponse
- Throws:
java.io.IOException
- I/O exception
-
isDisconnected
public boolean isDisconnected()
Returns whetherdisconnect()
has been called.- Since:
- 1.14
-
-