{"id":16558883,"url":"https://github.com/gmazzo/okhttp-client-mock","last_synced_at":"2025-04-04T08:06:46.217Z","repository":{"id":45559413,"uuid":"99586427","full_name":"gmazzo/okhttp-client-mock","owner":"gmazzo","description":"A simple OKHttp client mock, using a programmable request interceptor","archived":false,"fork":false,"pushed_at":"2024-10-21T22:57:01.000Z","size":486,"stargazers_count":122,"open_issues_count":0,"forks_count":21,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-10-22T19:20:46.551Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gmazzo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"gmazzo"}},"created_at":"2017-08-07T14:25:07.000Z","updated_at":"2024-10-21T22:57:04.000Z","dependencies_parsed_at":"2023-10-17T04:31:14.619Z","dependency_job_id":"e4931af0-b847-4f40-8611-ed36f3995b18","html_url":"https://github.com/gmazzo/okhttp-client-mock","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmazzo%2Fokhttp-client-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmazzo%2Fokhttp-client-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmazzo%2Fokhttp-client-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmazzo%2Fokhttp-client-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gmazzo","download_url":"https://codeload.github.com/gmazzo/okhttp-client-mock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247142052,"owners_count":20890652,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T20:24:03.374Z","updated_at":"2025-04-04T08:06:46.185Z","avatar_url":"https://github.com/gmazzo.png","language":"Java","funding_links":["https://github.com/sponsors/gmazzo"],"categories":["测试"],"sub_categories":[],"readme":"# okhttp-client-mock\n\nA simple OKHttp client mock, using a programmable request interceptor\n\n![GitHub](https://img.shields.io/github/license/gmazzo/okhttp-client-mock)\n[![Maven Central](https://img.shields.io/maven-central/v/com.github.gmazzo.okhttp.mock/mock-client)](https://central.sonatype.com/artifact/com.github.gmazzo.okhttp.mock/mock-client)\n[![Build Status](https://github.com/gmazzo/okhttp-client-mock/actions/workflows/build.yaml/badge.svg)](https://github.com/gmazzo/okhttp-client-mock/actions/workflows/build.yaml)\n[![codecov](https://codecov.io/gh/gmazzo/okhttp-client-mock/branch/master/graph/badge.svg)](https://codecov.io/gh/gmazzo/okhttp-client-mock)\n[![Users](https://img.shields.io/badge/users_by-Sourcegraph-purple)](https://sourcegraph.com/search?q=content:okhttp-mock\\b+content:com.github.gmazzo.okhttp+-repo:github.com/gmazzo/okhttp-client-mock\u0026patternType=regexp)\n\n## Import\n\nOn your `build.gradle` add:\n\n```groovy\ndependencies {\n    testImplementation 'com.github.gmazzo.okhttp.mock:mock-client:\u003cversion\u003e'\n}\n```\n\n## Usage\n\nCreate an OkHttp request interceptor and record some rules, for instance:\n\n```kotlin\nval interceptor = MockInterceptor().apply {\n\n    rule(get or post or put, url eq \"https://testserver/api/login\") {\n        respond(HTTP_401_UNAUTHORIZED).header(\"WWW-Authenticate\", \"Basic\")\n    }\n\n    rule(url eq \"https://testserver/api/json\") {\n        respond(\"{succeed:true}\", MEDIATYPE_JSON)\n    }\n\n    rule(url eq \"https://testserver/api/json\") {\n        respond(resource(\"sample.json\"), MEDIATYPE_JSON)\n    }\n\n    rule(path matches \"/aPath/(\\\\w+)\".toRegex(), times = anyTimes) {\n        respond { body(\"Path was \" + it.url().encodedPath()) }\n    }\n\n    rule(delete) {\n        respond(code = HTTP_405_METHOD_NOT_ALLOWED) {\n            body(\"{succeed:false}\", MEDIATYPE_JSON)\n        }\n    }\n\n    // throw an exception\n    rule(get) {\n        respond { throw IllegalStateException(\"an IO error\") }\n    }\n\n}\n```\n\nOr in Java:\n\n```java\nMockInterceptor interceptor = new MockInterceptor();\n\ninterceptor.addRule()\n        .get().or().post().or().put()\n        .url(\"https://testserver/api/login\")\n        .respond(HTTP_401_UNAUTHORIZED)\n        .header(\"WWW-Authenticate\", \"Basic\");\n\ninterceptor.addRule()\n        .get(\"https://testserver/api/json\")\n        .respond(\"{succeed:true}\", MEDIATYPE_JSON);\n\ninterceptor.addRule()\n        .get(\"https://testserver/api/json\")\n        .respond(resource(\"sample.json\"), MEDIATYPE_JSON);\n\ninterceptor.addRule()\n        .pathMatches(Pattern.compile(\"/aPath/(\\\\w+)\"))\n        .anyTimes()\n        .answer(request -\u003e new Response.Builder()\n            .code(200)\n            .body(ResponseBody.create(null, \"Path was \" + request.url().encodedPath())));\n```\n\nThen add the interceptor to your OkHttpClient client and use it as usual:\n\n```java\nOkHttpClient client = new OkHttpClient.Builder()\n                .addInterceptor(interceptor)\n                .build();\n```\n\nCheck an example [Integration Test](/library/src/test/java/okhttp3/mock/MockInterceptorITTest.java) with mocked HTTP\nresponses\n\nYou can use the following helper classes to provide mock responses from resources:\n\n- `ClasspathResources.resource` to load content from classpath\n- `AndroidResources.asset` to load content from an Android's asset\n- `AndroidResources.rawRes` to load content from an Android's raw resource\n- `RoboResources.asset` and `RoboResources.rawRes` if you are\n  running [Roboelectric](https://github.com/robolectric/robolectric) tests\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmazzo%2Fokhttp-client-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgmazzo%2Fokhttp-client-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmazzo%2Fokhttp-client-mock/lists"}