/okhttp-json-mock

Mock your datas for Okhttp and Retrofit in json format in just a few moves

Primary LanguageJavaApache License 2.0Apache-2.0

okhttp-mock-suit

Download

Simple interceptor for OkHttp that replaces remote data with any data that you provide - put mock reponses in local storage or assets folder and replace away

Usage

First add jitpack to your projects build.gradle file

allprojects {
   	repositories {
   		...
   		maven { url "https://jitpack.io" }
   	}
}

Then add the dependency in modules build.gradle file

dependencies {
    implementation'studio.icecreamhappens:okhttp-mock-suit:4.0-alpha03'
 }
  1. Construct your custom InputStreamProvider:
InputStreamProvider inputStreamProvider = new InputStreamProvider() {
    @Override
    public InputStream provide(String path) {
        try {
            return getAssets().open(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
};
  1. Use the InputStreamProvider to construct the OkHttpMockInterceptor and client:
OkHttpClient client = new OkHttpClient.Builder()
    .addInterceptor(new OkHttpMockInterceptor(getAndroidProvider(), 5))
    .build();

4. Implement listener, for example if you want to use AlertDialog to choose appropriate json reponse:

ResponsesQueue.getInstance().setListener { responseHandler ->
                launch(Dispatchers.Main) {
                    val list = responseHandler.files.toTypedArray()
                    AlertDialog.Builder(this@MainActivit)
                        .setTitle(responseHandler.methodName)
                        .setItems(list) { dialog, which ->
                            responseHandler.fileName = list[which]
                        }.apply {
                            setOnDismissListener {
                                responseHandler.dismissed()
                            }
                            show()
                        }
                }
            }

Original idea and code: https://github.com/mirrajabi/okhttp-json-mock