Skip to content

Instantly share code, notes, and snippets.

@wahibhaq
Last active April 21, 2023 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wahibhaq/20124ed53ae0e3929ca90bb44c96ad78 to your computer and use it in GitHub Desktop.
Save wahibhaq/20124ed53ae0e3929ca90bb44c96ad78 to your computer and use it in GitHub Desktop.
/**
* This will help us to test our networking code while a particular API is not implemented
* yet on Backend side.
*/
class MockInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
if (BuildConfig.DEBUG) {
val uri = chain.request().url().uri().toString()
val responseString = when {
uri.endsWith("starred") -> getListOfReposBeingStarredJson
else -> ""
}
return chain.proceed(chain.request())
.newBuilder()
.code(SUCCESS_CODE)
.protocol(Protocol.HTTP_2)
.message(responseString)
.body(ResponseBody.create(MediaType.parse("application/json"),
responseString.toByteArray()))
.addHeader("content-type", "application/json")
.build()
} else {
//just to be on safe side.
throw IllegalAccessError("MockInterceptor is only meant for Testing Purposes and " +
"bound to be used only with DEBUG mode")
}
}
}
const val getListOfReposBeingStarredJson = """
[{
"id": 1296269,
"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
"name": "Hello-World",
"full_name": "octocat/Hello-World",
"private": false,
"html_url": "https://github.com/octocat/Hello-World",
"description": "This your first repo!",
"fork": false,
"languages_url": "http://api.github.com/repos/octocat/Hello-World/languages",
"stargazers_count": 80,
"watchers_count": 80,
"pushed_at": "2011-01-26T19:06:43Z",
"created_at": "2011-01-26T19:01:12Z",
"updated_at": "2011-01-26T19:14:43Z",
"subscribers_count": 42
}]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment