Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

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

License

Notifications You must be signed in to change notification settings

ATizik/okhttp-json-mock

 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%