Skip to content

Instantly share code, notes, and snippets.

@wahibhaq
Last active April 21, 2023 10:03
Show Gist options
  • Select an option

  • Save wahibhaq/20124ed53ae0e3929ca90bb44c96ad78 to your computer and use it in GitHub Desktop.

Select an option

Save wahibhaq/20124ed53ae0e3929ca90bb44c96ad78 to your computer and use it in GitHub Desktop.

Revisions

  1. wahibhaq renamed this gist Mar 19, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MockedInterceptor.kt → MockInterceptor.kt
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * This will help us to test our networking code while a particular API is not implemented
    * yet on Backend side.
    */
    class MockedInterceptor : Interceptor {
    class MockInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
    if (BuildConfig.DEBUG) {
    @@ -24,7 +24,7 @@ class MockedInterceptor : Interceptor {
    .build()
    } else {
    //just to be on safe side.
    throw IllegalAccessError("MockedInterceptor is only meant for Testing Purposes and " +
    throw IllegalAccessError("MockInterceptor is only meant for Testing Purposes and " +
    "bound to be used only with DEBUG mode")
    }
    }
  2. wahibhaq revised this gist Mar 19, 2019. 1 changed file with 25 additions and 20 deletions.
    45 changes: 25 additions & 20 deletions MockedInterceptor.kt
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,8 @@
    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
    }]
    """

    /**
    * This will help us to test our networking code while a particular API is not implemented
    * yet on Backend side.
    */
    class MockedInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
    @@ -44,4 +29,24 @@ class MockedInterceptor : Interceptor {
    }
    }

    }
    }

    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
    }]
    """
  3. wahibhaq revised this gist Mar 19, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MockedInterceptor.kt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    const val getListRepositoriesBeingStarredJson = """
    const val getListOfReposBeingStarredJson = """
    [{
    "id": 1296269,
    "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
    @@ -24,7 +24,7 @@ class MockedInterceptor : Interceptor {
    if (BuildConfig.DEBUG) {
    val uri = chain.request().url().uri().toString()
    val responseString = when {
    uri.endsWith("starred") -> getListRepositoriesBeingStarredJson
    uri.endsWith("starred") -> getListOfReposBeingStarredJson
    else -> ""
    }

  4. wahibhaq revised this gist Mar 19, 2019. 1 changed file with 20 additions and 9 deletions.
    29 changes: 20 additions & 9 deletions MockedInterceptor.kt
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,30 @@
    const val getListRepositoriesBeingStarredJson = """
    [{
    "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
    }]
    """

    class MockedInterceptor : Interceptor {

    private fun buildJsonResponse(body: String) = "{\n" +
    "\t\t\t\t$body\n" +
    "\t\t\t,\n" +
    "\t\t\t\"response\": {\n" +
    "\t\t\t\t\"code\": 200\n" +
    "\t\t\t}\n" +
    "\t\t}"

    override fun intercept(chain: Interceptor.Chain): Response {
    if (BuildConfig.DEBUG) {
    val uri = chain.request().url().uri().toString()
    val responseString = when {
    uri.endsWith("getAllItems") -> buildJsonResponse(getAllItems)
    uri.endsWith("starred") -> getListRepositoriesBeingStarredJson
    else -> ""
    }

  5. wahibhaq revised this gist Mar 19, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MockedInterceptor.kt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@

    class FakeInterceptor : Interceptor {
    class MockedInterceptor : Interceptor {

    private fun buildJsonResponse(body: String) = "{\n" +
    "\t\t\t\t$body\n" +
  6. wahibhaq revised this gist Feb 21, 2019. 1 changed file with 31 additions and 23 deletions.
    54 changes: 31 additions & 23 deletions MockedInterceptor.kt
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,36 @@

    class FakeInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
    if (BuildConfig.DEBUG) {
    val uri = chain.request().url().uri().toString()
    val responseString = when {
    uri.endsWith("getAllItems") -> buildJsonResponse(getAllItems)
    else -> ""
    }
    private fun buildJsonResponse(body: String) = "{\n" +
    "\t\t\t\t$body\n" +
    "\t\t\t,\n" +
    "\t\t\t\"response\": {\n" +
    "\t\t\t\t\"code\": 200\n" +
    "\t\t\t}\n" +
    "\t\t}"

    override fun intercept(chain: Interceptor.Chain): Response {
    if (BuildConfig.DEBUG) {
    val uri = chain.request().url().uri().toString()
    val responseString = when {
    uri.endsWith("getAllItems") -> buildJsonResponse(getAllItems)
    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("MockedInterceptor is only meant for Testing Purposes and " +
    "bound to be used only with DEBUG mode")
    }
    }

    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("MockedInterceptor is only meant for Testing Purposes and " +
    "bound to be used only with DEBUG mode")
    }
    }

    }
  7. wahibhaq created this gist Feb 21, 2019.
    28 changes: 28 additions & 0 deletions MockedInterceptor.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@

    class FakeInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
    if (BuildConfig.DEBUG) {
    val uri = chain.request().url().uri().toString()
    val responseString = when {
    uri.endsWith("getAllItems") -> buildJsonResponse(getAllItems)
    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("MockedInterceptor is only meant for Testing Purposes and " +
    "bound to be used only with DEBUG mode")
    }
    }

    }