Class MockHttpURLConnection


  • @Beta
    public class MockHttpURLConnection
    extends java.net.HttpURLConnection
    Beta
    Mock for HttpURLConnection.

    Implementation is not thread-safe.

    Since:
    1.11
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean doOutputCalled
      Whether URLConnection.doOutput was called.
      static byte[] ERROR_BUF
      Deprecated.
      As of 1.20.
      private java.io.InputStream errorStream
      The error stream.
      private java.util.Map<java.lang.String,​java.util.List<java.lang.String>> headers  
      static byte[] INPUT_BUF
      Deprecated.
      As of 1.20.
      private java.io.InputStream inputStream
      The input stream.
      private java.io.OutputStream outputStream
      Output stream or null to throw an UnknownServiceException when getOutputStream() is called.
      private boolean setFixedLengthStreamingModeIntCalled
      private boolean setFixedLengthStreamingModeLongCalled
      • 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
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      MockHttpURLConnection addHeader​(java.lang.String name, java.lang.String value)
      Sets a custom response header.
      void connect()  
      void disconnect()  
      boolean doOutputCalled()
      Returns whether URLConnection.doOutput was called.
      int getChunkLength()  
      java.io.InputStream getErrorStream()  
      java.lang.String getHeaderField​(java.lang.String name)  
      java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getHeaderFields()  
      java.io.InputStream getInputStream()  
      java.io.OutputStream getOutputStream()  
      int getResponseCode()  
      boolean isSetFixedLengthStreamingModeIntCalled()  
      boolean isSetFixedLengthStreamingModeLongCalled()  
      void setDoOutput​(boolean dooutput)  
      MockHttpURLConnection setErrorStream​(java.io.InputStream is)
      Sets the error stream.
      void setFixedLengthStreamingMode​(int contentLength)  
      void setFixedLengthStreamingMode​(long contentLength)  
      MockHttpURLConnection setInputStream​(java.io.InputStream is)
      Sets the input stream.
      MockHttpURLConnection setOutputStream​(java.io.OutputStream outputStream)
      Sets the output stream or null to throw an UnknownServiceException when getOutputStream() 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, 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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • doOutputCalled

        private boolean doOutputCalled
        Whether URLConnection.doOutput was called.
      • outputStream

        private java.io.OutputStream outputStream
        Output stream or null to throw an UnknownServiceException when getOutputStream() is called.
      • INPUT_BUF

        @Deprecated
        public static final byte[] INPUT_BUF
        Deprecated.
        As of 1.20. Use setInputStream(InputStream) instead.
        The input byte array which represents the content when the status code is less then 400.
      • ERROR_BUF

        @Deprecated
        public static final byte[] ERROR_BUF
        Deprecated.
        As of 1.20. Use setErrorStream(InputStream) instead.
        The error byte array which represents the content when the status code is greater or equal to 400.
      • inputStream

        private java.io.InputStream inputStream
        The input stream.
      • errorStream

        private java.io.InputStream errorStream
        The error stream.
      • headers

        private java.util.Map<java.lang.String,​java.util.List<java.lang.String>> headers
    • Constructor Detail

      • MockHttpURLConnection

        public MockHttpURLConnection​(java.net.URL u)
        Parameters:
        u - the URL or null for none
    • Method Detail

      • disconnect

        public void disconnect()
        Specified by:
        disconnect in class java.net.HttpURLConnection
      • usingProxy

        public boolean usingProxy()
        Specified by:
        usingProxy in class java.net.HttpURLConnection
      • connect

        public void connect()
                     throws java.io.IOException
        Specified by:
        connect in class java.net.URLConnection
        Throws:
        java.io.IOException
      • getResponseCode

        public int getResponseCode()
                            throws java.io.IOException
        Overrides:
        getResponseCode in class java.net.HttpURLConnection
        Throws:
        java.io.IOException
      • setDoOutput

        public void setDoOutput​(boolean dooutput)
        Overrides:
        setDoOutput in class java.net.URLConnection
      • getOutputStream

        public java.io.OutputStream getOutputStream()
                                             throws java.io.IOException
        Overrides:
        getOutputStream in class java.net.URLConnection
        Throws:
        java.io.IOException
      • doOutputCalled

        public final boolean doOutputCalled()
        Returns whether URLConnection.doOutput was called.
      • setOutputStream

        public MockHttpURLConnection setOutputStream​(java.io.OutputStream outputStream)
        Sets the output stream or null to throw an UnknownServiceException when getOutputStream() is called.

        By default it is null.

      • setResponseCode

        public MockHttpURLConnection setResponseCode​(int responseCode)
        Sets the HTTP response status code.
      • addHeader

        public MockHttpURLConnection addHeader​(java.lang.String name,
                                               java.lang.String value)
        Sets a custom response header.
        Since:
        1.20
      • setInputStream

        public MockHttpURLConnection setInputStream​(java.io.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​(java.io.InputStream is)
        Sets the error stream.

        To prevent incidental overwrite, only the first non-null assignment is honored.

        Since:
        1.20
      • getInputStream

        public java.io.InputStream getInputStream()
                                           throws java.io.IOException
        Overrides:
        getInputStream in class java.net.URLConnection
        Throws:
        java.io.IOException
      • getErrorStream

        public java.io.InputStream getErrorStream()
        Overrides:
        getErrorStream in class java.net.HttpURLConnection
      • getHeaderFields

        public java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getHeaderFields()
        Overrides:
        getHeaderFields in class java.net.URLConnection
      • getHeaderField

        public java.lang.String getHeaderField​(java.lang.String name)
        Overrides:
        getHeaderField in class java.net.URLConnection
      • getChunkLength

        public int getChunkLength()
      • setFixedLengthStreamingMode

        public void setFixedLengthStreamingMode​(int contentLength)
        Overrides:
        setFixedLengthStreamingMode in class java.net.HttpURLConnection
      • setFixedLengthStreamingMode

        public void setFixedLengthStreamingMode​(long contentLength)
        Overrides:
        setFixedLengthStreamingMode in class java.net.HttpURLConnection
      • isSetFixedLengthStreamingModeIntCalled

        public boolean isSetFixedLengthStreamingModeIntCalled()
      • isSetFixedLengthStreamingModeLongCalled

        public boolean isSetFixedLengthStreamingModeLongCalled()