Package okhttp3.mock

Class MockInterceptor

  • All Implemented Interfaces:
    okhttp3.Interceptor

    public class MockInterceptor
    extends java.lang.Object
    implements okhttp3.Interceptor
    An Interceptor for OkHttpClient, which with match request and provide pre-configured mock responses.
    • Constructor Detail

      • MockInterceptor

        public MockInterceptor()
        Creates a MockInterceptor with a default Behavior.SEQUENTIAL behavior
      • MockInterceptor

        public MockInterceptor​(Behavior behavior)
    • Method Detail

      • getRules

        public java.util.List<Rule> getRules()
      • addRule

        public MockInterceptor addRule​(okhttp3.Response.Builder builder)
        Adds a new mock rule to this interceptor. Use Rule.Builder to create one and call the final statements Rule.Builder.respond(java.lang.String).

        Example:

        
          interceptor.addRule(new Rule.Builder()
              .get()
              .urlStarts("https://someserver/")
              .respond(HTTP_200_OK)
                  .header("SomeHeader", "SomeValue"));
         
        Parameters:
        builder - the rule to add
        Returns:
        this instance
      • addRule

        public MockInterceptor addRule​(Rule rule)
        Adds a new mock rule to this interceptor.
        Parameters:
        rule - the rule to add
        Returns:
        this instance
      • intercept

        public okhttp3.Response intercept​(okhttp3.Interceptor.Chain chain)
                                   throws java.io.IOException
        Specified by:
        intercept in interface okhttp3.Interceptor
        Throws:
        java.io.IOException