Class MockHttpURLConnection
- java.lang.Object
-
- java.net.URLConnection
-
- java.net.HttpURLConnection
-
- com.google.api.client.testing.http.javanet.MockHttpURLConnection
-
@Beta public class MockHttpURLConnection extends HttpURLConnection
- Since:
- 1.11
- Author:
- Yaniv Inbar
-
-
Field Summary
Fields Modifier and Type Field Description static byte[]
ERROR_BUF
Deprecated.As of 1.20.static byte[]
INPUT_BUF
Deprecated.As of 1.20.-
Fields inherited from class java.net.HttpURLConnection
chunkLength, fixedContentLength, fixedContentLengthLong, HTTP_ACCEPTED, HTTP_BAD_GATEWAY, HTTP_BAD_METHOD, HTTP_BAD_REQUEST, HTTP_CLIENT_TIMEOUT, HTTP_CONFLICT, HTTP_CREATED, HTTP_ENTITY_TOO_LARGE, HTTP_FORBIDDEN, HTTP_GATEWAY_TIMEOUT, HTTP_GONE, HTTP_INTERNAL_ERROR, HTTP_LENGTH_REQUIRED, HTTP_MOVED_PERM, HTTP_MOVED_TEMP, HTTP_MULT_CHOICE, HTTP_NO_CONTENT, HTTP_NOT_ACCEPTABLE, HTTP_NOT_AUTHORITATIVE, HTTP_NOT_FOUND, HTTP_NOT_IMPLEMENTED, HTTP_NOT_MODIFIED, HTTP_OK, HTTP_PARTIAL, HTTP_PAYMENT_REQUIRED, HTTP_PRECON_FAILED, HTTP_PROXY_AUTH, HTTP_REQ_TOO_LONG, HTTP_RESET, HTTP_SEE_OTHER, HTTP_SERVER_ERROR, HTTP_UNAUTHORIZED, HTTP_UNAVAILABLE, HTTP_UNSUPPORTED_TYPE, HTTP_USE_PROXY, HTTP_VERSION, instanceFollowRedirects, method, responseCode, responseMessage
-
Fields inherited from class java.net.URLConnection
allowUserInteraction, connected, doInput, doOutput, ifModifiedSince, url, useCaches
-
-
Constructor Summary
Constructors Constructor Description MockHttpURLConnection(URL u)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MockHttpURLConnection
addHeader(String name, String value)
Sets a custom response header.void
connect()
void
disconnect()
boolean
doOutputCalled()
Returns whetherURLConnection.doOutput
was called.InputStream
getErrorStream()
String
getHeaderField(String name)
Map<String,List<String>>
getHeaderFields()
InputStream
getInputStream()
OutputStream
getOutputStream()
int
getResponseCode()
void
setDoOutput(boolean dooutput)
MockHttpURLConnection
setErrorStream(InputStream is)
Sets the error stream.MockHttpURLConnection
setInputStream(InputStream is)
Sets the input stream.MockHttpURLConnection
setOutputStream(OutputStream outputStream)
Sets the output stream ornull
to throw anUnknownServiceException
whengetOutputStream()
is called.MockHttpURLConnection
setResponseCode(int responseCode)
Sets the HTTP response status code.boolean
usingProxy()
-
Methods inherited from class java.net.HttpURLConnection
getFollowRedirects, getHeaderField, getHeaderFieldDate, getHeaderFieldKey, getInstanceFollowRedirects, getPermission, getRequestMethod, getResponseMessage, setChunkedStreamingMode, setFixedLengthStreamingMode, setFixedLengthStreamingMode, setFollowRedirects, setInstanceFollowRedirects, setRequestMethod
-
Methods inherited from class java.net.URLConnection
addRequestProperty, getAllowUserInteraction, getConnectTimeout, getContent, getContent, getContentEncoding, getContentLength, getContentLengthLong, getContentType, getDate, getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getDoInput, getDoOutput, getExpiration, getFileNameMap, getHeaderFieldInt, getHeaderFieldLong, getIfModifiedSince, getLastModified, getReadTimeout, getRequestProperties, getRequestProperty, getURL, getUseCaches, guessContentTypeFromName, guessContentTypeFromStream, setAllowUserInteraction, setConnectTimeout, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setDoInput, setFileNameMap, setIfModifiedSince, setReadTimeout, setRequestProperty, setUseCaches, toString
-
-
-
-
Field Detail
-
INPUT_BUF
@Deprecated public static final byte[] INPUT_BUF
Deprecated.As of 1.20. UsesetInputStream(InputStream)
instead.The input byte array which represents the content when the status code is less then400
.
-
ERROR_BUF
@Deprecated public static final byte[] ERROR_BUF
Deprecated.As of 1.20. UsesetErrorStream(InputStream)
instead.The error byte array which represents the content when the status code is greater or equal to400
.
-
-
Constructor Detail
-
MockHttpURLConnection
public MockHttpURLConnection(URL u)
- Parameters:
u
- the URL ornull
for none
-
-
Method Detail
-
disconnect
public void disconnect()
- Specified by:
disconnect
in classHttpURLConnection
-
usingProxy
public boolean usingProxy()
- Specified by:
usingProxy
in classHttpURLConnection
-
connect
public void connect() throws IOException
- Specified by:
connect
in classURLConnection
- Throws:
IOException
-
getResponseCode
public int getResponseCode() throws IOException
- Overrides:
getResponseCode
in classHttpURLConnection
- Throws:
IOException
-
setDoOutput
public void setDoOutput(boolean dooutput)
- Overrides:
setDoOutput
in classURLConnection
-
getOutputStream
public OutputStream getOutputStream() throws IOException
- Overrides:
getOutputStream
in classURLConnection
- Throws:
IOException
-
doOutputCalled
public final boolean doOutputCalled()
Returns whetherURLConnection.doOutput
was called.
-
setOutputStream
public MockHttpURLConnection setOutputStream(OutputStream outputStream)
Sets the output stream ornull
to throw anUnknownServiceException
whengetOutputStream()
is called.By default it is
null
.
-
setResponseCode
public MockHttpURLConnection setResponseCode(int responseCode)
Sets the HTTP response status code.
-
addHeader
public MockHttpURLConnection addHeader(String name, String value)
Sets a custom response header.- Since:
- 1.20
-
setInputStream
public MockHttpURLConnection setInputStream(InputStream is)
Sets the input stream.To prevent incidental overwrite, only the first non-null assignment is honored.
- Since:
- 1.20
-
setErrorStream
public MockHttpURLConnection setErrorStream(InputStream is)
Sets the error stream.To prevent incidental overwrite, only the first non-null assignment is honored.
- Since:
- 1.20
-
getInputStream
public InputStream getInputStream() throws IOException
- Overrides:
getInputStream
in classURLConnection
- Throws:
IOException
-
getErrorStream
public InputStream getErrorStream()
- Overrides:
getErrorStream
in classHttpURLConnection
-
getHeaderFields
public Map<String,List<String>> getHeaderFields()
- Overrides:
getHeaderFields
in classURLConnection
-
getHeaderField
public String getHeaderField(String name)
- Overrides:
getHeaderField
in classURLConnection
-
-