Skip to content

Instantly share code, notes, and snippets.

@Zulqurnain
Last active December 27, 2021 23:39
Show Gist options
  • Save Zulqurnain/ed1d605333ec8e771e6904914b2129cd to your computer and use it in GitHub Desktop.
Save Zulqurnain/ed1d605333ec8e771e6904914b2129cd to your computer and use it in GitHub Desktop.
MockCallInterceptor working in kotlin using Gson
class MockCallInterceptor<T> constructor(val value: T) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val uri = chain.request().url().uri().toString()
val responseString: String = Gson().toJson(T::class.java)
val response = Response.Builder()
.code(200)
.message(responseString)
.request(chain.request())
.protocol(Protocol.HTTP_1_0)
.body(
ResponseBody.create(
MediaType.parse("application/json"),
responseString
)
)
.addHeader("content-type", "application/json")
.build()
return response
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment