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
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'
}
- 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;
}
};
- Use the
InputStreamProvider
to construct theOkHttpMockInterceptor
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